依赖冲突是指项目依赖的某一个jar包,有多个不一样的版本,于是形成类包版本冲突java
依赖冲突很常常是类包之间的间接依赖引发的。每一个显式声明的类包都会依赖于一些其它的隐式类包,这些隐式的类包会被maven间接引入进来,从而形成类包冲突spring
首先查看产生依赖冲突的类jar,其次找出咱们不想要的依赖类jar,手工将其排除在外就能够了。具体执行步骤以下express
a、经过dependency:tree是命令来检查版本冲突maven
mvn -Dverbose dependency:tree
复制代码
当敲入上述命令时,控制台会出现形以下内容ide
[INFO] org.example:hello:jar:1.0-SNAPSHOT
[INFO] +- org.springframework:spring-context:jar:5.2.7.RELEASE:compile
[INFO] | +- (org.springframework:spring-aop:jar:5.2.7.RELEASE:compile - omitted for conflict with 5.2.0.RELEASE)
[INFO] | +- org.springframework:spring-beans:jar:5.2.7.RELEASE:compile
[INFO] | | \- (org.springframework:spring-core:jar:5.2.7.RELEASE:compile - omitted for duplicate)
[INFO] | +- org.springframework:spring-core:jar:5.2.7.RELEASE:compile
[INFO] | | \- org.springframework:spring-jcl:jar:5.2.7.RELEASE:compile
[INFO] | \- org.springframework:spring-expression:jar:5.2.7.RELEASE:compile
[INFO] | \- (org.springframework:spring-core:jar:5.2.7.RELEASE:compile - omitted for duplicate)
[INFO] \- org.springframework:spring-aop:jar:5.2.0.RELEASE:compile
[INFO] +- (org.springframework:spring-beans:jar:5.2.0.RELEASE:compile - omitted for conflict with 5.2.7.RELEASE)
[INFO] \- (org.springframework:spring-core:jar:5.2.0.RELEASE:compile - omitted for conflict with 5.2.7.RELEASE)
复制代码
其中omitted for duplicate表示有jar包被重复依赖,最后写着omitted for conflict with xxx的,说明和别的jar包版本冲突了,而该行的jar包不会被引入。好比上面有一行最后写着omitted for conflict with 5.2.7.RELEASE,表示spring-core 5.2.0版本不会被项目引用,而spring-core 5.2.7版本会被项目引用idea
b、若是是idea,能够安装maven helper插件来检查依赖冲突spa
maven helper插件安装成功,点开pom.xml会发现多了一个Dependency Analyzer视图,以下 插件
上图说明有3个jar存在冲突,点击冲突的jar,能够查看和哪一个jar产生冲突,以下图 code
项目的pom.xml形以下cdn
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
</dependencies>
复制代码
经过查看依赖树,咱们知道项目会引用5.2.7.RELEASE的spring core jar包,而不会引用5.2.0的jar包,若是咱们想用5.2.0版本的spring core包,咱们该如何作?
a、使用第一声明者优先原则
谁先定义的就用谁的传递依赖,即在pom.xml文件自上而下,先声明的jar坐标,就先引用该jar的传递依赖。所以咱们若是要使用5.2.0版本的spring core包,咱们能够改为以下声明
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
</dependencies>
复制代码
查看依赖树
[INFO] org.example:hello:jar:1.0-SNAPSHOT
[INFO] +- org.springframework:spring-aop:jar:5.2.0.RELEASE:compile
[INFO] | +- org.springframework:spring-beans:jar:5.2.0.RELEASE:compile
[INFO] | | \- (org.springframework:spring-core:jar:5.2.0.RELEASE:compile - omitted for duplicate)
[INFO] | \- org.springframework:spring-core:jar:5.2.0.RELEASE:compile
[INFO] | \- org.springframework:spring-jcl:jar:5.2.0.RELEASE:compile
[INFO] \- org.springframework:spring-context:jar:5.2.7.RELEASE:compile
[INFO] +- (org.springframework:spring-aop:jar:5.2.7.RELEASE:compile - omitted for conflict with 5.2.0.RELEASE)
[INFO] +- (org.springframework:spring-beans:jar:5.2.7.RELEASE:compile - omitted for conflict with 5.2.0.RELEASE)
[INFO] +- (org.springframework:spring-core:jar:5.2.7.RELEASE:compile - omitted for conflict with 5.2.0.RELEASE)
[INFO] \- org.springframework:spring-expression:jar:5.2.7.RELEASE:compile
[INFO] \- (org.springframework:spring-core:jar:5.2.7.RELEASE:compile - omitted for conflict with 5.2.0.RELEASE)
复制代码
经过依赖树,咱们能够看到项目已经引入5.2.0版本的spring core包
b、使用路径近者优先原则
即直接依赖级别高于传递依赖。所以咱们能够在最早的pom.xml添加以下内容
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
</dependencies>
复制代码
c、排除依赖
排除依赖若是是idea,可使用maven helper插件进行排除。点开pom.xml,切换到Dependency Analyzer视图,选择All Dependencies as Tree,点击要排除的jar,右键会出现Execlude选项,以下
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>spring-core</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
</dependencies>
复制代码
四、版本锁定
使用dependencyManagement 进行版本锁定,dependencyManagement能够统一管理项目的版本号,确保应用的各个项目的依赖和版本一致。
若是咱们项目中只想使用spring core 5.2.0的包,pom.xml能够改成以下
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
</dependencies>
复制代码
综上就是maven如何排查依赖冲突以及解决方法,对于排查依赖我的比较推荐使用maven helper插件,至于解决依赖冲突我的推荐使用版本锁定的方法,此外dependencyManagement只是声明依赖,并不自动实现引入,所以子项目须要显示的声明须要用的依赖