Maven依賴沖突原因以及解決方法
什么是依賴沖突
依賴沖突是指項目依賴的某一個 jar 包,有多個不同的版本,因而造成類包版本沖突
依賴沖突的原因
依賴沖突很經(jīng)常是類包之間的間接依賴引起的。每個顯式聲明的類包都會依賴于一些其它的隱式類包,這些隱式的類包會被 maven 間接引入進來,從而造成類包沖突
如何解決依賴沖突
首先查看產(chǎn)生依賴沖突的類 jar,其次找出我們不想要的依賴類 jar,手工將其排除在外就可以了。具體執(zhí)行步驟如下
1、查看依賴沖突
a、通過 dependency:tree 是命令來檢查版本沖突
mvn -Dverbose dependency:tree
當敲入上述命令時,控制臺會出現(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 包被重復依賴,最后寫著 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 會發(fā)現(xiàn)多了一個 Dependency Analyzer 視圖,如下
上面按鈕的圖標含義如下
- Conflicts(查看沖突)
- All Dependencies as List(列表形式查看所有依賴)
- All Dependencies as Tree(樹形式查看所有依賴)
上圖說明有 3 個 jar 存在沖突,點擊沖突的 jar,可以查看和哪個 jar 產(chǎn)生沖突,如下圖
2、解決沖突
項目的 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>
通過查看依賴樹,我們知道項目會引用 5.2.7.RELEASE 的 spring core jar 包,而不會引用 5.2.0 的 jar 包,如果我們想用 5.2.0 版本的 spring core 包,我們該如何做?
a、使用第一聲明者優(yōu)先原則
誰先定義的就用誰的傳遞依賴,即在 pom.xml 文件自上而下,先聲明的 jar 坐標,就先引用該 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)
通過依賴樹,我們可以看到項目已經(jīng)引入 5.2.0 版本的 spring core 包
b、使用路徑近者優(yōu)先原則
即直接依賴級別高于傳遞依賴。因此我們可以在最先的 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>
通過上圖可以看到項目引入是 spring core 5.2.0 的包
c、排除依賴
排除依賴如果是 idea,可以使用 maven helper 插件進行排除。點開 pom.xml,切換到 Dependency Analyzer 視圖,選擇 All Dependencies as Tree,點擊要排除的 jar,右鍵會出現(xiàn) Execlude 選項,如下
它產(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>
通過上圖可以看到項目引入是 spring core 5.2.0 的包
4、版本鎖定
使用 dependencyManagement 進行版本鎖定,dependencyManagement 可以統(tǒ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>
通過上圖可以看到項目引入是 spring core 5.2.0 的包
總結(jié)
綜上就是 maven 如何排查依賴沖突以及解決方法,對于排查依賴個人比較推薦使用 maven helper 插件,至于解決依賴沖突個人推薦使用版本鎖定的方法,此外 dependencyManagement 只是聲明依賴,并不自動實現(xiàn)引入,因此子項目需要顯示的聲明需要用的依賴
以上就是Maven依賴沖突原因以及解決方法的詳細內(nèi)容,更多關(guān)于Maven依賴沖突的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
spring cloud gateway請求跨域問題解決方案
這篇文章主要介紹了spring cloud gateway請求跨域問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01