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)。
找到你的項(xiàng)目,展開 Dependencies 節(jié)點(diǎn)。
- 這里能看到所有依賴樹結(jié)構(gòu)。
- 如果某個(gè)依賴有沖突,IDEA 通常會(huì)用 灰色/紅色字體標(biāo)注出被排除或版本沖突的 jar。
鼠標(biāo)懸停在依賴上,可以看到它的 來源(哪個(gè)依賴引入的)。

方法 2:使用命令行
在項(xiàng)目根目錄執(zhí)行:
mvn dependency:tree
這會(huì)打印依賴樹,例如:
[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 插件市場(chǎng)搜索)。
打開 pom.xml,底部會(huì)出現(xiàn) Dependency Analyzer 標(biāo)簽頁(yè)。
在這個(gè)面板里,可以:
- 一鍵查看依賴樹
- 高亮顯示沖突 jar 包
- 直接右鍵選擇 Exclude 依賴

二、找出沖突 jar 包
- 在依賴樹里尋找 同一個(gè) groupId + artifactId 但不同版本的依賴。
例如:
org.springframework:spring-beans:6.0.9 org.springframework:spring-beans:5.3.23 (omitted for conflict)
- 表示
spring-beans有兩個(gè)版本沖突。 - Maven 默認(rèn)會(huì)選 路徑最短(離項(xiàng)目最近)的依賴,其他版本就會(huì)被排除(omitted)。
但有時(shí)候這個(gè)版本并不是你想要的,就需要手動(dòng)干預(yù)。
三、解決沖突(exclusion)
在 pom.xml 中找到?jīng)_突依賴的 上游依賴,添加 exclusion。
例如,如果 spring-boot-starter 引入了錯(cuò)誤的 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 里強(qiáng)制指定版本:
<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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot 2.7.6整合redis與低版本的區(qū)別
這篇文章主要介紹了Spring Boot 2.7.6整合redis與低版本的區(qū)別,文中補(bǔ)充介紹了SpringBoot各個(gè)版本使用Redis之間的區(qū)別實(shí)例講解,需要的朋友可以參考下2023-02-02
Java實(shí)現(xiàn)將html字符串插入到PPT幻燈片
Java后端代碼操作PPT幻燈片時(shí),可直接在幻燈片中繪制形狀,并在形狀中添加文本字符串內(nèi)容。本篇文章主要介紹通過java實(shí)現(xiàn)將html字符串添加到PPT幻燈片的的方法,可添加文字、圖片、視頻、音頻等。以下是具體方法和步驟。2021-11-11
關(guān)于spring?data?jpa?模糊查詢like的坑點(diǎn)
這篇文章主要介紹了關(guān)于spring?data?jpa?模糊查詢like的坑點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
MyBatis中resultType和parameterType和resultMap使用總結(jié)
這篇文章主要介紹了MyBatis中resultType和parameterType和resultMap使用總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
SpringBoot集成PostgreSQL表級(jí)備份與恢復(fù)的實(shí)戰(zhàn)指南
本文介紹了在企業(yè)級(jí)應(yīng)用中使用SpringBoot+PostgreSQL實(shí)現(xiàn)數(shù)據(jù)備份與恢復(fù)的方法,主要包括使用pg_dump導(dǎo)出數(shù)據(jù)、pg_restore恢復(fù)數(shù)據(jù)及ProcessBuilder調(diào)用系統(tǒng)命令,并詳細(xì)解釋了ProcessBuilder、pg_dump與pg_restore的用法、參數(shù)配置及常見問題解決方法2026-04-04
IntelliJ IDEA設(shè)置JVM運(yùn)行參數(shù)的操作方法
這篇文章主要介紹了IntelliJ IDEA設(shè)置JVM運(yùn)行參數(shù)的操作方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-03-03

