maven多個倉庫查詢的優(yōu)先級順序案例講解
上一篇我們詳解了setttings.xml的配置項(xiàng),里面的配置項(xiàng)基本都和倉庫有關(guān)系,我們使用maven更多的也是要從倉庫下載jar包,然后也把我們自己公共的jar包上傳到倉庫。由于我們是可以配置多個倉庫的,這時候就涉及到了一個問題:下載一個jar包時,怎么確定這些倉庫的使用順序?
1、官網(wǎng)的解釋
maven官網(wǎng)對這個問題給了一定的解答,如下:
Remote repository URLs are queried in the following order for artifacts until one returns a valid result:
1. effective settings:
1. Global
settings.xml2. User
settings.xml2. local effective build POM:
1. Local
pom.xml2. Parent POMs, recursively
3. Super POM
3. effective POMs from dependency path to the artifact.
For each of these locations, the repositories within the profiles are queried first in the order outlined at Introduction to build profiles.
Before downloading from a repository, mirrors configuration is applied.
All profile elements in a POM from active profiles overwrite the global elements with the same name of the POM or extend those in case of collections. In case multiple profiles are active in the same POM or external file, the ones which are defined later take precedence over the ones defined earlier (independent of their profile id and activation order).
If a profile is active from
settings, its values will override any equivalently ID'd profiles in a POM orprofiles.xmlfile.
Take note that profiles in the
settings.xmltakes higher priority than profiles in the POM.
簡單翻譯一下,就是:
- • 全局配置文件settings.xml中的配置項(xiàng)的優(yōu)先級最高,也就是maven安裝目錄下的conf/settings.xml優(yōu)先級最高
- • 其次是用戶級別的配置文件優(yōu)先級次高,默認(rèn)是${user.home}/.m2/settings.xml
- • 最后就是本地的pom.xml文件優(yōu)先級次次高
- • 當(dāng)確定了要查詢某個倉庫時,會先看這個倉庫有沒有對應(yīng)的鏡像倉庫,如果有的話,則轉(zhuǎn)向去查鏡像倉庫,也就是會查當(dāng)前倉庫的替代品(鏡像倉庫),跳過對本倉庫的檢索
- • 如果同一個pom文件里面有多個激活的profile,則靠后面激活的profile的優(yōu)先級高
- • 針對pom文件,如果有激活的profile,且profile里面配置了repositories,則profile里面的repositories的倉庫優(yōu)先級比標(biāo)簽下面的repositories的優(yōu)先級高
- • pom文件中無論是project標(biāo)簽下面直接定義的repositories,還是profile標(biāo)簽下面定義的repositories,repositories內(nèi)部的repository的查詢順序,都是按照倉庫定義的順序查詢,也就是自上而下查詢。
- • 如果settings.xml中的profile的id和pom文件中的profile的id一樣,則以settings.xml中的profile中配置的值為準(zhǔn)
- • 如果同一個pom文件中有多個profile被激活,那么處于profiles內(nèi)部靠后面生效的profile優(yōu)先級比profiles中靠前的profile的優(yōu)先級高
也就是整體的優(yōu)先級方面:
conf/settings.xml > ${user.home}/.m2/settings.xml >本地的pom.xml文件
2、案例講解
考慮到我們常用的配置文件是conf/settings.xml和工程里面的pom.xml文件,我們針對這兩個文件的結(jié)合來分析倉庫的使用順序。
假如我們有如下的全局配置文件:settings.xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd">
<localRepository>D:/programs/.m2/repository</localRepository>
<servers>
<server>
<id>dev</id>
<username>repouser</username>
<password>repopwd</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>dev-mirror</id>
<mirrorOf>dev1</mirrorOf>
<name>第二套開發(fā)倉庫</name>
<url>http://192.168.1.2/repository/devM</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>env-dev</id>
<repositories>
<repository>
<id>dev5</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://192.168.1.1/repository/dev5</url>
</repository>
</repositories>
</profile>
<profile>
<id>env-test</id>
<repositories>
<repository>
<id>test</id>
<name>test</name>
<url>http://192.168.1.1/repository/test</url>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>env-dev</activeProfile>
</activeProfiles>
</settings>工程的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>test</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<revision>1.0.0</revision>
</properties>
<repositories>
<repository>
<id>dev4</id>
<name>dev4</name>
<url>http://192.168.1.1/repository/dev4</url>
</repository>
</repositories>
<profiles>
<profile>
<id>profile-1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>dev1</id>
<name>dev1</name>
<url>http://192.168.1.1/repository/dev1</url>
</repository>
<repository>
<id>dev2</id>
<name>dev2</name> <url>http://192.168.1.1/repository/dev2</url>
</repository>
</repositories>
</profile>
<profile>
<id>profile-2</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>dev3</id>
<name>dev3</name>
<url>http://192.168.1.1/repository/dev3</url>
</repository>
</repositories>
</profile>
</profiles>
</project>2.1、settings.xml和pom都配置激活了各自的profile
pom.xml文件默認(rèn)激活了profile-1和profile-2,settings中默認(rèn)激活了env-dev。按照在同一文件的profile的生效順序規(guī)則,pom文件中的倉庫使用順序?yàn)?/p>
dev5->dev3->dev1->dev2->dev4->central(超級pom中定義的中央倉庫),
而由于在setttings.xml中為dev1和central配置了鏡像倉庫,所以最終倉庫的優(yōu)先查詢順序?yàn)椋?/p>
dev5->dev3->dev-mirror->dev2->dev4->nexus-aliyun
2.2、settings.xml沒有配置激活的profile,pom中配置了激活的profile
這種情況下,settings中沒有設(shè)置activeProfiles,我們只需要考慮pom文件中倉庫的查詢順序,按照先前說的規(guī)則:
- • 如果同一個pom文件里面有多個激活的profile,則靠后面激活的profile的優(yōu)先級高
- • 針對pom文件,如果有激活的profile,且profile里面配置了repositories,則profile里面的repositories的倉庫優(yōu)先級比標(biāo)簽下面的repositories的優(yōu)先級高
- • pom文件中無論是project標(biāo)簽下面直接定義的repositories,還是profile標(biāo)簽下面定義的repositories,repositories內(nèi)部的repository的查詢順序,都是按照倉庫定義的順序查詢,也就是自上而下查詢。
則倉庫使用順序?yàn)?/p>
dev3->dev1->dev2->dev4->central(超級pom中定義的中央倉庫),
而由于在setttings.xml中為dev1和central配置了鏡像倉庫,所以最終倉庫的優(yōu)先查詢順序?yàn)椋?/p>
dev3->dev-mirror->dev2->dev4->nexus-aliyun
3、倉庫配置建議
maven官方不建議在settings中配置profile,因?yàn)閜rofile中配置的一些屬性或者倉庫基本都是為項(xiàng)目服務(wù)的,我們的項(xiàng)目可以通過代碼倉庫(比如gitlab)進(jìn)行共享,但是settings配置文件一般很難共享。如果我們的項(xiàng)目依賴了自己本地的settings文件中的一些配置信息,但是其他同事本地的settings文件又沒這些信息,那么其他同事就無法正常的運(yùn)行項(xiàng)目。而且profile中定義的信息一般都和項(xiàng)目運(yùn)行的環(huán)境有關(guān),比如有開發(fā)環(huán)境的配置,測試環(huán)境的配置,還有生產(chǎn)環(huán)境的配置。既然和項(xiàng)目有直接的緊密關(guān)系,就應(yīng)該將其配置到項(xiàng)目里面。
3.1 針對倉庫配置的建議
- • 如果倉庫和環(huán)境無關(guān),可以將倉庫配置到pom文件的結(jié)點(diǎn)下面
- • 如果不同環(huán)境使用不同的倉庫,則可以通過在pom文件中定義profile,并在profile結(jié)點(diǎn)下面配置倉庫
3.2、針對settings文件的配置
seetings文件建議用來配置下列幾項(xiàng)
- • 配置本地倉庫路徑,即配置localRepository
- • 配置中央倉庫的鏡像,即配置mirrors
- • 配置訪問倉庫的認(rèn)證信息,即配置servers
到此這篇關(guān)于maven多個倉庫查詢的優(yōu)先級順序的文章就介紹到這了,更多相關(guān)maven多個倉庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于java file 文件操作operate file of java的應(yīng)用
本篇文章介紹了,基于java file 文件操作operate file of java的應(yīng)用。需要的朋友參考下2013-05-05
java中int轉(zhuǎn)string與string轉(zhuǎn)int的效率對比
這篇文章主要介紹了java中int轉(zhuǎn)string與string轉(zhuǎn)int的效率對比,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Java中從Integer到Date的轉(zhuǎn)換方法
這篇文章主要介紹了Java中integer怎么轉(zhuǎn)換date,在Java中,如果我們有一個Integer類型的數(shù)據(jù),想要將其轉(zhuǎn)換為Date類型,本文給大家介紹了實(shí)現(xiàn)方法,并通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-05-05
Java面試題沖刺第十三天--數(shù)據(jù)庫(3)
這篇文章主要為大家分享了最有價(jià)值的三道數(shù)據(jù)庫面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下2021-07-07
將JavaWeb項(xiàng)目部署到云服務(wù)器的詳細(xì)步驟
這篇文章主要介紹了將JavaWeb項(xiàng)目部署到云服務(wù)器的詳細(xì)步驟,文章通過圖文結(jié)合的方式給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-12-12
為什么不推薦使用BeanUtils屬性轉(zhuǎn)換工具示例詳解
這篇文章主要介紹了為什么不推薦使用BeanUtils屬性轉(zhuǎn)換工具,本文通過示例代碼給大家詳細(xì)介紹,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
淺談Java中@Autowired和@Inject注解的區(qū)別和使用場景
本文主要介紹了淺談Java中@Autowired和@Inject注解的區(qū)別和使用場景,@Autowired注解在依賴查找方式和注入方式上更加靈活,適用于Spring框架中的依賴注入,而@Inject注解在依賴查找方式上更加嚴(yán)格,適用于Java的依賴注入標(biāo)準(zhǔn),感興趣的可以了解一下2023-11-11
springboot集成nacos讀取nacos配置數(shù)據(jù)的原理
這篇文章主要介紹了springboot集成nacos讀取nacos配置數(shù)據(jù)的原理,文中有詳細(xì)的代碼流程,對大家學(xué)習(xí)springboot集成nacos有一定的幫助,需要的朋友可以參考下2023-05-05

