Maven如何查看工程依赖 & 依赖冲突解决方案

732 阅读1分钟

maven-dependency-plugin版本号

3.1.2

命令

  • 参考官方文档
  • It will output the resolved tree of dependencies that the Maven build process actually uses
mvn dependency:tree

示例输出

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ maven-demo ---
[INFO] org.example:maven-demo:jar:1.0-SNAPSHOT
[INFO] +- commons-fileupload:commons-fileupload:jar:1.4:compile
[INFO] |  \- commons-io:commons-io:jar:2.2:compile
[INFO] \- org.jodconverter:jodconverter-local:jar:4.2.4:compile
[INFO]    +- org.jodconverter:jodconverter-core:jar:4.2.4:compile
[INFO]    |  \- com.google.code.gson:gson:jar:2.8.6:runtime
[INFO]    +- org.openoffice:juh:jar:4.1.2:runtime
[INFO]    +- org.openoffice:jurt:jar:4.1.2:runtime
[INFO]    +- org.openoffice:ridl:jar:4.1.2:runtime
[INFO]    +- org.openoffice:unoil:jar:4.1.2:runtime
[INFO]    +- org.apache.commons:commons-lang3:jar:3.9:runtime
[INFO]    \- org.slf4j:slf4j-api:jar:1.7.29:runtime

依赖冲突解决办法

解决前

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-local</artifactId>
    <version>4.2.4</version>
</dependency>

image.png

image.png

  • 可以看到,commons-fileupload依赖了2.2版本的commons-io,jodconverter-local依赖了2.6版本的commons-io,Maven最终选择了2.2版本的commons-io解决冲突
  • 对于多个dependency依赖的相同dependency的不同版本,Maven如何选择最终的版本,官网中的原文是:

by default Maven resolves version conflicts with a nearest-wins strategy.

解决方法

在依赖树根级显式的引入期望的commons-io版本,解决该冲突问题

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-local</artifactId>
    <version>4.2.4</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

image.png

image.png