多个存储库的 Maven 设置

2022-09-01 05:19:02

我在设置中有以下内容.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>

在pom.xml我有两个罐子

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

但是当我运行时,它会尝试从Org-central下载许可.jar。maven clean install

我怎样才能让它使用付费罐子来下载?有没有可能首先进入组织中心,如果失败,它会尝试付费jars?如果是这样,如何?我不想把回购条目放在pom中.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>

答案 1

您必须设置镜像

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

然后添加内部和外部存储库

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

答案 2

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

如果您正在寻找一个完整的,其中 - 除了多个存储库 - 还定义了一个企业代理以及要推送到企业Nexus 1的凭据和用户,请查看此要点settings.xmlmaven-releasesmaven-snapshots

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

mvn dependency:get \
    -DgroupId=your.package.name \
    -DartifactId=yourArtifactId \
    -Dversion=YOURVERSION \
    -Dpackaging=jar

如果这导致一个 ,其中至少可以下载来自Nexus 1和Nexus 2之一的依赖项,则一切都应按预期工作,并且看起来有点像这样(省略了很多软件包!BUILD SUCCESS

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

推荐