Maven依賴沖突原因以及解決方法
什么是依賴沖突
依賴沖突是指項(xiàng)目依賴的某一個(gè) jar 包,有多個(gè)不同的版本,因而造成類包版本沖突
依賴沖突的原因
依賴沖突很經(jīng)常是類包之間的間接依賴引起的。每個(gè)顯式聲明的類包都會(huì)依賴于一些其它的隱式類包,這些隱式的類包會(huì)被 maven 間接引入進(jìn)來(lái),從而造成類包沖突
如何解決依賴沖突
首先查看產(chǎn)生依賴沖突的類 jar,其次找出我們不想要的依賴類 jar,手工將其排除在外就可以了。具體執(zhí)行步驟如下
1、查看依賴沖突
a、通過(guò) dependency:tree 是命令來(lái)檢查版本沖突
mvn -Dverbose dependency:tree
當(dāng)敲入上述命令時(shí),控制臺(tái)會(huì)出現(xiàn)形如下內(nèi)容
[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 包被重復(fù)依賴,最后寫著 omitted for conflict with xxx 的,說(shuō)明和別的 jar 包版本沖突了,而該行的 jar 包不會(huì)被引入。比如上面有一行最后寫著 omitted for conflict with 5.2.7.RELEASE,表示 spring-core 5.2.0 版本不會(huì)被項(xiàng)目引用,而 spring-core 5.2.7 版本會(huì)被項(xiàng)目引用
b、如果是 idea,可以安裝 maven helper 插件來(lái)檢查依賴沖突
maven helper 插件安裝成功,點(diǎn)開 pom.xml 會(huì)發(fā)現(xiàn)多了一個(gè) Dependency Analyzer 視圖,如下
上面按鈕的圖標(biāo)含義如下
- Conflicts(查看沖突)
- All Dependencies as List(列表形式查看所有依賴)
- All Dependencies as Tree(樹形式查看所有依賴)
上圖說(shuō)明有 3 個(gè) jar 存在沖突,點(diǎn)擊沖突的 jar,可以查看和哪個(gè) jar 產(chǎn)生沖突,如下圖
2、解決沖突
項(xiàng)目的 pom.xml 形如下
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.0.RELEASE</version> </dependency> </dependencies>
通過(guò)查看依賴樹,我們知道項(xiàng)目會(huì)引用 5.2.7.RELEASE 的 spring core jar 包,而不會(huì)引用 5.2.0 的 jar 包,如果我們想用 5.2.0 版本的 spring core 包,我們?cè)撊绾巫觯?/p>
a、使用第一聲明者優(yōu)先原則
誰(shuí)先定義的就用誰(shuí)的傳遞依賴,即在 pom.xml 文件自上而下,先聲明的 jar 坐標(biāo),就先引用該 jar 的傳遞依賴。因此我們?nèi)绻褂?5.2.0 版本的 spring core 包,我們可以改成如下聲明
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.7.RELEASE</version> </dependency> </dependencies>
查看依賴樹
[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)
通過(guò)依賴樹,我們可以看到項(xiàng)目已經(jīng)引入 5.2.0 版本的 spring core 包
b、使用路徑近者優(yōu)先原則
即直接依賴級(jí)別高于傳遞依賴。因此我們可以在最先的 pom.xml 添加如下內(nèi)容
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.0.RELEASE</version> </dependency> </dependencies>
通過(guò)上圖可以看到項(xiàng)目引入是 spring core 5.2.0 的包
c、排除依賴
排除依賴如果是 idea,可以使用 maven helper 插件進(jìn)行排除。點(diǎn)開 pom.xml,切換到 Dependency Analyzer 視圖,選擇 All Dependencies as Tree,點(diǎn)擊要排除的 jar,右鍵會(huì)出現(xiàn) Execlude 選項(xiàng),如下
它產(chǎn)生的效果和如下配置是一樣
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.7.RELEASE</version> <exclusions> <exclusion> <artifactId>spring-core</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.0.RELEASE</version> </dependency> </dependencies>
通過(guò)上圖可以看到項(xiàng)目引入是 spring core 5.2.0 的包
4、版本鎖定
使用 dependencyManagement 進(jìn)行版本鎖定,dependencyManagement 可以統(tǒng)一管理項(xiàng)目的版本號(hào),確保應(yīng)用的各個(gè)項(xiàng)目的依賴和版本一致。
如果我們項(xiàng)目中只想使用 spring core 5.2.0 的包,pom.xml 可以改為如下
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.0.RELEASE</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.0.RELEASE</version> </dependency> </dependencies>
通過(guò)上圖可以看到項(xiàng)目引入是 spring core 5.2.0 的包
總結(jié)
綜上就是 maven 如何排查依賴沖突以及解決方法,對(duì)于排查依賴個(gè)人比較推薦使用 maven helper 插件,至于解決依賴沖突個(gè)人推薦使用版本鎖定的方法,此外 dependencyManagement 只是聲明依賴,并不自動(dòng)實(shí)現(xiàn)引入,因此子項(xiàng)目需要顯示的聲明需要用的依賴
以上就是Maven依賴沖突原因以及解決方法的詳細(xì)內(nèi)容,更多關(guān)于Maven依賴沖突的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
spring cloud gateway請(qǐng)求跨域問題解決方案
這篇文章主要介紹了spring cloud gateway請(qǐng)求跨域問題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01Java中數(shù)組復(fù)制的三種方式小結(jié)
在Java中,數(shù)組復(fù)制是一種常見的操作,它允許開發(fā)人員在不修改原始數(shù)組的情況下創(chuàng)建一個(gè)新的數(shù)組,本文就來(lái)介紹三種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02自定義一個(gè)簡(jiǎn)單的JDBC連接池實(shí)現(xiàn)方法
下面小編就為大家分享一篇自定義一個(gè)簡(jiǎn)單的JDBC連接池實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12