maven依賴沖突以及解決方法


什么是依賴沖突

依賴沖突是指項目依賴的某一個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視圖,如下
Dependency Analyzer.png
上面按鈕的圖標含義如下

  • Conflicts(查看沖突)
  • All Dependencies as List(列表形式查看所有依賴)
  • All Dependencies as Tree(樹形式查看所有依賴)

上圖說明有3個jar存在沖突,點擊沖突的jar,可以查看和哪個jar產生沖突,如下圖
查看沖突.png

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、使用第一聲明者優先原則

誰先定義的就用誰的傳遞依賴,即在pom.xml文件自上而下,先聲明的jar坐標,就先引用該jar的傳遞依賴。因此我們如果要使用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)

通過依賴樹,我們可以看到項目已經引入5.2.0版本的spring core包

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

即直接依賴級別高於傳遞依賴。因此我們可以在最先的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>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>



    </dependencies>

路徑近者優先.png通過上圖可以看到項目引入是 spring core 5.2.0的包

c、排除依賴

排除依賴如果是idea,可以使用maven helper插件進行排除。點開pom.xml,切換到Dependency Analyzer視圖,選擇All Dependencies as Tree,點擊要排除的jar,右鍵會出現Execlude選項,如下
去除依賴.png
它產生的效果和如下配置是一樣

 <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>

查看依賴.png
通過上圖可以看到項目引入是 spring core 5.2.0的包

4、版本鎖定

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

如果我們項目中只想使用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>

版本鎖定.png
通過上圖可以看到項目引入是 spring core 5.2.0的包

總結

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM