maven多個倉庫查詢的優(yōu)先級順序案例講解
上一篇我們詳解了setttings.xml的配置項,里面的配置項基本都和倉庫有關系,我們使用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.xml
2. User
settings.xml
2. local effective build POM:
1. Local
pom.xml
2. 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.xml
file.
Take note that profiles in the
settings.xml
takes higher priority than profiles in the POM.
簡單翻譯一下,就是:
- • 全局配置文件settings.xml中的配置項的優(yōu)先級最高,也就是maven安裝目錄下的conf/settings.xml優(yōu)先級最高
- • 其次是用戶級別的配置文件優(yōu)先級次高,默認是${user.home}/.m2/settings.xml
- • 最后就是本地的pom.xml文件優(yōu)先級次次高
- • 當確定了要查詢某個倉庫時,會先看這個倉庫有沒有對應的鏡像倉庫,如果有的話,則轉向去查鏡像倉庫,也就是會查當前倉庫的替代品(鏡像倉庫),跳過對本倉庫的檢索
- • 如果同一個pom文件里面有多個激活的profile,則靠后面激活的profile的優(yōu)先級高
- • 針對pom文件,如果有激活的profile,且profile里面配置了repositories,則profile里面的repositories的倉庫優(yōu)先級比標簽下面的repositories的優(yōu)先級高
- • pom文件中無論是project標簽下面直接定義的repositories,還是profile標簽下面定義的repositories,repositories內部的repository的查詢順序,都是按照倉庫定義的順序查詢,也就是自上而下查詢。
- • 如果settings.xml中的profile的id和pom文件中的profile的id一樣,則以settings.xml中的profile中配置的值為準
- • 如果同一個pom文件中有多個profile被激活,那么處于profiles內部靠后面生效的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文件,我們針對這兩個文件的結合來分析倉庫的使用順序。
假如我們有如下的全局配置文件: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文件默認激活了profile-1和profile-2,settings中默認激活了env-dev。按照在同一文件的profile的生效順序規(guī)則,pom文件中的倉庫使用順序為
dev5->dev3->dev1->dev2->dev4->central(超級pom中定義的中央倉庫),
而由于在setttings.xml中為dev1和central配置了鏡像倉庫,所以最終倉庫的優(yōu)先查詢順序為:
dev5->dev3->dev-mirror->dev2->dev4->nexus-aliyun
2.2、settings.xml沒有配置激活的profile,pom中配置了激活的profile
這種情況下,settings中沒有設置activeProfiles,我們只需要考慮pom文件中倉庫的查詢順序,按照先前說的規(guī)則:
- • 如果同一個pom文件里面有多個激活的profile,則靠后面激活的profile的優(yōu)先級高
- • 針對pom文件,如果有激活的profile,且profile里面配置了repositories,則profile里面的repositories的倉庫優(yōu)先級比標簽下面的repositories的優(yōu)先級高
- • pom文件中無論是project標簽下面直接定義的repositories,還是profile標簽下面定義的repositories,repositories內部的repository的查詢順序,都是按照倉庫定義的順序查詢,也就是自上而下查詢。
則倉庫使用順序為
dev3->dev1->dev2->dev4->central(超級pom中定義的中央倉庫),
而由于在setttings.xml中為dev1和central配置了鏡像倉庫,所以最終倉庫的優(yōu)先查詢順序為:
dev3->dev-mirror->dev2->dev4->nexus-aliyun
3、倉庫配置建議
maven官方不建議在settings中配置profile,因為profile中配置的一些屬性或者倉庫基本都是為項目服務的,我們的項目可以通過代碼倉庫(比如gitlab)進行共享,但是settings配置文件一般很難共享。如果我們的項目依賴了自己本地的settings文件中的一些配置信息,但是其他同事本地的settings文件又沒這些信息,那么其他同事就無法正常的運行項目。而且profile中定義的信息一般都和項目運行的環(huán)境有關,比如有開發(fā)環(huán)境的配置,測試環(huán)境的配置,還有生產環(huán)境的配置。既然和項目有直接的緊密關系,就應該將其配置到項目里面。
3.1 針對倉庫配置的建議
- • 如果倉庫和環(huán)境無關,可以將倉庫配置到pom文件的結點下面
- • 如果不同環(huán)境使用不同的倉庫,則可以通過在pom文件中定義profile,并在profile結點下面配置倉庫
3.2、針對settings文件的配置
seetings文件建議用來配置下列幾項
- • 配置本地倉庫路徑,即配置localRepository
- • 配置中央倉庫的鏡像,即配置mirrors
- • 配置訪問倉庫的認證信息,即配置servers
到此這篇關于maven多個倉庫查詢的優(yōu)先級順序的文章就介紹到這了,更多相關maven多個倉庫內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解spring boot實現(xiàn)websocket
這篇文章主要介紹了詳解spring boot實現(xiàn)websocket,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06Java基礎知識之ByteArrayOutputStream流的使用
這篇文章主要介紹了Java基礎知識之ByteArrayOutputStream流的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12使用spring mail發(fā)送html郵件的示例代碼
本篇文章主要介紹了使用spring mail發(fā)送html郵件的示例代碼,這里整理了詳細的示例代碼,具有一定的參考價值,有興趣的可以了解一下2017-09-09Retrofit+Rxjava實現(xiàn)文件上傳和下載功能
這篇文章主要介紹了Retrofit+Rxjava實現(xiàn)文件上傳和下載功能,文中提到了單文件上傳和多文件上傳及相關參數(shù)的請求,需要的朋友參考下吧2017-11-11springboot如何為web層添加統(tǒng)一請求前綴
這篇文章主要介紹了springboot如何為web層添加統(tǒng)一請求前綴,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02