Java – How to prevent Maven from checking for updates from repositories that I don’t list in the settings.xml file

javamaven-2

In my settings.xml file I have listed repositories that I want Maven to use (see the file below). These repositories are located in the build machine and I am working this way to prevent a build fail when there's is no Internet connection in the build machine.

The problem is that Maven automatically looks for updates in the central repository (and possibly from other non-listed repositories) during the build. Is there a way to prevent this behaviour?

 ...
<profile>
  <id>myProfile</id>
  <repositories>
    <repository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <updatePolicy>never</updatePolicy>
      </snapshots>
      <id>myRepo</id>
      <url>file://${my.home}/maven/.m2/repository</url>
      <layout>default</layout>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <updatePolicy>never</updatePolicy>
      </snapshots>
      <id>myRepo</id>
      <url>file://${my.home}/maven/.m2/repository</url>
      <layout>default</layout>
    </pluginRepository>
  </pluginRepositories>
</profile>
...

Note: Using the offline option (e.g. -o flag) is not an option for me. What I really want is Maven to use only the repositories that I list in my settings.xml file.

Best Answer

Every Maven project inherits the configuration for the central repository from the Maven Super POM. You can use Maven's mirrors feature to redirect calls to central to your preferred repository. You do this by adding some configuration to your settings.xml like this:

<settings>
...
  <mirrors>
    <mirror>
      <id>central-proxy</id>
      <mirrorOf>central</mirrorOf>
      <url>http://myrepository/releases</url>
    </mirror>
  </mirrors>
  ..
</settings>

This configuration can either be put in your user settings (${user.home}/.m2/settings.xml) or global settings ({$M2_HOME}/conf/settings.xml).