maven包依赖冲突

转自:点击打开链接

那句话怎么讲来着的... 

引用
如果你爱他,就请让他用Maven,因为那里是天堂,如果你恨他,就请让他用Maven,因为那里是地狱.


Maven对于新手来说是《步步惊心》,因为它包罗万象,博大精深,因为当你初来乍到时,你就像一个进入森林的陌生访客一样迷茫。 

Maven对于老手来说是《真爱配方》,因为它无所不能,利如刀锋,使用Maven做开发,如饮美酒如悦美人。 

Maven对于新手来说,最痛苦的一件事莫过于包之间的冲突,由于Maven的依赖传递性,当你引入一个依赖类时,其身后的依赖类也一起如过江之鲫纷至沓来了。 

举个栗子  

A依赖于B及C,而B又依赖于X、Y,而C依赖于X、M,则A除引B及C的依赖包下,还会引入X,Y,M的依赖包(一般情况下了,Maven可通过<scope>等若干种方式控制传递依赖)。 

这里有一个需要特别注意的,即B和C同时依赖于X,假设B依赖于X的1.0版本,而C依赖于X的2.0版本,A究竟依赖于X的1.0还是2.0版本呢? 

这就看Classloader的加载顺序了,假设Classloader先加载X_1.0,而它就不会再加载X_2.0了,如果A恰恰希望使用X_2.0呢,血案就这样不期而遇了。 

我常抡的三板斧  

第一板斧:找到传递依赖的鬼出在哪里?  

dependency:tree 是把照妖照,pom.xml用它照照,所有传递性依赖都将无处遁形,并且会以层级树方式展现,非常直观。 

以下就是执行 dependency:tree 后的一个输出: 
引用

