Java 多个存储库的 Maven 设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25424338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:21:58  来源:igfitidea点击:

Maven Settings for multiple repositories

javabuildrepositorymaven-3nexus

提问by Rohit Sachan

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 我有两个罐子

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

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

但是当我运行maven clean install它时,它会尝试从 Org-central 下载 license.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? If so, how? I don't want to put repo entries in pom.xml

我怎样才能让它使用付费罐子下载?是否有可能首先它转到 Org-central,如果失败,它会尝试付费的 jars?如果是这样,如何?我不想在 pom.xml 中放置 repo 条目



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>

回答by aleung

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.xmland it will be okay.

指定专用存储库来查找工件是不可能的。Maven 将一一查找所有配置的存储库,直到找到工件。只需将中央镜像和内部存储库添加到settings.xml,就可以了。

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

阅读Maven 指南以设置多个存储库。关于存储库的顺序,请参阅此答案

回答by Ashkrit Sharma

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>

回答by Arpit Aggarwal

In settings.xml, defining mirrorwith idand urlfor the repository besides using the same in profileworked for me, as below:

settings.xml,定义mirroridurl用于除了使用在同一仓库profile为我工作,如下图所示:

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

回答by jonashackt

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>标签功能的多个镜像的解决方案(如官方文档中所述)工作正常(与相同)所以问答)。

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

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

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

我们找到了另一种基于多次激活的 Maven 配置文件内部<repository>定义的解决方案(如果没有配置文件定义,它就无法工作!!)。这是我们的: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-releasesand maven-snapshotsusers to push to the corporate Nexus 1, have a look at this gist.

如果您正在寻找一个成熟的 . settings.xml,其中 - 除了多个存储库 - 还定义了一个公司代理以及用于推送到公司 Nexus 1的凭据maven-releasesmaven-snapshots用户,请查看这个 gist

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!):

如果这导致 a 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)