![](/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.