Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Settings for multiple repositories

I have the following in settings.xml

<mirrors>        <mirror>           <id>paid-jars</id>           <name>jars with license</name>           <url>http://url:8081/nexus/content/repositories/paidjars/</url>           <mirrorOf>!central</mirrorOf>       </mirror>       <mirror>           <id>Org-central</id>           <name>mirror of central</name>           <url>http://url:8081/nexus/content/repositories/central/</url>           <mirrorOf>central</mirrorOf>       </mirror>   </mirrors> 

In pom.xml I have two jars

  1. apache-commons.jar (which I assumes to be downloaded from central)
  2. licensed.jar (which I assume to be downloaded from paid-jars)

But when I run maven clean install it tries to download licensed.jar from Org-central.

How can I make it use paid-jars to download? Is it possible first it goes to Org-central and if fails it tries at paid-jars? If so, how? I don't want to put repo entries in pom.xml


Settings.xml

<?xml version="1.0" encoding="UTF-8"?>     <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0                  http://maven.apache.org/xsd/settings-1.0.0.xsd">   <proxies>         <proxy>       <id>Proxy</id>       <active>true</active>       <protocol>http</protocol>       <username>username</username>       <password>******</password>       <host>host.url</host>       <port>8080</port>       <nonProxyHosts>local.net|internal.com</nonProxyHosts>     </proxy>   </proxies>  <mirrors>        <mirror>           <id>paid-jars</id>           <name>jars with license</name>           <url>http://url:8081/nexus/content/repositories/paidjars/</url>           <mirrorOf>!central</mirrorOf>       </mirror>       <mirror>           <id>Org-central</id>           <name>mirror of central</name>           <url>http://url:8081/nexus/content/repositories/central/</url>           <mirrorOf>central</mirrorOf>       </mirror>   </mirrors>   <profiles>       <profile>           <id>compiler</id>           <properties>               <JAVA_1_7_HOME>C:\Program Files (x86)\Java\jdk1.7.0_51\bin</JAVA_1_7_HOME>           </properties>       </profile>   </profiles> </settings> 
like image 516
Rohit Sachan Avatar asked Aug 21 '14 10:08

Rohit Sachan


People also ask

What is mirror in Maven settings?

You can force Maven to use a single repository by having it mirror all repository requests. The repository must contain all of the desired artifacts, or be able to proxy the requests to other repositories.

What is m2 repository in Maven?

m2 folder is the default folder used by maven to store its: settings. xml file which specifies properties, like the central repository to download your dependencies, the location of the so-called localRepository. by default, the localRepository in which maven stores all the dependencies your project might need to run.

How many repositories are there in Maven?

A repository in Maven holds build artifacts and dependencies of varying types. There are exactly two types of repositories: local and remote: the local repository is a directory on the computer where Maven runs. It caches remote downloads and contains temporary build artifacts that you have not yet released.


2 Answers

you have to setup mirror

<mirror>   <id>nexus</id>   <mirrorOf>*</mirrorOf>   <url>http://internal/nexus/content/repositories/thirdparty</url> </mirror>   <mirror>   <id>google</id>   <mirrorOf>google</mirrorOf>   <url>http://google-maven-repository.googlecode.com/svn/repository</url> </mirror>    

then add internal & external repo

<profile>      <id>nexus</id>   <repositories>      <repository>       <id>central</id>       <name>central</name>       <url>http://internal/nexus/content/repositories/thirdparty</url>     </repository>       <repository>       <id>google</id>       <name>google</name>       <url>http://google-maven-repository.googlecode.com/svn/repository</url>     </repository>    </repositories> </profile> 
like image 56
Ashkrit Sharma Avatar answered Sep 22 '22 18:09

Ashkrit Sharma


If you need to distinguish between an internal (Nexus, Artifactory, ..) repository and Maven central, the already discussed solutions here based on multiple Mirrors using the <mirrorOf> tags capabilities (as described in the official docs also ) work fine (the same with this so Q&A ).

BUT in our case, where we wanted some libraries to be downloaded from internal Nexus 1 - and others (with the same package names, but different versions) from internal Nexus 2, those solutions didn't work. We simply couldn't use the <mirrorOf> tag.

We found another solution based on multiple <repository> definitions INSIDE of an ever activated Maven profile (it didn't work without the profile definition!!). Here's our settings.xml:

<?xml version="1.0" encoding="UTF-8"?>  <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">      <!-- The resolution of multiple Repositories only works with profiles!-->     <profiles>         <profile>           <id>use-multiple-repos</id>           <!--Request multiple Repositories for dependencies -->           <repositories>             <repository>             <id>nexus-repository</id>             <name>Internal Nexus Repository 1 https://nexus.your.lan</name>             <url>https://nexus.your.lan/repository/maven-public/</url>             </repository>         <repository>             <id>nexus-repository-2</id>             <name>Internal Nexus Repository 2 https://nexus2.completely.other.net</name>             <url>https://nexus2.completely.other.net/repository/maven-public/</url>         </repository>           </repositories>         </profile>     </profiles>      <activeProfiles>         <!--make the profile active all the time -->         <activeProfile>use-multiple-repos</activeProfile>     </activeProfiles>  </settings> 

If you're looking for a full-blown settings.xml, where - besides the multiple repositories - also a corporate proxy is defined alongside with the credentials for maven-releases and maven-snapshots users to push to the corporate Nexus 1, have a look at this gist.

And if you want to check fast, if you're configuration is working, you could simply use maven-dependency-plugin's dependency:get:

mvn dependency:get \     -DgroupId=your.package.name \     -DartifactId=yourArtifactId \     -Dversion=YOURVERSION \     -Dpackaging=jar 

If this results in a BUILD SUCCESS, where a minimum of one dependency from Nexus 1 and one of Nexus 2 could be downloaded, everything should work as expected and looks somehow like this (omitted lot's of packages!):

[INFO] Resolving your.first.package:artifact1.jar:1.1.0 Downloaded from nexus-repository: https://nexus.your.lan/repository/maven-public/your/first/package/artifact/1.1.0/artifact2.jar (575 kB at 868 kB/s) [INFO] Resolving your.second.package:artifact2.jar:1.1.0 Downloading from nexus-repository-2: https://nexus2.completely.other.net/repository/maven-public/your/second/package/artifact2/1.1.0/artifact2.jar (14 kB at 305 kB/s) 
like image 30
jonashackt Avatar answered Sep 21 '22 18:09

jonashackt