37

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>
kuporific
  • 10,053
  • 3
  • 42
  • 46
Rohit Sachan
  • 1,178
  • 1
  • 8
  • 16
  • Have you configured the internal repository in settings.xml or pom.xml? Can you paste it here? – aleung Aug 22 '14 at 04:04
  • I would recommend to use a repository manager like Nexus for such things instead hard coding that in the settings.xml... – khmarbaise Aug 23 '14 at 14:03
  • @khmarbaise agreed I am already using nexus and created a proxy and hosted two repos one for central mirror second one for internal licensed jars. anyways below ans helped. – Rohit Sachan Aug 23 '14 at 15:12

6 Answers6

21

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>
kuporific
  • 10,053
  • 3
  • 42
  • 46
Ashkrit Sharma
  • 627
  • 5
  • 7
  • 3
    Thanks Ashkrit, it worked. with your solution I added a line to activate that profile as well. – Rohit Sachan Aug 22 '14 at 09:38
  • 3
    But still I wonder why in maven we need to specify that same URL twice. Some Ids relationship should be enough. At the end its just list of URL to try for jars. – Rohit Sachan Aug 22 '14 at 09:40
  • thanks it did the trick; and also don't forget that for plugins there is a separate `pluginRepositories` tag. – D. Kovács Jan 26 '17 at 13:44
  • 1
    @RohitSachan I think you don't need to specify the real URL in the repository element if you have a mirror in place for it. So the URL element could contain ht tp://google for example and it will be redirected to the correct URL via the mirror. – Adriaan Koster Feb 17 '17 at 09:11
  • 1
    Why do you need both the repository and the mirror instead of just setting the repository to be the correct url? – Jontia Jan 08 '21 at 16:36
  • this answer is almost correct. What you need to add is a) activeprofiles statement for the profile id you have and 2) remove mirror configuration for google. that's not a mirror of anything. Also, 3) internal repository mirror should be named "central" so requests reaching for central explicitly would go to mirror. – eis Feb 09 '21 at 13:03
15

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)
jonashackt
  • 12,022
  • 5
  • 67
  • 124
  • (need for profile definition is also [mentioned in the manual](https://maven.apache.org/guides/mini/guide-multiple-repositories.html)) – eis Feb 09 '21 at 13:06
12

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>    
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
  • 1
    According to https://maven.apache.org/guides/mini/guide-mirror-settings.html : *To save us bandwidth and you time, mirroring the entire central repository is not allowed. (Doing so will get you automatically banned)* – Babyburger Apr 11 '21 at 16:27
5

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.

Community
  • 1
  • 1
aleung
  • 9,848
  • 3
  • 55
  • 69
1

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>
0
  • 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>
eel ghEEz
  • 1,186
  • 11
  • 24