Rohit Sachan
Rohit Sachan

Reputation: 1222

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>

Upvotes: 42

Views: 122911

Answers (6)

eel ghEEz
eel ghEEz

Reputation: 1225

  • Define (duplicate) authentication (server) entries in settings.xml,
  <servers>
    <server>
      <id>auth-default</id>
      <username>${env.ARTIFACTORY_USER}</username>
      <password>${env.ARTIFACTORY_PASS}</password>
    </server>
    <server>
      <id>auth-libs-release-art</id>
      <username>${env.ARTIFACTORY_USER}</username>
      <password>${env.ARTIFACTORY_PASS}</password>
    </server>
[...]
  </servers>
  • Define respective mirrors with a special "catch-the-rest" in mirrorOf for the main repository and references to "repository" IDs in the other mirrorOf entries.
  <mirrors>
    <mirror>
      <id>auth-default</id>
      <url>https://artifactory.COMPANY.net/artifactory/repo.maven.apache.org</url>
      <mirrorOf>*,!libs-release,[...]</mirrorOf>
    </mirror>
    <mirror>
      <id>auth-libs-release-art</id>
      <url>https://artifactory.COMPANY.net/artifactory/libs-release</url>
      <mirrorOf>libs-release</mirrorOf>
    </mirror>
[...]
  </mirrors>
  • Define repositories with "example" URLs (the URLs will be ignored because of using the above mirrors; reusing the server IDs in the repository IDs and placing the real URLs directly in the repository entries will prevent from using the indirection via mirror definitions).
  <profiles>
    <profile>
      <id>libs-art</id>
      <repositories>
        <repository>
          <id>libs-release</id>
          <releases><enabled>true</enabled><updatePolicy>never</updatePolicy></releases>
          <url>https://example.test/libs-release</url>
        </repository>
        <repository>
          <id>libs-snapshot</id>
          <snapshots />
          <url>https://example.test/libs-snapshot</url>
        </repository>
[...]
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>libs-art</activeProfile>
  </activeProfiles>

Upvotes: 0

In the settings.xml, you need to make 3 changes

(Replace https://repository.internal/mvn-public/ with your org internal repo url)

A) Add the mirrors as below

<mirror>
  <mirrorOf>artifact-int</mirrorOf>
  <url>https://repository.internal/mvn-public/</url>
  <id>artifact-int-mirror</id>
  <name>internal Repository</name>
</mirror>
 <mirror>
  <mirrorOf>artifact-web</mirrorOf>
  <url>https://repo1.maven.org/maven2/</url>
  <id>artifact-web-mirror</id>
  <name>Amadeus Repository</name>
</mirror>

B) Update the profile section as below

 <profiles>
    <profile>
        <id>artifact.repository.int</id>
        <repositories>
            <repository>
                <id>artifact-int</id>
                <name>Internal Artifactory</name>
                <url>https://repository.internal/mvn-public/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>artifact-int</id>
                <name>Int Artifactory</name>
                <url>https://repository.internal/mvn-public/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>

     <profile>
        <id>artifact.repository.web</id>
        <repositories>
            <repository>
                <id>artifact-web</id>
                <name>Web Artifactory</name>
                <url>https://repo1.maven.org/maven2</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>artifact-web</id>
                <name>Web Artifactory</name>
                <url>https://repo1.maven.org/maven2/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>

</profiles>

C) Set the active profile as below

 <activeProfiles>
    <activeProfile>artifact.repository.int</activeProfile>
    <activeProfile>artifact.repository.web</activeProfile>
</activeProfiles>

Upvotes: 1

jonashackt
jonashackt

Reputation: 14429

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)

Upvotes: 17

Arpit Aggarwal
Arpit Aggarwal

Reputation: 29276

In settings.xml, defining mirror with id and url for the repository besides using the same in profile worked for me, as below:

<mirrors>
       <mirror>
        <id>Artifactory</id>      
        <url>http://localhost:4900/archiva/repository/</url>
        <mirrorOf>artifactory</mirrorOf>
       </mirror>
       <mirror>
        <id>MavenCentral</id>       
        <url>https://repo.maven.apache.org/maven2</url>
        <mirrorOf>central</mirrorOf>
       </mirror>
</mirrors>

<profiles>
    <profile>
        <id>Project</id>
        <properties>
           <framework.version>1.0.9</framework.version>      
           <maven.test.skip>false</maven.test.skip>
           <maven.test.failure.ignore>false</maven.test.failure.ignore> 
           <maven.javadoc.skip>false</maven.javadoc.skip>               
           <source.jdkversion>1.8</source.jdkversion>
           <target.jdkversion>1.8</target.jdkversion>
        </properties>   
        <repositories>
           <repository>
                <id>Artifactory</id>
                <name>Maven Artifactory for Project</name>
                <url>http://localhost:4900/archiva/repository/</url>
                <layout>default</layout>
                <releases>
                        <enabled>true</enabled>
                        <updatePolicy>never</updatePolicy>
                </releases>
                <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>never</updatePolicy>
                </snapshots>
            </repository>
            <repository>
                <id>MavenCentral</id>
                <url>https://repo.maven.apache.org/maven2/</url>
            </repository>
        </repositories>             
     </profile>
 </profiles>    

Upvotes: 13

Ashkrit Sharma
Ashkrit Sharma

Reputation: 637

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>

Upvotes: 22

aleung
aleung

Reputation: 10298

It's impossible to specify a dedicated repository to look up an artifact. Maven will look up all configured repositories one by one until the artifact is found. Just add both the central mirror and internal repository to the settings.xml and it will be okay.

Read Maven guide to setup multiple repositories. In respect to the order of repositories, see this answer.

Upvotes: 5

Related Questions