[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ euler-foundation --- 
[INFO] com.hsit:euler-foundation:jar:0.9.0.1-SNAPSHOT 
[INFO] +- com.rop:rop:jar:1.0.1:compile 
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.5:compile 
[INFO] |  +- org.slf4j:slf4j-log4j12:jar:1.7.5:compile 
[INFO] |  +- log4j:log4j:jar:1.2.16:compile 
[INFO] |  +- commons-lang:commons-lang:jar:2.6:compile 
[INFO] |  +- commons-codec:commons-codec:jar:1.6:compile 
[INFO] |  +- javax.validation:validation-api:jar:1.0.0.GA:compile 
[INFO] |  +- org.hibernate:hibernate-validator:jar:4.2.0.Final:compile 
[INFO] |  +- org.codehaus.jackson:jackson-core-asl:jar:1.9.5:compile 
[INFO] |  +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.5:compile 
[INFO] |  +- org.codehaus.jackson:jackson-jaxrs:jar:1.9.5:compile 
[INFO] |  +- org.codehaus.jackson:jackson-xc:jar:1.9.5:compile 
[INFO] |  \- com.fasterxml.jackson.dataformat:jackson-dataformat-xml:jar:2.2.3:compile 
[INFO] |     +- com.fasterxml.jackson.core:jackson-core:jar:2.2.3:compile 
[INFO] |     +- com.fasterxml.jackson.core:jackson-annotations:jar:2.2.3:compile 
[INFO] |     +- com.fasterxml.jackson.core:jackson-databind:jar:2.2.3:compile 
[INFO] |     +- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.2.3:compile 
[INFO] |     \- org.codehaus.woodstox:stax2-api:jar:3.1.1:compile 
[INFO] |        \- javax.xml.stream:stax-api:jar:1.0-2:compile 


     刚才吹嘘 dependency:tree 时,我用到了“无处遁形”,其实有时你会发现简单地用 dependency:tree 往往并不能查看到所有的传递依赖。不过如果你真的想要看所有的,必须得加一个 -Dverbose 参数,这时就必定是最全的了。 

    全是全了,但显示出来的东西太多,头晕目眩,有没有好法呢?当然有了,加上 Dincludes 或者 Dexcludes 说出你喜欢或讨厌, dependency:tree 就会帮你过滤出来: 
引用
Dincludes=org.springframework:spring-tx

   过滤串使用groupId:artifactId:version的方式进行过滤,可以不写全啦,如: 
Java代码   收藏代码
  1. mvn dependency:tree -Dverbose -Dincludes=asm:asm  

   就会出来asm依赖包的分析信息: 
引用

[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ ridge-test --- 
[INFO] com.ridge:ridge-test:jar:1.0.2-SNAPSHOT 
[INFO] +- asm:asm:jar:3.2:compile 
[INFO] \- org.unitils:unitils-dbmaintainer:jar:3.3:compile 
[INFO]    \- org.hibernate:hibernate:jar:3.2.5.ga:compile 
[INFO]       +- cglib:cglib:jar:2.1_3:compile 
[INFO]       |  \- (asm:asm:jar:1.5.3:compile - omitted for conflict with 3.2) 
[INFO]       \- (asm:asm:jar:1.5.3:compile - omitted for conflict with 3.2) 
[INFO] ------------------------------------------------------------------------ 

   对asm有依赖有一个直接的依赖(asm:asm:jar:3.2)还有一个传递进入的依赖(asm:asm:jar:1.5.3) 

第二板斧:将不想要的传递依赖剪除掉  

   承上,假设我们不希望asm:asm:jar:1.5.3出现,根据分析,我们知道它是经由org.unitils:unitils-dbmaintainer:jar:3.3引入的,那么在pom.xml中找到这个依赖,做其它的调整: 
Xml代码   收藏代码
  1. <dependency>  
  2.     <groupId>org.unitils</groupId>  
  3.     <artifactId>unitils-dbmaintainer</artifactId>  
  4.     <version>${unitils.version}</version>  
  5.     <exclusions>  
  6.         <exclusion>  
  7.             <artifactId>dbunit</artifactId>  
  8.             <groupId>org.dbunit</groupId>  
  9.         </exclusion>  
  10.         <!-- 这个就是我们要加的片断 -->  
  11.         <exclusion>  
  12.             <artifactId>asm</artifactId>  
  13.             <groupId>asm</groupId>  
  14.         </exclusion>  
  15.     </exclusions>  
  16. </dependency>  


   再分析一下,你可以看到传递依赖没有了: 
Xml代码   收藏代码
  1. [INFO]  
  2. [INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ ridge-test ---  
  3. [INFO] com.ridge:ridge-test:jar:1.0.2-SNAPSHOT  
  4. [INFO] \- asm:asm:jar:3.2:compile  
  5. [INFO] ------------------------------------------------------------------------  
  6. [INFO] BUILD SUCCESS  


第三板斧:查看运行期类来源的JAR包  

有时,你以为解决了,但是偏偏还是报类包冲突(典型症状是java.lang.ClassNotFoundException或Method不兼容等异常),这时你可以设置一个断点,在断点处通过下面这个我做的工具类来查看Class所来源的JAR包: 

Java代码   收藏代码
  1. package com.ridge.util;  
  2.   
  3. import java.io.File;  
  4. import java.net.MalformedURLException;  
  5. import java.net.URL;  
  6. import java.security.CodeSource;  
  7. import java.security.ProtectionDomain;  
  8.   
  9. /** 
  10.  * @author : chenxh 
  11.  * @date: 13-10-31 
  12.  */  
  13. public class ClassLocationUtils {  
  14.   
  15.     /** 
  16.      * 获取类所有的路径 
  17.      * @param cls 
  18.      * @return 
  19.      */  
  20.     public static String where(final Class cls) {  
  21.         if (cls == null)throw new IllegalArgumentException("null input: cls");  
  22.         URL result = null;  
  23.         final String clsAsResource = cls.getName().replace('.''/').concat(".class");  
  24.         final ProtectionDomain pd = cls.getProtectionDomain();  
  25.         if (pd != null) {  
  26.             final CodeSource cs = pd.getCodeSource();  
  27.             if (cs != null) result = cs.getLocation();  
  28.             if (result != null) {  
  29.                 if ("file".equals(result.getProtocol())) {  
  30.                     try {  
  31.                         if (result.toExternalForm().endsWith(".jar") ||  
  32.                                 result.toExternalForm().endsWith(".zip"))  
  33.                             result = new URL("jar:".concat(result.toExternalForm())  
  34.                                     .concat("!/").concat(clsAsResource));  
  35.                         else if (new File(result.getFile()).isDirectory())  
  36.                             result = new URL(result, clsAsResource);  
  37.                     }  
  38.                     catch (MalformedURLException ignore) {}  
  39.                 }  
  40.             }  
  41.         }  
  42.         if (result == null) {  
  43.             final ClassLoader clsLoader = cls.getClassLoader();  
  44.             result = clsLoader != null ?  
  45.                     clsLoader.getResource(clsAsResource) :  
  46.                     ClassLoader.getSystemResource(clsAsResource);  
  47.         }  
  48.         return result.toString();  
  49.     }  
  50.   
  51. }  


随便写一个测试,设置好断点,在执行到断点处按alt+F8动态执行代码(intelij idea),假设我们输入: 
Java代码   收藏代码
  1. ClassLocationUtils.where(org.objectweb.asm.ClassVisitor.class)  

即可马上查出类对应的JAR了: 


这就是org.objectweb.asm.ClassVisitor类在运行期对应的JAR包,如果这个JAR包版本不是你期望你,就说明是你的IDE缓存造成的,这时建议你Reimport一下maven列表就可以了,如下所示(idea): 

 

Reimport一下,IDE会强制根据新的pom.xml设置重新分析并加载依赖类包,以得到和pom.xml设置相同的依赖。(这一步非常重要哦,经常项目组pom.xml是相同的,但是就是有些人可以运行,有些人不能运行,俗称人品问题,其实都是IDE的缓存造成的了  ) 


转自:点击打开链接

使用maven最烦人的可能就是类包之间的版本冲突引发的问题了,类包冲突的一个很大的原因即产类包之间的间接依赖引起的。每个显式声明的类包都会依赖于一些其它的隐式类包,这些隐式的类包会被maven间接引入进来,因而可能造成一个我们不想要的类包的载入,严重的甚至会引起类包之间的冲突。 


要解决这个问题,首先就是要查看pom.xml显式和隐式的依赖类包,然后通过这个类包树找出我们不想要的依赖类包,手工将其排除在外就可以了。 

下面,通过一个例子来说明: 

我的项目使用testng进行测试,使用了untilis,由于unitils的类包会隐式依赖于junit,这是我不想看到的,下面的目的就是找出junit会谁隐式载入了,然后exculte掉它。 


通过idea的maven依赖分析将不需要的依赖exclude掉 


打开maven的pom.xml,在某个<dependency>中通过右键菜单:maven->show dependency 打开分析的图形化页面,如下所示: 



通过菜单的exclude即解决这个间接依赖。 

通过这个依赖树,我们还可以看到Junit还通过“unitils-spring”的依赖间接载入了,如下所示: 



从上面的依赖树中,我们可以看出junit通过unitils-spring的unitils-database间接引入了,由于我的项目都不需要数据库的测试,因此,可以把unitils-database项整个exclude掉。 

下面,是处理完成后的pom.xml关键片断: 

Xml代码   收藏代码
  1.     <dependency>  
  2.         <groupId>org.unitils</groupId>  
  3.         <artifactId>unitils-testng</artifactId>  
  4.         <version>${unitils.version}</version>  
  5.         <scope>test</scope>  
  6.         <exclusions>  
  7.             <exclusion>  
  8.                 <artifactId>junit</artifactId>  
  9.                 <groupId>junit</groupId>  
  10.             </exclusion>  
  11.         </exclusions>  
  12.   
  13.     </dependency>  
  14.   
  15.     <dependency>  
  16.         <groupId>org.unitils</groupId>  
  17.         <artifactId>unitils-spring</artifactId>  
  18.         <version>${unitils.version}</version>  
  19.         <scope>test</scope>  
  20.         <exclusions>  
  21.             <exclusion>  
  22.                 <artifactId>unitils-database</artifactId>  
  23.                 <groupId>org.unitils</groupId>  
  24.             </exclusion>  
  25.         </exclusions>  
  26.     </dependency>  
  27. </dependencies>  


这样,被间接隐式引入的junit就被我们exclude在外了。 


有时通过idea的依赖分析工具产生的树不够全,这时就需要使用mvn dependency:tree来查看依赖树了。 
通过mvn dependency:tree 查看依赖树 


引用
mvn dependency:tree


以下是使用这个工具产生的依赖树: 
引用
E:\01workspace\chenxh\09research\rop\rop>mvn dependency:tree 
[WARNING] 
[WARNING] Some problems were encountered while building the effective settings 
[WARNING] 'pluginRepositories.pluginRepository.id' must not be 'local', this identifier is reserved for the local re 
tory, using it for other repositories will corrupt your repository metadata. @ C:\Users\Administrator\.m2\settings.x 
[WARNING] 
[INFO] Scanning for projects... 
[INFO] 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building rop 1.0-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ rop --- 
[INFO] com.rop:rop:jar:1.0-SNAPSHOT 
[INFO] +- javax.validation:validation-api:jar:1.0.0.GA:compile 
[INFO] +- org.hibernate:hibernate-validator:jar:4.2.0.Final:compile 
[INFO] +- org.codehaus.jackson:jackson-core-asl:jar:1.9.5:compile 
[INFO] +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.5:compile 
[INFO] +- org.codehaus.jackson:jackson-jaxrs:jar:1.9.5:compile 
[INFO] +- org.codehaus.jackson:jackson-xc:jar:1.9.5:compile 
[INFO] +- com.fasterxml.jackson.dataformat:jackson-dataformat-xml:jar:2.0.0-RC2:compile 
[INFO] |  +- com.fasterxml.jackson.core:jackson-core:jar:2.0.0-RC2:compile 
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.0.0-RC2:compile 
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.0.0-RC2:compile 
[INFO] |  +- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.0.0-RC2:compile 
[INFO] |  \- org.codehaus.woodstox:stax2-api:jar:3.1.1:compile 
[INFO] |     \- javax.xml.stream:stax-api:jar:1.0-2:compile 
[INFO] +- org.slf4j:slf4j-api:jar:1.6.1:compile 
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.6.1:compile 
[INFO] +- log4j:log4j:jar:1.2.16:compile 
[INFO] +- org.springframework:spring-core:jar:3.1.1.RELEASE:compile 
[INFO] |  +- org.springframework:spring-asm:jar:3.1.1.RELEASE:compile 
[INFO] |  \- commons-logging:commons-logging:jar:1.1.1:compile 
[INFO] +- org.springframework:spring-context:jar:3.1.1.RELEASE:compile 
[INFO] |  +- org.springframework:spring-aop:jar:3.1.1.RELEASE:compile 
[INFO] |  +- org.springframework:spring-beans:jar:3.1.1.RELEASE:compile 
[INFO] |  \- org.springframework:spring-expression:jar:3.1.1.RELEASE:compile 
[INFO] +- org.springframework:spring-context-support:jar:3.1.1.RELEASE:compile 
[INFO] +- org.springframework:spring-web:jar:3.1.1.RELEASE:compile 
[INFO] |  \- aopalliance:aopalliance:jar:1.0:compile 
[INFO] +- org.springframework:spring-test:jar:3.1.1.RELEASE:compile 
[INFO] +- org.springframework:spring-webmvc:jar:3.1.1.RELEASE:compile 
[INFO] +- org.testng:testng:jar:6.3:test 
[INFO] |  +- org.beanshell:bsh:jar:2.0b4:test 
[INFO] |  +- com.beust:jcommander:jar:1.12:test 
[INFO] |  \- org.yaml:snakeyaml:jar:1.6:test 
[INFO] +- org.mockito:mockito-all:jar:1.8.5:test 
[INFO] +- javax.servlet:servlet-api:jar:2.5:provided 
[INFO] +- org.unitils:unitils-core:jar:3.3:test 
[INFO] |  +- commons-lang:commons-lang:jar:2.3:test 
[INFO] |  +- commons-collections:commons-collections:jar:3.2:test 
[INFO] |  \- ognl:ognl:jar:2.6.9:test 
[INFO] +- org.unitils:unitils-testng:jar:3.3:test 
[INFO] |  \- org.testng:testng:jar:jdk15:5.8:test 
[INFO] |     \- junit:junit:jar:3.8.1:test 
[INFO] \- org.unitils:unitils-spring:jar:3.3:test 
[INFO]    +- org.springframework:spring-tx:jar:2.5.2:test 
[INFO]    \- org.unitils:unitils-database:jar:3.3:test 
[INFO]       +- org.unitils:unitils-dbmaintainer:jar:3.3:test 
[INFO]       |  +- org.hibernate:hibernate:jar:3.2.5.ga:test 
[INFO]       |  |  +- net.sf.ehcache:ehcache:jar:1.2.3:test 
[INFO]       |  |  +- asm:asm-attrs:jar:1.5.3:test 
[INFO]       |  |  +- dom4j:dom4j:jar:1.6.1:test 
[INFO]       |  |  +- antlr:antlr:jar:2.7.6:test 
[INFO]       |  |  +- cglib:cglib:jar:2.1_3:test 
[INFO]       |  |  \- asm:asm:jar:1.5.3:test 
[INFO]       |  \- org.dbunit:dbunit:jar:2.2.2:test 
[INFO]       |     +- junit-addons:junit-addons:jar:1.4:test 
[INFO]       |     |  +- xerces:xercesImpl:jar:2.6.2:test 
[INFO]       |     |  \- xerces:xmlParserAPIs:jar:2.6.2:test 
[INFO]       |     +- poi:poi:jar:2.5.1-final-20040804:test 
[INFO]       |     \- org.slf4j:slf4j-nop:jar:1.4.3:test 
[INFO]       +- commons-dbcp:commons-dbcp:jar:1.2.2:test 
[INFO]       |  \- commons-pool:commons-pool:jar:1.3:test 
[INFO]       \- org.springframework:spring-jdbc:jar:2.5.2:test 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 8.250s 
[INFO] Finished at: Fri Jun 08 09:08:09 CST 2012 
[INFO] Final Memory: 7M/245M 
[INFO] ------------------------------------------------------------------------ 


我原来一个使用idea分析不出的隐式依赖就是通用mvn dependency:tree找到的。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值