詳解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)绻褂?.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)目需要顯示的聲明需要用的依賴
到此這篇關(guān)于詳解maven依賴沖突以及解決方法的文章就介紹到這了,更多相關(guān)maven依賴沖突 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot之搞定mongoTemplate的知識(shí)小結(jié)
這篇文章主要介紹了Spring Boot之搞定mongoTemplate的知識(shí)小結(jié),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Spring實(shí)現(xiàn)聲明式事務(wù)的方法詳解
這篇文章主要介紹了Spring實(shí)現(xiàn)聲明式事務(wù)的方法詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01VScode 打造完美java開發(fā)環(huán)境最新教程
這篇文章主要介紹了VScode 打造完美java開發(fā)環(huán)境最新教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12