Maven Settings for multiple repositories

前端 未结 4 1921
無奈伤痛
無奈伤痛 2020-12-13 04:37

I have the following in settings.xml


       
          paid-jars         


        
相关标签:
4条回答
  • 2020-12-13 04:58

    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>    
    
    0 讨论(0)
  • 2020-12-13 05:08

    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)
    
    0 讨论(0)
  • 2020-12-13 05:11

    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.

    0 讨论(0)
  • 2020-12-13 05:12

    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>
    
    0 讨论(0)
提交回复
热议问题