国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

淺談Maven 項目中依賴的搜索順序

2024-07-14 08:42:25
字體:
供稿:網(wǎng)友

網(wǎng)上有很多關(guān)于maven項目中mirror、profile、repository的搜索順序的文章,說法不一。官方文檔并沒有找到相關(guān)的說明,鑒于此,我抽時間做了一個驗證。

依賴倉庫的配置方式

maven項目使用的倉庫一共有如下幾種方式:

  1. 中央倉庫,這是默認的倉庫
  2. 鏡像倉庫,通過 sttings.xml 中的 settings.mirrors.mirror 配置
  3. 全局profile倉庫,通過 settings.xml 中的 settings.repositories.repository 配置
  4. 項目倉庫,通過 pom.xml 中的 project.repositories.repository 配置
  5. 項目profile倉庫,通過 pom.xml 中的 project.profiles.profile.repositories.repository 配置
  6. 本地倉庫

如果所有配置都存在,依賴的搜索順序就會變得異常復(fù)雜。

分析依賴搜索順序

先從最簡單開始,慢慢增加配置,查看有什么變化。

準備測試環(huán)境

安裝jdk、maven。

使用如下命令創(chuàng)建測試項目:

 

復(fù)制代碼代碼如下:
yes | mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp  -DinteractiveMode=true -DgroupId=com.pollyduan -DartifactId=myweb -Dversion=1.0 -Dpackage=com.pollyduan

 

創(chuàng)建完成后,為了避免后續(xù)測試干擾,先執(zhí)行一次compile。

cd mywebmvn 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.12mvn compile

在日志中查看下載依賴的倉庫:

 

復(fù)制代碼代碼如下:
Downloaded from settings_mirror: https://maven.aliyun.com/repository/public/junit/junit/4.12/junit-4.12.pom (24 kB at 35 kB/s)

 

可以看出,是從 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.12mvn compile

在日志中查看下載依賴的倉庫:

 

復(fù)制代碼代碼如下:
Downloaded from pom_repositories: http://10.18.29.128/nexus/content/groups/public/junit/junit/4.12/junit-4.12.pom (24 kB at 95 kB/s)

 

從顯示的倉庫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.12mvn compile -Ps_profile

在日志中查看下載依賴的倉庫:

 

復(fù)制代碼代碼如下:
Downloaded from settings_profile_repo: http://mirrors.163.com/maven/repository/maven-public/junit/junit/4.12/junit-4.12.pom (24 kB at 63 kB/s)

 

從顯示的倉庫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.12mvn compile -Ps_profile,p_profilemvn compile -Pp_profile,s_profile

在日志中查看下載依賴的倉庫:

 

復(fù)制代碼代碼如下:
Downloaded from settings_profile_repo: http://mirrors.163.com/maven/repository/maven-public/junit/junit/4.12/junit-4.12.pom (24 kB at 68 kB/s)

 

從顯示的倉庫id可以看出:

  • jar 是從 settings_profile_repo 中下載的
  • settings_profile_repo 優(yōu)先級高于 pom_profile_repo

進一步測試:

rm -rf ~/.m2/repository/junit/junit/4.12mvn compile -Pp_profile

在日志中查看下載依賴的倉庫:

 

復(fù)制代碼代碼如下:
Downloaded from pom_profile_repo: http://10.18.29.128/nexus/content/groups/public/junit/junit/4.12/junit-4.12.pom (24 kB at 106 kB/s)

 

從顯示的倉庫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í)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 偏关县| 资源县| 双江| 鹤岗市| 会泽县| 遂溪县| 郯城县| 达日县| 涞水县| 精河县| 桑植县| 项城市| 伊金霍洛旗| 梅河口市| 石阡县| 旬阳县| 博罗县| 天峨县| 永泰县| 孝义市| 乌恰县| 姜堰市| 乌拉特后旗| 普陀区| 成都市| 长子县| 阿图什市| 鄢陵县| 卢湾区| 怀远县| 上林县| 镇宁| 元朗区| 安达市| 类乌齐县| 兴仁县| 荣成市| 襄樊市| 揭西县| 六枝特区| 梅河口市|