淺談Maven 項目中依賴的搜索順序
網(wǎng)上有很多關(guān)于maven項目中mirror、profile、repository的搜索順序的文章,說法不一。官方文檔并沒有找到相關(guān)的說明,鑒于此,我抽時間做了一個驗證。
依賴倉庫的配置方式
maven項目使用的倉庫一共有如下幾種方式:
- 中央倉庫,這是默認的倉庫
- 鏡像倉庫,通過 sttings.xml 中的 settings.mirrors.mirror 配置
- 全局profile倉庫,通過 settings.xml 中的 settings.repositories.repository 配置
- 項目倉庫,通過 pom.xml 中的 project.repositories.repository 配置
- 項目profile倉庫,通過 pom.xml 中的 project.profiles.profile.repositories.repository 配置
- 本地倉庫
如果所有配置都存在,依賴的搜索順序就會變得異常復(fù)雜。
分析依賴搜索順序
先從最簡單開始,慢慢增加配置,查看有什么變化。
準備測試環(huán)境
安裝jdk、maven。
使用如下命令創(chuàng)建測試項目:
創(chuàng)建完成后,為了避免后續(xù)測試干擾,先執(zhí)行一次compile。
cd myweb mvn compile
最后,修改 pom.xml 文件,將 junit版本號改為 4.12 。我們要使用這個jar來測試依賴的搜索順序。
默認情況
首先確保junit4.12不存在:
rm -rf ~/.m2/repository/junit/junit/4.12
默認情況下沒有配置任何倉庫,也就是說,既沒改 $M2_HOME/conf/settings.xml 也沒有添加 ~/.m2/settings.xml
執(zhí)行編譯,查看日志中拉取junit的倉庫。
mvn compile ... Downloaded from central: https://repo.maven.apache.org/maven2/junit/junit/4.12/junit-4.12.pom (24 kB at 11 kB/s)
可以看出,默認是從 central 中央倉庫拉取的jar.
配置鏡像倉庫 settings_mirror
創(chuàng)建 ~/.m2/setttings.xml ,內(nèi)容如下:
<settings> <mirrors> <mirror> <id>settings_mirror</id> <url>https://maven.aliyun.com/repository/public</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> </settings>
重新測試:
rm -rf ~/.m2/repository/junit/junit/4.12 mvn compile
在日志中查看下載依賴的倉庫:
可以看出,是從 settings_mirror 中下載的jar
結(jié)論:settings_mirror 的優(yōu)先級高于 central
配置pom中的倉庫 pom_repositories
在 project 中增加如下配置:
<repositories> <repository> <id>pom_repositories</id> <name>local</name> <url>http://10.18.29.128/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories>
由于我們改變了id的名字,所以倉庫地址無所謂,使用相同的地址也不影響測試。
執(zhí)行測試:
rm -rf ~/.m2/repository/junit/junit/4.12 mvn compile
在日志中查看下載依賴的倉庫:
從顯示的倉庫id可以看出:
- jar 是從 pom_repositories 中下載的。
- pom_repositories 優(yōu)先級高于 settings_mirror
配置全局profile倉庫 settings_profile_repo
在 ~/.m2/settings.xml 中 settings 的節(jié)點內(nèi)增加:
<profiles>
<profile>
<id>s_profile</id>
<repositories>
<repository>
<id>settings_profile_repo</id>
<name>netease</name>
<url>http://mirrors.163.com/maven/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
執(zhí)行測試:
rm -rf ~/.m2/repository/junit/junit/4.12 mvn compile -Ps_profile
在日志中查看下載依賴的倉庫:
從顯示的倉庫id可以看出:
- jar 是從 settings_profile_repo 中下載的。
- settings_profile_repo 優(yōu)先級高于 settings_mirror。
- settings_profile_repo 優(yōu)先級高于 pom_repositories 。
配置項目profile倉庫 pom_profile_repo
<profiles>
<profile>
<id>p_profile</id>
<repositories>
<repository>
<id>pom_profile_repo</id>
<name>local</name>
<url>http://10.18.29.128/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
執(zhí)行測試:
rm -rf ~/.m2/repository/junit/junit/4.12 mvn compile -Ps_profile,p_profile mvn compile -Pp_profile,s_profile
在日志中查看下載依賴的倉庫:
從顯示的倉庫id可以看出:
- jar 是從 settings_profile_repo 中下載的
- settings_profile_repo 優(yōu)先級高于 pom_profile_repo
進一步測試:
rm -rf ~/.m2/repository/junit/junit/4.12 mvn compile -Pp_profile
在日志中查看下載依賴的倉庫:
從顯示的倉庫id可以看出:
- jar 是從 settings_profile_repo 中下載的
- pom_profile_repo 優(yōu)先級高于 pom_repositories
最后確認 local_repo 本地倉庫 ~/.m2/repository
這不算測試了,只是一個結(jié)論,可以任意測試。
只要 ~/.m2/repository 中包含依賴,無論怎么配置,都會優(yōu)先使用local本地倉庫中的jar.
最終結(jié)論
- settings_mirror 的優(yōu)先級高于 central
- settings_profile_repo 優(yōu)先級高于 settings_mirror
- settings_profile_repo 優(yōu)先級高于 pom_repositories
- settings_profile_repo 優(yōu)先級高于 pom_profile_repo
- pom_profile_repo 優(yōu)先級高于 pom_repositories
- pom_repositories 優(yōu)先級高于 settings_mirror
通過上面的比較得出完整的搜索鏈:
local_repo > settings_profile_repo > pom_profile_repo > pom_repositories > settings_mirror > central
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud環(huán)境搭建過程之Rest使用小結(jié)
這篇文章主要介紹了SpringCloud環(huán)境搭建之Rest使用,本文通過實例代碼圖文相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
Java開發(fā)環(huán)境配置及Vscode搭建過程
今天通過圖文并茂的形式給大家介紹Java開發(fā)環(huán)境配置及Vscode搭建過程,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-07-07
用java實現(xiàn)在txt文本中寫數(shù)據(jù)和讀數(shù)據(jù)的方法
今天小編就為大家分享一篇用java實現(xiàn)在txt文本中寫數(shù)據(jù)和讀數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
關(guān)于thymeleaf判斷對象是否為空的相關(guān)邏輯處理
這篇文章主要介紹了關(guān)于thymeleaf判斷對象是否為空的相關(guān)邏輯處理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
mybatis中mapper.xml文件的常用屬性及標簽講解
這篇文章主要介紹了mybatis中mapper.xml文件的常用屬性及標簽講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
netty-grpc一次DirectByteBuffer內(nèi)存泄露問題
這篇文章主要介紹了netty-grpc一次DirectByteBuffer內(nèi)存泄露問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

