![](/img/trans.png)
[英]Deploying Maven artifact to multiple repositories with different settings
[英]Maven Settings for multiple repositories
我在 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>
在 pom.xml 我有两个 jars
但是当我运行maven clean install
时,它会尝试从 Org-central 下载 licensed.jar。
我怎样才能让它使用付费罐子下载? 是否有可能首先进入 Org-central,如果失败,它会尝试付费罐子? 如果是这样,如何? 我不想将 repo 条目放入 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>
你必须设置镜像
<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>
在settings.xml
,除了在profile
使用相同的内容之外,还为存储库定义了带有id
和url
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>
如果您需要区分内部(Nexus、Artifactory、..)存储库和 Maven 中央存储库,这里已经讨论的基于使用<mirrorOf>
标签功能的多个镜像的解决方案(如官方文档中所述)也可以正常工作(与此相同, 因此问答)。
但是在我们的案例中,我们希望从内部 Nexus 1 下载一些库 - 以及从内部 Nexus 2 下载其他库(具有相同的包名称,但版本不同),这些解决方案不起作用。 我们根本无法使用<mirrorOf>
标签。
我们发现基于多个另一个解决方案<repository>
不断激活Maven的配置文件(它没有没有配置文件定义的工作!)的定义INSIDE。 这是我们的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>
如果您正在寻找完整的settings.xml
,其中 - 除了多个存储库 - 还定义了一个公司代理以及maven-releases
和maven-snapshots
用户推送到公司 Nexus 1 的凭据,有一个看看这个要点。
如果你想快速检查,如果你的配置正在运行,你可以简单地使用maven-dependency-plugin 的依赖项:get :
mvn dependency:get \
-DgroupId=your.package.name \
-DartifactId=yourArtifactId \
-Dversion=YOURVERSION \
-Dpackaging=jar
如果这导致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)
指定专用存储库来查找工件是不可能的。 Maven 将一一查找所有配置的存储库,直到找到工件。 只需将中央镜像和内部存储库添加到settings.xml
就可以了。
阅读Maven 指南以设置多个存储库。 关于存储库的顺序,请参阅此答案。
在settings.xml中,您需要进行3处更改
(将https://repository.internal/mvn-public/替换为您的组织内部 repo url)
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)更新配置文件部分如下
<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) 如下设置活动配置文件
<activeProfiles>
<activeProfile>artifact.repository.int</activeProfile>
<activeProfile>artifact.repository.web</activeProfile>
</activeProfiles>
<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>
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>
<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.