歡迎您光臨本站 註冊首頁

詳解maven依賴衝突以及解決方法

←手機掃碼閱讀     retouched @ 2020-06-24 , reply:0

什麼是依賴衝突

依賴衝突是指專案依賴的某一個jar包,有多個不同的版本,因而造成類包版本衝突

依賴衝突的原因

依賴衝突很經常是類包之間的間接依賴引起的。每個顯式宣告的類包都會依賴於一些其它的隱式類包,這些隱式的類包會被maven間接引入進來,從而造成類包衝突

如何解決依賴衝突

首先檢視產生依賴衝突的類jar,其次找出我們不想要的依賴類jar,手工將其排除在外就可以了。具體執行步驟如下

1、檢視依賴衝突

a、透過dependency:tree是命令來檢查版本衝突

  mvn -Dverbose dependency:tree

 

當敲入上述命令時,控制檯會出現形如下內容

[INFO] org.example:hello:jar:1.0-SNAPSHOT
 [INFO] +- org.springframework:spring-context:jar:5.2.7.RELEASE:compile
 [INFO] | +- (org.springframework:spring-aop:jar:5.2.7.RELEASE:compile - omitted for conflict with 5.2.0.RELEASE)
 [INFO] | +- org.springframework:spring-beans:jar:5.2.7.RELEASE:compile
 [INFO] | | - (org.springframework:spring-core:jar:5.2.7.RELEASE:compile - omitted for duplicate)
 [INFO] | +- org.springframework:spring-core:jar:5.2.7.RELEASE:compile
 [INFO] | | - org.springframework:spring-jcl:jar:5.2.7.RELEASE:compile
 [INFO] | - org.springframework:spring-expression:jar:5.2.7.RELEASE:compile
 [INFO] | - (org.springframework:spring-core:jar:5.2.7.RELEASE:compile - omitted for duplicate)
 [INFO] - org.springframework:spring-aop:jar:5.2.0.RELEASE:compile
 [INFO] +- (org.springframework:spring-beans:jar:5.2.0.RELEASE:compile - omitted for conflict with 5.2.7.RELEASE)
 [INFO] - (org.springframework:spring-core:jar:5.2.0.RELEASE:compile - omitted for conflict with 5.2.7.RELEASE)

其中omitted for duplicate表示有jar包被重複依賴,最後寫著omitted for conflict with xxx的,說明和別的jar包版本衝突了,而該行的jar包不會被引入。比如上面有一行最後寫著omitted for conflict with 5.2.7.RELEASE,表示spring-core 5.2.0版本不會被專案引用,而spring-core 5.2.7版本會被專案引用

b、如果是idea,可以安裝maven helper外掛來檢查依賴衝突

maven helper外掛安裝成功,點開pom.xml會發現多了一個Dependency Analyzer檢視,如下

上面按鈕的圖示含義如下

  • Conflicts(檢視衝突)

  • All Dependencies as List(列表形式檢視所有依賴)

  • All Dependencies as Tree(樹形式檢視所有依賴)

上圖說明有3個jar存在衝突,點選衝突的jar,可以檢視和哪個jar產生衝突,如下圖

2、解決衝突

專案的pom.xml形如下

   org.springframeworkspring-context5.2.7.RELEASEorg.springframeworkspring-aop5.2.0.RELEASE

 

透過檢視依賴樹,我們知道專案會引用5.2.7.RELEASE的spring core jar包,而不會引用5.2.0的jar包,如果我們想用5.2.0版本的spring core包,我們該如何做?

a、使用第一宣告者優先原則

誰先定義的就用誰的傳遞依賴,即在pom.xml檔案自上而下,先宣告的jar座標,就先引用該jar的傳遞依賴。因此我們如果要使用5.2.0版本的spring core包,我們可以改成如下宣告

   org.springframeworkspring-aop5.2.0.RELEASEorg.springframeworkspring-context5.2.7.RELEASE

 

檢視依賴樹

[INFO] org.example:hello:jar:1.0-SNAPSHOT
 [INFO] +- org.springframework:spring-aop:jar:5.2.0.RELEASE:compile
 [INFO] | +- org.springframework:spring-beans:jar:5.2.0.RELEASE:compile
 [INFO] | | - (org.springframework:spring-core:jar:5.2.0.RELEASE:compile - omitted for duplicate)
 [INFO] | - org.springframework:spring-core:jar:5.2.0.RELEASE:compile
 [INFO] | - org.springframework:spring-jcl:jar:5.2.0.RELEASE:compile
 [INFO] - org.springframework:spring-context:jar:5.2.7.RELEASE:compile
 [INFO] +- (org.springframework:spring-aop:jar:5.2.7.RELEASE:compile - omitted for conflict with 5.2.0.RELEASE)
 [INFO] +- (org.springframework:spring-beans:jar:5.2.7.RELEASE:compile - omitted for conflict with 5.2.0.RELEASE)
 [INFO] +- (org.springframework:spring-core:jar:5.2.7.RELEASE:compile - omitted for conflict with 5.2.0.RELEASE)
 [INFO] - org.springframework:spring-expression:jar:5.2.7.RELEASE:compile
 [INFO] - (org.springframework:spring-core:jar:5.2.7.RELEASE:compile - omitted for conflict with 5.2.0.RELEASE)

透過依賴樹,我們可以看到專案已經引入5.2.0版本的spring core包

b、使用路徑近者優先原則

即直接依賴級別高於傳遞依賴。因此我們可以在最先的pom.xml新增如下內容

   org.springframeworkspring-context5.2.7.RELEASEorg.springframeworkspring-aop5.2.0.RELEASEorg.springframeworkspring-core5.2.0.RELEASE

 

 

透過上圖可以看到專案引入是 spring core 5.2.0的包

c、排除依賴

排除依賴如果是idea,可以使用maven helper外掛進行排除。點開pom.xml,切換到Dependency Analyzer檢視,選擇All Dependencies as Tree,點選要排除的jar,右鍵會出現Execlude選項,如下

它產生的效果和如下配置是一樣

   org.springframeworkspring-context5.2.7.RELEASEspring-coreorg.springframeworkorg.springframeworkspring-aop5.2.0.RELEASE

 

透過上圖可以看到專案引入是 spring core 5.2.0的包

4、版本鎖定

使用dependencyManagement 進行版本鎖定,dependencyManagement可以統一管理專案的版本號,確保應用的各個專案的依賴和版本一致。

如果我們專案中只想使用spring core 5.2.0的包,pom.xml可以改為如下

  org.springframeworkspring-core5.2.0.RELEASEorg.springframeworkspring-context5.2.7.RELEASEorg.springframeworkspring-aop5.2.0.RELEASE

 

透過上圖可以看到專案引入是 spring core 5.2.0的包

總結

綜上就是maven如何排查依賴衝突以及解決方法,對於排查依賴個人比較推薦使用maven helper外掛,至於解決依賴衝突個人推薦使用 版本鎖定 的方法,此外dependencyManagement只是宣告依賴,並不自動實現引入,因此子專案需要顯示的宣告需要用的依賴



[retouched ] 詳解maven依賴衝突以及解決方法已經有284次圍觀

http://coctec.com/docs/java/show-post-239539.html