一。参考文档
https://maven.apache.org/plugins/maven-dependency-plugin/examples/filtering-the-dependency-tree.html
https://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html
二。查看 jar 包的间接依赖
1. 如果发现某个 jar 包,自己的 pom 中并没有定义,想看一下是被哪个 jar 包间接引用的,两种方法
(1) 用 mvn dependency:tree>temp/tree.txt, 直接输出到当前项目下,然后在 idea 中打开,搜索要找的 jar 包名字即可.
这里的 "+-" 和 "\-" 并没有什么意义,只是为了让分级看起来更直观
参考:https://stackoverflow.com/questions/34006740/what-is-the-difference-between-and-in-maven-dependency-tree-output
如图,可看到 jline 是在 zookeeper 中被间接引用的
(2) 用 include 参数
H:\下载\新建文件夹\yjg>mvn dependency:tree -Dincludes=jline
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effe
[WARNING] 'build.plugins.plugin.version'for org.apache.maven.pl
[WARNING]
[WARNING] It is highly recommended to fix these problems because
[WARNING]
[WARNING] For this reason, future Maven versions might no longer
[WARNING]
[INFO]
[INFO] ---------------------------------------------------------
[INFO] Building esshop Maven Webapp 0.0.1-SNAPSHOT
[INFO] ---------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ essh
[INFO] esshop:esshop:war:0.0.1-SNAPSHOT
[INFO] \- org.apache.zookeeper:zookeeper:jar:3.3.3:compile
[INFO] \- jline:jline:jar:0.9.94:compile
[INFO] ---------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ---------------------------------------------------------
[INFO] Total time: 3.256 s
[INFO] Finished at: 2017-12-05T08:57:49+08:00
同样的结果
2. 如果想看冲突和重复的具体情况,用 verbose 参数
如图,用了程序显式定义的版本
H:\下载\新建文件夹\yjg>mvn dependency:tree -Dverbose -Dincludes=commons-collections
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for esshop:esshop:war:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version'for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 621,
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building esshop Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ esshop ---
[INFO] esshop:esshop:war:0.0.1-SNAPSHOT
[INFO] +- commons-collections:commons-collections:jar:3.2.1:compile
[INFO] +- org.hibernate:hibernate:jar:3.2.2.ga:compile
[INFO] | \- (commons-collections:commons-collections:jar:2.1.1:compile - omitted for conflict with 3.2.1)
[INFO] +- org.hibernate:hibernate-annotations:jar:3.4.0.GA:compile
[INFO] | \- org.hibernate:hibernate-core:jar:3.3.0.SP1:compile
[INFO] | \- (commons-collections:commons-collections:jar:3.1:compile - omitted for conflict with 3.2.1)
[INFO] +- org.springframework.security:spring-security-core:jar:2.0.4:compile
[INFO] | \- (commons-collections:commons-collections:jar:3.2:compile - omitted for conflict with 3.2.1)
[INFO] +- org.apache.velocity:velocity:jar:1.5:compile
[INFO] | \- (commons-collections:commons-collections:jar:3.1:compile - omitted for conflict with 3.2.1)
[INFO] \- net.sf.json-lib:json-lib:jar:jdk15:2.4:compile
[INFO] \- (commons-collections:commons-collections:jar:3.2.1:compile - omitted for duplicate)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
三。处理 jar 包依赖冲突
简介:处理 jar 包依赖冲突,首先,对于多个 jar 包都引用同一 jar 包的情况,最好是在程序中显式定义被共同引用的 jar 包的依赖,来统一版本号,方便维护
如果 A 和 B 都依赖同一 jar 包 C, 可能会出现两种情况
1.A 和 B 引用的 C 版本相同,这时按照 pom 定义顺序选择第一个即可,没有冲突问题,如果在项目的 maven 中显示定义了 C 依赖,那么用选择项目定义的依赖,反正 version 都一样,没有影响
2.A 和 B 依赖的 C 版本不同,选择版本高的那个,这时会出现两种结果
(1) 高版本兼容低版本,所以不会出现问题
(2) 高版本不兼容低版本,假如 A 依赖 C2 版本,B 依赖 C3 版本,C3 不兼容 C2,maven 选择了高版本 C3, 对 A 来说会出现问题
有 3 种解决方法
[1] 提升 A 版本,找到依赖 C3 的 A 版本
[2] 如果 B 版本也可依赖 C2, 在项目的 maven 中显示定义对 C2 的依赖,这样所有都使用 C2 版本
[3] 如果 B 版本不支持 C2 版本,只能降低 B 版本,找到依赖 C2 的 B 版本
从功能性和可维护性考虑,高版本提供的功能更多,bug 更少,优先考虑 1
再考虑 2
最后考虑 3