一:前提
在使用maven开发项目的过程中,经常会遇到jar包重复加载或者jar包冲突的问题的,但是由于有些jar是由于maven的依赖 加载自动加载进来的,而不是开发者自己配置的,特别是当项目中pom中配置的jar包依赖本身很多时,开发者靠自己的经验,有时很难找出是哪个jar的加载导致加载了多余的依赖jar,从而产生冲突。
二.解决方法:
目前我知道有两种解决方法,如下:
1:在pom.xml文件中,在Dependency Hierarchy(依赖列表)中查看jar包的依赖层次关系。查到相同jar包名称相同,版本不同的。可以右键自己不需要的jar包,选择 “Exclude”,然后保存pom.xml文件。这样就可以了。此时查看pom.xml文件,可以发现在对应的<dependency>中有了<exclusions>,如下
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.2</version> <exclusions> <exclusion> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </exclusion> </exclusions> </dependency> |
下面来看第二种方法
2.使用maven插件Maven Enforcer Plugin
使用方法:在pom.xml文件中直接引入即可,如下:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.3.1</version>
<executions>
<execution>
<id>enforce</id>
<configuration>
<rules>
<DependencyConvergence />
</rules>
</configuration>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
然后执行maven命令 “validate”会打出日志告诉我们哪些jar包冲突。
[WARNING] [WARNING] Some problems were encountered while building the effective settings [WARNING] Unrecognised tag: 'id' (position: START_TAG seen ...<profiles>\n <id>... @199:8) @ D:\Apache\apache-maven-3.5.0\conf\settings.xml, line 199, column 8 [WARNING] Unrecognised tag: 'id' (position: START_TAG seen ...<profiles>\n <id>... @199:8) @ D:\Apache\apache-maven-3.5.0\conf\settings.xml, line 199, column 8 [WARNING] [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building test-spring-boot 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-enforcer-plugin:1.3.1:enforce (enforce) @ test-spring-boot --- [WARNING] Dependency convergence error for org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1 paths to dependency are: +-cn.com.chnsys:test-spring-boot:0.0.1-SNAPSHOT +-org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1 and +-cn.com.chnsys:test-spring-boot:0.0.1-SNAPSHOT +-com.github.pagehelper:pagehelper-spring-boot-starter:1.2.2 +-org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.DependencyConvergence failed with message: Failed while enforcing releasability the error(s) are [ Dependency convergence error for org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1 paths to dependency are: +-cn.com.chnsys:test-spring-boot:0.0.1-SNAPSHOT +-org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1 and +-cn.com.chnsys:test-spring-boot:0.0.1-SNAPSHOT +-com.github.pagehelper:pagehelper-spring-boot-starter:1.2.2 +-org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0 ] [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.833 s [INFO] Finished at: 2018-05-22T15:51:01+08:00 [INFO] Final Memory: 15M/352M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:1.3.1:enforce (enforce) on project test-spring-boot: Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed. -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException |
这个方法,直接去右键Exclude不需要的版本即可。最好再去检查一下需要的jar包版本对不对