maven冲突解决

如果项目报错NoClassDefFoundError, NoSuchMethodError,不要头疼,看看你的包是否冲突。
如何定位冲突原因呢?

先来学习maven依赖的几个原则:
(1)依赖是使用Maven坐标来定位的,而Maven坐标主要由GAV(groupId, artifactId, version)构成。如果两个相同的依赖包,如果groupId, artifactId, version不同,那么maven也认为这两个是不同的。
(2)依赖会传递,A依赖了B,B依赖了C,那么A的依赖中就会出现B和C。
(3)Maven对同一个groupId, artifactId的冲突仲裁,不是以version越大越保留,而是依赖路径越短越优先,然后进行保留。
(4)依赖的scope会影响依赖的影响范围。

下面就来定位冲突原因:
首先看报错确定冲突的包;
然后查看项目的maven依赖树:
通过mvn dependency:tree  >  tree.txt 导出全部的依赖。
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ cart-web ---
[INFO] com.mogujie.trade:cart-web:war:1.0.0-SNAPSHOT
[INFO] +- org.springframework:spring-core:jar:4.0.6.RELEASE:compile
[INFO] +- org.springframework:spring-web:jar:4.0.6.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:4.0.6.RELEASE:compile
[INFO] |  |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- org.springframework:spring-beans:jar:4.0.6.RELEASE:compile
[INFO] |  \- org.springframework:spring-context:jar:4.0.6.RELEASE:compile
[INFO] +- org.springframework:spring-webmvc:jar:4.0.6.RELEASE:compile
[INFO] |  \- org.springframework:spring-expression:jar:4.0.6.RELEASE:compile
[INFO] +- org.springframework:spring-test:jar:4.0.6.RELEASE:test
[INFO] +- javax.servlet:javax.servlet-api:jar:3.0.1:provided
[INFO] +- javax.servlet.jsp:jsp-api:jar:2.2:provided
[INFO] +- com.mogujie.trade:cart-service:jar:1.0.0-SNAPSHOT:compile
[INFO] |  +- com.mogujie.trade:cart-component:jar:1.0.0-SNAPSHOT:compile
[INFO] |  |  \- org.springframework:spring-context-support:jar:4.0.6.RELEASE:compile
[INFO] |  +- com.mogujie.pricecenter:pricecenter-common:jar:1.0.1-SNAPSHOT:compile
[INFO] |  |  +- org.projectlombok:lombok:jar:1.16.4:compile
[INFO] |  |  +- com.google.guava:guava:jar:18.0:compile
[INFO] |  |  +- org.apache.commons:commons-lang3:jar:3.0:compile
[INFO] |  |  +- org.apache.commons:commons-collections4:jar:4.0:compile
[INFO] |  |  +- redis.clients:jedis:jar:2.7.3:compile
[INFO] |  |  |  \- org.apache.commons:commons-pool2:jar:2.0:compile
[INFO] |  |  +- com.esotericsoftware.kryo:kryo:jar:2.24.0:compile
[INFO] |  |  |  +- com.esotericsoftware.minlog:minlog:jar:1.2:compile
[INFO] |  |  |  \- org.objenesis:objenesis:jar:2.1:compile
[INFO] |  |  \- net.sf.dozer:dozer:jar:5.5.1:compile
[INFO] |  |     \- commons-beanutils:commons-beanutils:jar:1.9.2:compile
[INFO] |  +- com.mogujie.service:muser-api:jar:1.0.3:compile
[INFO] |  +- com.mogujie.service:shopcenter-api:jar:1.0-SNAPSHOT:compile
[INFO] |  +- com.mogujie.service:item-center-api:jar:1.3.2-SNAPSHOT:compile
[INFO] |  |  +- com.mogujie.service:image-service-api:jar:1.1-SNAPSHOT:compile
[INFO] |  |  +- com.mogujie.pricecenter:pricecenter-client:jar:1.0.1-SNAPSHOT:compile
[INFO] |  |  |  +- com.mogujie.pricecenter:pricecenter-calculator:jar:1.0.0-SNAPSHOT:compile
[INFO] |  |  |  \- com.mogujie.pricecenter:pricecenter-api:jar:1.0.5-SNAPSHOT:compile
[INFO] |  |  +- com.mogujie.item:commons:jar:1.0.10-SNAPSHOT:compile
[INFO] |  |  |  +- commons-collections:commons-collections:jar:20040616:compile
[INFO] |  |  |  +- org.mybatis:mybatis:jar:3.2.8:compile
[INFO] |  |  |  \- org.mybatis:mybatis-spring:jar:1.2.2:compile
[INFO] |  |  +- org.aopalliance:com.springsource.org.aopalliance:jar:1.0.0:compile
[INFO] |  |  +- org.aspectj:aspectjrt:jar:1.8.0:compile
[INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.8.4:compile
[INFO] |  +- com.mogujie.service:member-api:jar:1.0.1-SNAPSHOT:compile
[INFO] |  |  +- javax.persistence:persistence-api:jar:1.0:compile
[INFO] |  |  \- com.mogujie.membersdk:membersdk:jar:0.0.20-SNAPSHOT:compile
[INFO] |  +- com.mogujie.service.hummer:hummer-api:jar:1.1.0.redpacket-SNAPSHOT:compile
[INFO] |  +- com.mogujie.service:trade.microservice.cart-api:jar:1.0.3-SNAPSHOT:compile
[INFO] |  |  +- org.apache.poi:poi:jar:3.2-FINAL:compile
[INFO] |  |  +- org.apache.maven.plugin-tools:maven-plugin-annotations:jar:3.1:compile
[INFO] |  |  |  \- org.apache.maven:maven-artifact:jar:3.0:compile
[INFO] |  |  |     \- org.codehaus.plexus:plexus-utils:jar:2.0.4:compile
[INFO] |  |  \- org.codehaus.mojo:animal-sniffer-annotations:jar:1.13:compile
[INFO] |  +- com.mogujie.trade:trade.base.order.constants:jar:1.0.12-SNAPSHOT:compile
[INFO] |  +- com.mogujie.ecommerce:metadata:jar:1.0-SNAPSHOT:compile
[INFO] |  |  \- com.thoughtworks.xstream:xstream:jar:1.4.8:compile
[INFO] |  |     +- xmlpull:xmlpull:jar:1.1.3.1:compile
[INFO] |  |     \- xpp3:xpp3_min:jar:1.1.4c:compile
[INFO] |  +- com.mogujie.darwin:darwin-client:jar:1.0.0-SNAPSHOT:compile
[INFO] |  |  +- com.mogujie.market:market-base:jar:1.0.0-SNAPSHOT:compile
[INFO] |  |  +- com.mogujie.mait:mait-center-client:jar:1.0.1-SNAPSHOT:compile
[INFO] |  |  |  +- org.apache.curator:curator-framework:jar:2.8.0:compile
[INFO] |  |  |  |  +- org.apache.curator:curator-client:jar:2.8.0:compile
[INFO] |  |  |  |  \- org.apache.zookeeper:zookeeper:jar:3.4.6:compile
[INFO] |  |  |  |     +- jline:jline:jar:0.9.94:compile
[INFO] |  |  |  |     \- io.netty:netty:jar:3.7.0.Final:compile
[INFO] |  |  |  \- org.apache.curator:curator-recipes:jar:2.8.0:compile
[INFO] |  |  \- com.mogujie.recsys:basedelivery:jar:1.0.0-SNAPSHOT:compile
[INFO] |  |     \- com.mogujie.recsys:apollo:jar:1.0.1-SNAPSHOT:compile
[INFO] |  |        \- com.mogujie.thrift:libthrift:jar:0.9.3-SNAPSHOT:compile
[INFO] |  +- com.mogujie.metabase:metabase-spring-client:jar:1.7.2:compile
[INFO] |  |  \- com.mogujie.metabase:metabase-client:jar:1.7.2:compile
[INFO] |  |     \- org.apache.httpcomponents:fluent-hc:jar:4.3.5:compile
[INFO] |  +- com.mogujie.metabase:metabase-admin:jar:1.7.2:compile
[INFO] |  |  \- com.mogujie.metabase:metabase-common:jar:1.7.2:compile
[INFO] |  |     +- com.fasterxml.jackson.core:jackson-core:jar:2.5.3:compile
[INFO] |  |     +- com.fasterxml.jackson.core:jackson-databind:jar:2.5.3:compile
[INFO] |  |     +- com.google.code.gson:gson:jar:2.3.1:compile
[INFO] |  |     \- org.apache.avro:avro:jar:1.7.7:compile
[INFO] |  |        +- com.thoughtworks.paranamer:paranamer:jar:2.3:compile
[INFO] |  |        +- org.xerial.snappy:snappy-java:jar:1.1.0.1:compile
[INFO] |  |        \- org.apache.commons:commons-compress:jar:1.4.1:compile
[INFO] |  |           \- org.tukaani:xz:jar:1.0:compile
[INFO] |  \- log4j:log4j:jar:1.2.17:compile
[INFO] +- com.mogujie.trade:cart-common:jar:1.0.0-SNAPSHOT:compile
[INFO] |  +- com.alibaba:fastjson:jar:1.2.6:compile
[INFO] |  +- com.mogujie.service:trade.service.base:jar:1.0.14-for-tradeorderservice:compile
[INFO] |  |  +- com.mogujie.corgi:corgi-client:jar:1.0.4:compile
[INFO] |  |  |  \- com.mogujie.corgi:corgi-common:jar:1.0.4:compile
[INFO] |  |  |     \- com.mogujie.corgi:corgi-net:jar:1.0.4:compile
[INFO] |  |  \- com.mogujie.trade:trade.switch:jar:1.0.2.1111switch:compile
[INFO] |  |     \- com.mogujie.tesla:tesla-dal-cache:jar:1.0.6.1.RELEASE:compile
[INFO] |  \- com.mogujie.trade:trade.base.response:jar:1.0.0-SNAPSHOT:compile
[INFO] +- ch.qos.logback:logback-core:jar:1.1.3:compile
[INFO] +- ch.qos.logback:logback-classic:jar:1.1.3:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.7.12:compile
[INFO] +- com.mogujie.trade:common-interceptors:jar:0.0.2-SNAPSHOT:compile
[INFO] |  +- commons-lang:commons-lang:jar:2.6:compile
[INFO] |  \- com.mogujie.dragon.bigdata:CDNBalance:jar:0.0.8-SNAPSHOT:compile
[INFO] |     +- com.alibaba:druid:jar:1.0.12:compile
[INFO] |     |  +- com.alibaba:jconsole:jar:1.8.0:system
[INFO] |     |  \- com.alibaba:tools:jar:1.8.0:system
[INFO] |     \- mysql:mysql-connector-java:jar:5.1.26:compile
[INFO] +- org.msgpack:msgpack:jar:0.6.9:compile
[INFO] |  +- com.googlecode.json-simple:json-simple:jar:1.1.1:compile
[INFO] |  \- org.javassist:javassist:jar:3.16.1-GA:compile
[INFO] +- security-filter:security-filter:jar:0.0.1-SNAPSHOT:compile
[INFO] |  \- junit:junit:jar:4.11:test
[INFO] |     \- org.hamcrest:hamcrest-core:jar:1.3:runtime
[INFO] +- com.mogujie.session:session-filter:jar:1.0.2.2:compile
[INFO] |  \- com.mogujie.session:session-common:jar:1.0.0-SNAPSHOT:compile
[INFO] +- com.mogujie:actionlet:jar:1.2-SNAPSHOT:compile
[INFO] |  +- org.jetbrains:annotations:jar:13.0:compile
[INFO] |  +- com.mogujie:actionlet-annotation:jar:1.0:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-jsonSchema:jar:2.6.3:compile
[INFO] |  |     +- com.fasterxml.jackson.core:jackson-annotations:jar:2.6.0:compile
[INFO] |  |     \- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  +- com.mogujie:annotation-cache:jar:1.0:compile
[INFO] |  \- com.mogujie:wt-pipeline:jar:1.0:compile
[INFO] +- com.mogujie:actionlet.mwp:jar:1.1-SNAPSHOT:compile
[INFO] |  +- com.mogujie:actionlet.session:jar:1.1-SNAPSHOT:compile
[INFO] |  \- com.mogujie:actionlet.mwp.api:jar:1.0:compile
[INFO] +- com.mogujie:muser-common:jar:1.0.2-SNAPSHOT:compile
[INFO] |  +- com.mogujie.sentry:sentry-client:jar:2.1.9:compile
[INFO] |  |  \- com.google.flatbuffers:flatbuffers-java:jar:1.2.0-SNAPSHOT:compile
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.3.5:compile
[INFO] |  |  \- org.apache.httpcomponents:httpcore:jar:4.3.2:compile
[INFO] |  +- org.apache.httpcomponents:httpasyncclient:jar:4.1:compile
[INFO] |  |  \- org.apache.httpcomponents:httpcore-nio:jar:4.4.1:compile
[INFO] |  \- commons-io:commons-io:jar:2.4:compile
[INFO] +- com.mogujie.tesla:tesla-client:jar:1.1.5.6:compile
[INFO] |  +- com.mogujie.tesla:furion-client:jar:1.2.6-SNAPSHOT:compile
[INFO] |  |  \- com.mogujie.tesla:furion-common:jar:1.2.6-SNAPSHOT:compile
[INFO] |  |     +- com.sleepycat:je:jar:6.1.5:compile
[INFO] |  |     \- org.mockito:mockito-core:jar:1.10.19:compile
[INFO] |  +- com.mogujie.tesla:tesla-core:jar:1.1.5.6:compile
[INFO] |  |  +- cglib:cglib-nodep:jar:3.1:compile
[INFO] |  |  +- com.esotericsoftware.reflectasm:reflectasm:jar:1.09:compile
[INFO] |  |  |  \- org.ow2.asm:asm:jar:4.0:compile
[INFO] |  |  +- io.netty:netty-all:jar:4.0.28.Final:compile
[INFO] |  |  +- net.jpountz.lz4:lz4:jar:1.2.0:compile
[INFO] |  |  +- de.javakaffee:kryo-serializers:jar:0.28:compile
[INFO] |  |  \- com.google.code.findbugs:jsr305:jar:3.0.0:compile
[INFO] |  \- com.mogujie.tesla:tesla-registry:jar:1.1.5.6:compile
[INFO] +- com.mogujie.tesla:tesla-spring-client:jar:1.1.5.6:compile
[INFO] |  \- com.mogujie.tesla:tesla-spring-config:jar:1.1.5.6:compile
[INFO] +- com.mogujie.tesla:tesla-spring-server:jar:1.1.5.6:compile
[INFO] |  \- com.mogujie.tesla:tesla-server:jar:1.1.5.6:compile
[INFO] |     +- com.mogujie.tesla:tesla-admin:jar:1.1.5.6:compile
[INFO] |     \- com.mogujie.service:tesla-registry-api:jar:1.1.1-SNAPSHOT:compile
[INFO] +- com.mogujie.tesla:tesla-spring-bootstrap:jar:1.1.5.6:compile
[INFO] |  +- org.slf4j:jul-to-slf4j:jar:1.7.12:compile
[INFO] |  +- org.slf4j:log4j-over-slf4j:jar:1.7.12:compile
[INFO] |  \- org.slf4j:jcl-over-slf4j:jar:1.7.12:compile
[INFO] +- com.mogujie.tesla:tesla-support-dependency:pom:1.0.6.1.RELEASE:compile
[INFO] |  +- com.mogujie.tesla:tesla-common:jar:1.0.6.1.RELEASE:compile
[INFO] |  +- com.mogujie.tesla:tesla-dal-counter:jar:1.0.6.1.RELEASE:compile
[INFO] |  +- com.mogujie.tesla:tesla-dal-db:jar:1.0.11:compile
[INFO] |  |  +- org.springframework:spring-jdbc:jar:4.0.6.RELEASE:compile
[INFO] |  |  |  \- org.springframework:spring-tx:jar:4.0.6.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-aspects:jar:4.0.6.RELEASE:compile
[INFO] |  +- com.mogujie.tesla:tesla-dal-file:jar:1.0.6.1.RELEASE:compile
[INFO] |  |  +- org.codehaus.jackson:jackson-core-asl:jar:1.9.13:compile
[INFO] |  |  \- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.13:compile
[INFO] |  +- com.mogujie.tesla:tesla-dal-hbase:jar:1.0.6.1.RELEASE:compile
[INFO] |  +- com.mogujie.tesla:tesla-dal-mongodb:jar:1.0.6.1.RELEASE:compile
[INFO] |  +- com.mogujie.tesla:tesla-dal-mq:jar:1.0.6.1.RELEASE:compile
[INFO] |  +- com.mogujie.tesla:tesla-dal-search:jar:1.0.6.1.RELEASE:compile
[INFO] |  +- com.mogujie.tesla:tesla-dal-sms:jar:1.0.6.1.RELEASE:compile
[INFO] |  +- com.mogujie.tesla:tesla-localcache:jar:1.0.6.1.RELEASE:compile
[INFO] |  |  +- com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:jar:1.4:compile
[INFO] |  |  \- net.sf.ehcache:ehcache:jar:2.8.3:compile
[INFO] |  +- com.mogujie.tesla:tesla-logging:jar:1.0.6.1.RELEASE:compile
[INFO] |  +- com.mogujie.tesla:tesla-monitor:jar:1.0.6.1.RELEASE:compile
[INFO] |  +- com.mogujie.tesla:tesla-security:jar:1.0.6.1.RELEASE:compile
[INFO] |  \- com.mogujie.tesla:tesla-service-loader:jar:1.0.6.1.RELEASE:compile
[INFO] +- com.mogujie.service:verify-api:jar:1.0-SNAPSHOT:compile
[INFO] |  \- com.mogujie.tesla:tesla-api:jar:1.1.4.2:compile
[INFO] +- commons-httpclient:commons-httpclient:jar:3.1:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.0.4:compile
[INFO] |  \- commons-codec:commons-codec:jar:1.9:compile
[INFO] +- com.mogujie.commons:utils:jar:0.0.1-SNAPSHOT:compile
[INFO] \- org.mogujie.trace:trace-lurkeragent:jar:1.0.5:compile
上面的依赖太多了,头晕目眩有木有~把自己想要看到的类过滤出来,过滤串使用groupId:artifactId:version的方式进行过滤,可以不写全,
对于以上的mvn依赖执行以下命令:
mvn dependency:tree -Dverbose -Dincludes=com.mogujie.trade:trade.base.response
[INFO] com.mogujie.trade:cart-web:war:1.0.0-SNAPSHOT
[INFO] +- com.mogujie.trade:cart-service:jar:1.0.0-SNAPSHOT:compile
[INFO] |  \- com.mogujie.service:trade.microservice.cart-api:jar:1.0.3-SNAPSHOT:compile
[INFO] |     \- (com.mogujie.trade:trade.base.response:jar:1.0.0-SNAPSHOT:compile - omitted for duplicate)
[INFO] \- com.mogujie.trade:cart-common:jar:1.0.0-SNAPSHOT:compile
[INFO]    \- com.mogujie.trade:trade.base.response:jar:1.0.0-SNAPSHOT:compile
这样可以很清晰地看到你想查找的那个类,这样便可以很方便地在导出的依赖文件中,查找问题相关的jar。确定这些jar是如何被依赖进来的,是直接依赖的还是通过传递依赖引入的,然后进行冲突排除即可.冲突排解有两种方法
(1)同一个jar包但groupId, artifactId不同,这种冲突只能通过设定依赖的 来进行排除。
  
    org.unitils  
    unitils-dbmaintainer  
    ${unitils.version} 
       
      
          
            dbunit  
            org.dbunit  
          
          
  
(2)需要的版本jar包依赖路径较长,根据maven依赖的原则,依赖路径越短越优先,可以把想要版本的依赖写成直接依赖,这样依赖路径就最短了.

最后再确认下问题是否存在即可.

你可能感兴趣的:(java,maven冲突)