2. 项目准备

在实际项目中,依赖树会迅速膨胀并变得复杂。但为了演示,我们创建一个包含两个模块的小型项目:module1module2,每个模块引入 2-3 个依赖。此外,module1 依赖 module2

首先创建 module2,添加以下依赖:

<dependencies>
    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-bean</artifactId>
        <version>6.1.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

接着创建 module1,添加以下依赖:

<dependencies>
    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>6.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>com.baeldung.module2</groupId>
        <artifactId>module2</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

⚠️ 注意:我们关注的是依赖关系本身,而非库的功能。目标是检测版本冲突、识别冗余依赖、解决构建/运行时问题,并可视化依赖树。

3. 分析依赖树的方法

3.1. 使用 Maven Dependency Plugin

maven-dependency-plugin 默认以文本格式输出依赖树。

module2 目录执行命令查看其依赖树:

$ mvn dependency:tree 
// 输出片段
[INFO] com.baeldung.module2:module2:jar:1.0
[INFO] +- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] +- org.springframework:spring-beans:jar:6.1.1:compile
[INFO] |  \- org.springframework:spring-core:jar:6.1.1:compile
[INFO] |     \- org.springframework:spring-jcl:jar:6.1.1:compile
[INFO] \- junit:junit:jar:4.13.2:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test

module1 目录执行命令查看其依赖树:

$ mvn dependency:tree 
// 输出片段
[INFO] com.baeldung.module1:module1:jar:1.0
[INFO] +- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] +- org.springframework:spring-core:jar:6.1.1:compile
[INFO] |  \- org.springframework:spring-jcl:jar:6.1.1:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] \- com.baeldung.module2:module2:jar:1.0:compile
[INFO]    \- org.springframework:spring-beans:jar:6.1.1:compile

过滤依赖树

✅ 使用 -Dincludes 只显示特定依赖(如 slf4j):

$ mvn dependency:tree -Dincludes=org.slf4j
// 输出
[INFO] com.baeldung.module1:module1:jar:1.0
[INFO] \- org.slf4j:slf4j-api:jar:1.7.25:compile

❌ 使用 -Dexcludes 排除特定依赖(如 slf4j):

$ mvn dependency:tree -Dexcludes=org.slf4j
// 输出
[INFO] com.baeldung.module1:module1:jar:1.0
[INFO] +- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] +- org.springframework:spring-core:jar:6.1.1:compile
[INFO] |  \- org.springframework:spring-jcl:jar:6.1.1:compile
[INFO] \- com.baeldung.module2:module2:jar:1.0:compile
[INFO]    \- org.springframework:spring-beans:jar:6.1.1:compile

详细分析依赖

使用 -Dverbose 查看冲突解决细节:

$ mvn dependency:tree -Dverbose
// 输出
[INFO] com.baeldung.module1:module1:jar:1.0
[INFO] +- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] +- org.springframework:spring-core:jar:6.1.1:compile
[INFO] |  \- org.springframework:spring-jcl:jar:6.1.1:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] \- com.baeldung.module2:module2:jar:1.0:compile
[INFO]    +- (commons-collections:commons-collections:jar:3.2.2:compile - omitted for duplicate)
[INFO]    +- (org.slf4j:slf4j-api:jar:1.7.25:compile - omitted for duplicate)
[INFO]    \- org.springframework:spring-beans:jar:6.1.1:compile
[INFO]       \- (org.springframework:spring-core:jar:6.1.1:compile - omitted for duplicate)

踩坑提示:这里显示 spring-core 采用了 module1 的版本,module2 中的重复依赖被忽略。

导出可视化格式

对于复杂项目,文本输出可读性差。插件支持导出为 dotgraphmltgf 等格式:

$ mvn dependency:tree -DoutputType=graphml -DoutputFile=dependency.graphml

使用 yED 编辑器打开 dependency.graphml,通过 Layout > Hierarchic > Orientation > Left to Right 调整布局:

yED 编辑器依赖图

3.2. 使用 Eclipse/IntelliJ 等 IDE

Eclipse

通过 m2e 插件查看依赖树:

  1. 打开 module1pom.xml
  2. 切换到 Dependency Hierarchy 标签页
  3. 左侧显示依赖层级(类似 -Dverbose 输出),右侧显示最终解析的依赖列表:

Eclipse 依赖图

IntelliJ IDEA

  • 社区版:右键项目 → Dependency AnalyserIntelliJ 社区版依赖图
  • 旗舰版:右键项目 → MavenShow DiagramIntelliJ 旗舰版依赖图

3.3. 使用第三方工具

以下工具可导入 graphml 文件进行高级可视化分析:

4. 总结

本文介绍了 Maven 依赖树的生成、过滤和可视化方法。核心要点:

  1. 使用 mvn dependency:tree 快速分析依赖关系
  2. 通过 -Dincludes/-Dexcludes 过滤依赖
  3. 结合 IDE 插件或第三方工具实现可视化

完整示例代码见 GitHub 仓库


原始标题:Understanding Maven Dependency Graph or Tree | Baeldung