Guide to Setting Up Multiple Repository in Maven

Setting up multiple repository in Maven can be achieved using two ways.

  1. Specify in a POM which repositories you want to use
  2. Specify by creating a profile in settings.xml

Note: What is Maven Repository – is discussed in this article. An overlook in this article will give you more depth in topic.

Example for Multiple Repository using POM

<project>
...
  <repositories>
    <repository>
      <id>my-repo1</id>
      <name>your custom repo1 name</name>
      <url>http://myrepo-url.com</url>
    </repository>
    <repository>
      <id>my-repo2</id>
      <name>your custom repo2 name</name>
      <url>http://myrepo2-url.com</url>
    </repository>
  </repositories>
...
</project>

Example for Multiple Repository using settings.xml

The second way of specifying multiple repositories is by creating a profile in your ${user.home}/.m2/settings.xml file like the following:

<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
 ...
 <profiles>
   ...
   <profile>
     <id>myprofile</id>
     <repositories>
       <repository>
         <id>my-repo2</id>
         <name>your custom repo</name>
         <url>http://jarsm2.dyndns.dk</url>
       </repository>
     </repositories>
   </profile>
   ...
 </profiles>
 
 <activeProfiles>
   <activeProfile>myprofile</activeProfile>
 </activeProfiles>
 ...
</settings>

If you specify repositories in profiles you must remember to activate that particular profile! As you can see above we do this by registering a profile to be active in the activeProfiles element.

You could also activate this profile on the command like by executing the following command:

mvn -Pmyprofile ...

In fact the -P option will take a CSV list of profiles to activate if you wish to activate multiple profiles simultaneously.

Downloading from a Remote Repository

Downloading in Maven is triggered by a project declaring a dependency that is not present in the local repository (or for a SNAPSHOT, when the remote repository contains one that is newer). By default, Maven will download from the central repository.

To override this, you need to specify a mirror as shown as below,

<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
 ...
 <mirrors>
    <mirror>
      <mirrorOf>*</mirrorOf>
      <id>my-repo</id>
      <name>my custom repo</name>
      <url>http://myrepo-url/latest/</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
 </mirrors>

 <profiles>
   ...
   <profile>
     <id>myprofile</id>
     <repositories>
       <repository>
         <id>my-repo2</id>
         <name>your custom repo</name>
         <url>http://my-repo2-urlk</url>
       </repository>
     </repositories>
   </profile>
   ...
 </profiles>
 
 <activeProfiles>
   <activeProfile>myprofile</activeProfile>
 </activeProfiles>
 ...
</settings>

You can set this in your settings.xml file to globally use a certain mirror. However, it is common for a project to customise the repository in its pom.xml and that your setting will take precedence. If dependencies are not being found, check that you have not overridden the remote repository.

Be careful not to include extra whitespace around identifiers or wildcards in comma separated lists. For example, a mirror with <mirrorOf> set to !repo1, * will not mirror anything while !repo1,* will mirror everything but repo1.

Building Offline

If you are temporarily disconnected from the internet and you need to build your projects offline, you can use the offline switch on the CLI:

mvn -o package

Many plugins honor the offline setting and do not perform any operations that connect to the internet. Some examples are resolving Javadoc links and link checking the site.

Conclusion

Hence we had a detailed look on following topics based on setting up multiple repository in maven using POM, by creating a profile in settings.xml. Also, we had a quick look on downloading from remote repository and packaging offline.

Please follow and like us:
error