IDEA查看Maven依賴樹與解決Jar包沖突的方法
模擬依賴沖突
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.0.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.23</version>
</dependency>
</dependencies>
一、查看依賴樹
方法 1:IDEA 自帶 Maven 工具
打開右側(cè) Maven 工具欄(快捷鍵 Alt+Shift+M / View > Tool Windows > Maven)。
找到你的項目,展開 Dependencies 節(jié)點。
- 這里能看到所有依賴樹結(jié)構(gòu)。
- 如果某個依賴有沖突,IDEA 通常會用 灰色/紅色字體標注出被排除或版本沖突的 jar。
鼠標懸停在依賴上,可以看到它的 來源(哪個依賴引入的)。

方法 2:使用命令行
在項目根目錄執(zhí)行:
mvn dependency:tree
這會打印依賴樹,例如:
[INFO] org.example:dependency-test:jar:1.0-SNAPSHOT [INFO] +- org.springframework:spring-webmvc:jar:6.0.9:compile [INFO] | +- org.springframework:spring-beans:jar:6.0.9:compile [INFO] | +- org.springframework:spring-context:jar:6.0.9:compile [INFO] | +- org.springframework:spring-core:jar:6.0.9:compile [INFO] | | \- org.springframework:spring-jcl:jar:6.0.9:compile [INFO] | +- org.springframework:spring-expression:jar:6.0.9:compile [INFO] | \- org.springframework:spring-web:jar:6.0.9:compile [INFO] | \- io.micrometer:micrometer-observation:jar:1.10.7:compile [INFO] | \- io.micrometer:micrometer-commons:jar:1.10.7:compile [INFO] \- org.springframework:spring-aop:jar:5.3.23:compile
org.springframework:spring-beans:jar:6.0.9:compile用的6.0.0的版本
如果樹太大,可以加過濾:
mvn dependency:tree -Dincludes=org.springframework
方法 3:IDEA 插件(推薦)
安裝 Maven Helper 插件(在 IDEA 插件市場搜索)。
打開 pom.xml,底部會出現(xiàn) Dependency Analyzer 標簽頁。
在這個面板里,可以:
- 一鍵查看依賴樹
- 高亮顯示沖突 jar 包
- 直接右鍵選擇 Exclude 依賴

二、找出沖突 jar 包
- 在依賴樹里尋找 同一個 groupId + artifactId 但不同版本的依賴。
例如:
org.springframework:spring-beans:6.0.9 org.springframework:spring-beans:5.3.23 (omitted for conflict)
- 表示
spring-beans有兩個版本沖突。 - Maven 默認會選 路徑最短(離項目最近)的依賴,其他版本就會被排除(omitted)。
但有時候這個版本并不是你想要的,就需要手動干預(yù)。
三、解決沖突(exclusion)
在 pom.xml 中找到?jīng)_突依賴的 上游依賴,添加 exclusion。
例如,如果 spring-boot-starter 引入了錯誤的 commons-logging,可以這樣寫:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.0.9</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>
</dependency>
或者如果只是版本不一致,可以在 dependencyManagement 里強制指定版本:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.23</version>
</dependency>
</dependencies>
</dependencyManagement>

四、總結(jié)
- 快速看依賴樹 → IDEA 自帶依賴樹 或
mvn dependency:tree。 - 高效排查沖突 → 裝 Maven Helper 插件,直觀顯示沖突。
- 解決沖突 → 用
exclusion排除不需要的包,或在dependencyManagement鎖定版本。
到此這篇關(guān)于IDEA查看Maven依賴樹與解決Jar包沖突的方法的文章就介紹到這了,更多相關(guān)IDEA查看Maven依賴樹內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用SpringDataJPA開啟審計功能,自動保存操作人操作時間
這篇文章主要介紹了利用SpringDataJPA開啟審計功能,自動保存操作人操作時間,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot集成RabbitMQ實現(xiàn)用戶注冊的示例代碼
這篇文章主要介紹了SpringBoot集成RabbitMQ實現(xiàn)用戶注冊的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
SpringBoot如何返回Json數(shù)據(jù)格式
這篇文章主要介紹了SpringBoot如何返回Json數(shù)據(jù)格式問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03

