多个存储库的Maven设置

Roh*_*han 19 java build repository nexus maven-3

我在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>
Run Code Online (Sandbox Code Playgroud)

在pom.xml中我有两个罐子

  1. apache-commons.jar(我假设从中心下载)
  2. licensed.jar(我假设从付费罐子下载)

但是当我运行maven clean install它时,它试图从Org-central下载licensed.jar.

如何让它使用付费罐子下载?是否有可能首先进入Org-central,如果失败,它会尝试付费罐子?如果是这样,怎么样?我不想把repo条目放在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>
Run Code Online (Sandbox Code Playgroud)

小智 14

你必须设置镜像

<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>   
Run Code Online (Sandbox Code Playgroud)

然后添加内部和外部回购

<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>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢 Ashkrit,它奏效了。使用您的解决方案,我还添加了一行来激活该配置文件。 (2认同)
  • 但我仍然想知道为什么在 maven 中我们需要两次指定相同的 URL。一些Ids关系应该就足够了。最后,它只是用于尝试 jars 的 URL 列表。 (2认同)

jon*_*ckt 10

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

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

我们找到了另一种基于多次激活的 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>
Run Code Online (Sandbox Code Playgroud)

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

如果你想快速检查,如果你的配置正在工作,你可以简单地使用maven-dependency-plugin's dependency:get

mvn dependency:get \
    -DgroupId=your.package.name \
    -DartifactId=yourArtifactId \
    -Dversion=YOURVERSION \
    -Dpackaging=jar
Run Code Online (Sandbox Code Playgroud)

如果这导致 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)
Run Code Online (Sandbox Code Playgroud)


Arp*_*wal 8

settings.xml,mirror使用id和为我工作url的存储库定义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>    
Run Code Online (Sandbox Code Playgroud)


ale*_*ung 5

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

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