一.参考文档
https://maven.apache.org/plugins/maven-dependency-plugin/examples/resolving-conflicts-using-the-dependency-tree.html
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