Tuesday, August 05, 2014

Configuring Maven to Fetch Artifacts from Multiple Repositories

It is quite common to have a situation where a project needs to source for artifacts from more than one source. This post quickly explain 3 different ways to achieve this.

Repositories settings in pom.xml


In pom.xml, you have the <repositories/> tag which can be used to specify the repositories a project should search and download artifacts from. For example:




   
   
      
      example-repo1
      Name of repo
      http://url.to.example.repo.org/maven2
      default
      
      
         
         true
         
         always
         
         warn
      
      
      
         true
         never
         fail
      
   
   
   
      example-repo2
      your custom repo
      http://url.to.second.example.repo.org/maven2
   


Now the project would consult both http://url.to.example.repo.org/maven2 and http://url.to.second.example.repo.org/maven2 for artifacts.


Using Profiles in settings.xml


If you have an inhouse repository which hosts and serves artifacts, chances are that you would prefer to not have to repeat the settings for this repository across every project, but have it in a more central place not tied to individual projects.

Setting the repositories via profiles in settings.xml helps in achieving that. With it, the settings are thus independent of projects but applies across all projects.

for example in the settings.xml:


   ...
   
      ...
      
         
            true
         
         myprofile
         
            
               example-repo1
               Name of repo
               http://url.to.example.repo.org/maven2
               default
               
                  true
                  always
                  warn
               
               
                  true
                  never
                  fail
               
            
            
               example-repo2
               your custom repo
               http://url.to.second.example.repo.org/maven2
            
         
      
      ...
   
   ...


Since there can be multiple profiles, you need to specifically activate the needed profile at a point in time. In above, the section:


    true


..does just that.

You can remove the <activation/> block and specify the active profile outside the <profiles/>  tag like thus:


   ...
   
      ...
      
         my-profile
         ...
      
      
         my-second-profile
         ...
      
      ...
   
   
      my-second-profile
   
   ...


Lastly you can also activate a profile via the command line. i.e.

mvn -Pmy-second-profile help:active-profiles

Note, it is possible to activate multiple profiles at the same time.

Using Mirror Settings in settings.xml


Using a repository manager like artifactory and have it serve as a proxy that sources artifacts from other remote repositories is also a common set up. In fact this is what you usually have: an inhouse repository that mirrors all other access to any remote repository.

This way, every request for an artifact goes through the repository manager, if found, it is downloaded, if not the repository manager looks for the artifact amongst its list of configured remote repositories.

For example with Artifactory, the configuration of the remote repositories can be done following the official guide: Configuring Repositories. Other repository manager would have a similar configuration step.

The settings.xml then need to be updated.

For example:


   ...
   
      
         artifactory
         http://[host]:[port]/path/to/inhouse-artifactory/
         *
         Artifactory
      
   
   ...


The <mirrorOf/> gets the job done. This would make sure all artifact download goes through http://[host]:[port]/path/to/inhouse-artifactory/. Having this settings would override what may have been specified in the pom.xml or within the profiles tag.

Check the guide: Using Mirrors for Repositories for more information.

No comments: