简体   繁体   English

Maven 多个存储库的设置

[英]Maven Settings for multiple repositories

I have the following in settings.xml我在 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在 pom.xml 我有两个 jars

  1. apache-commons.jar (which I assumes to be downloaded from central) apache-commons.jar(我假设是从中央下载的)
  2. licensed.jar (which I assume to be downloaded from paid-jars) licensed.jar(我假设是从付费罐子下载的)

But when I run maven clean install it tries to download licensed.jar from Org-central.但是当我运行maven clean install时,它会尝试从 Org-central 下载 licensed.jar。

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?是否有可能首先进入 Org-central,如果失败,它会尝试付费罐子? If so, how?如果是这样,如何? I don't want to put repo entries in pom.xml我不想将 repo 条目放入 pom.xml


Settings.xml设置.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>

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>

In settings.xml , defining mirror with id and url for the repository besides using the same in profile worked for me, as below:settings.xml ,除了在profile使用相同的内容之外,还为存储库定义了带有idurl mirror ,如下所示:

<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>    

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 ).如果您需要区分内部(Nexus、Artifactory、..)存储库和 Maven 中央存储库,这里已经讨论的基于使用<mirrorOf>标签功能的多个镜像的解决方案(如官方文档中所述也可以正常工作(与相同, 因此问答)。

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.但是在我们的案例中,我们希望从内部 Nexus 1 下载一些库 - 以及从内部 Nexus 2 下载其他库(具有相同的包名称,但版本不同),这些解决方案不起作用。 We simply couldn't use the <mirrorOf> tag.我们根本无法使用<mirrorOf>标签。

We found another solution based on multiple <repository> definitions INSIDE of an ever activated Maven profile (it didn't work without the profile definition!!).我们发现基于多个另一个解决方案<repository>不断激活Maven的配置文件(它没有没有配置文件定义的工作!)的定义INSIDE。 Here's our settings.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">

    <!-- 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 .如果您正在寻找完整的settings.xml ,其中 - 除了多个存储库 - 还定义了一个公司代理以及maven-releasesmaven-snapshots用户推送到公司 Nexus 1 的凭据,有一个看看这个要点

And if you want to check fast, if you're configuration is working, you could simply use maven-dependency-plugin's dependency:get :如果你想快速检查,如果你的配置正在运行,你可以简单地使用maven-dependency-plugin 的依赖项: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!):如果这导致BUILD SUCCESS ,其中至少可以下载 Nexus 1 的一个依赖项和 Nexus 2 的一个依赖项,一切都应该按预期工作,并且看起来像这样(省略了很多包!):

[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)

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. Maven 将一一查找所有配置的存储库,直到找到工件。 Just add both the central mirror and internal repository to the settings.xml and it will be okay.只需将中央镜像和内部存储库添加到settings.xml就可以了。

Read Maven guide to setup multiple repositories .阅读Maven 指南以设置多个存储库 In respect to the order of repositories, see this answer .关于存储库的顺序,请参阅此答案

In the settings.xml, you need to make 3 changes在settings.xml中,您需要进行3处更改

(Replace https://repository.internal/mvn-public/ with your org internal repo url) (将https://repository.internal/mvn-public/替换为您的组织内部 repo url)

A) Add the mirrors as below A) 添加镜像如下

<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 B)更新配置文件部分如下

 <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 C) 如下设置活动配置文件

 <activeProfiles>
    <activeProfile>artifact.repository.int</activeProfile>
    <activeProfile>artifact.repository.web</activeProfile>
</activeProfiles>
  • Define (duplicate) authentication (server) entries in settings.xml,在 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.在主存储库的mirrorOf中使用特殊的“catch-the-rest”定义各自的镜像,并在其他mirrorOf条目中引用“存储库”ID。
  <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).使用“示例”URL 定义存储库(由于使用上述镜像,URL 将被忽略;在存储库 ID 中重复使用服务器 ID 并将真实 URL 直接放置在存储库条目中将防止通过镜像定义使用间接访问)。
  <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM