一 版本冲突例子
![](https://img-blog.csdnimg.cn/20191123195000676.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw==,size_16,color_FFFFFF,t_70)
二 解决冲突
- 查看依赖报告
- 排除传递性依赖
- 强制一个版本(Gradle默认依赖高版本)
三 解决方法
1 修改默认解决策略
configurations.all{
resolutionStrategy{
failOnVersionConflict()
}
}
通过这个配置可以发现版本冲突。
2 排除传递性依赖
compile('org.hibernate:hibernate-core:3.6.3.Final'){
exclude group:"org.slf4j",module:"slf4j-api"
}
3 强制指定一个版本
configurations.all{
resolutionStratety{
force 'org.slf4j:slf4j-api:1.7.24'
}
}
四 实战
1 添加一个hibernate依赖
dependencies {
compile 'org.hibernate:hibernate-core:3.6.3.Final'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
2 查看依赖关系
![](https://img-blog.csdnimg.cn/20191123195356470.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw==,size_16,color_FFFFFF,t_70)
3 另外一种查看版本的方法
![](https://img-blog.csdnimg.cn/20191123195423614.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw==,size_16,color_FFFFFF,t_70)
结果输出如下
compile - Dependencies for source set 'main' (deprecated, use 'implementation ' instead).
+--- org.hibernate:hibernate-core:3.6.3.Final
| +--- antlr:antlr:2.7.6
| +--- commons-collections:commons-collections:3.1
| +--- dom4j:dom4j:1.6.1
| +--- org.hibernate:hibernate-commons-annotations:3.2.0.Final
| | \--- org.slf4j:slf4j-api:1.5.8 -> 1.7.25
| +--- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.0.Final
| +--- javax.transaction:jta:1.1
| \--- org.slf4j:slf4j-api:1.6.1 -> 1.7.25
\--- ch.qos.logback:logback-classic:1.2.3
+--- ch.qos.logback:logback-core:1.2.3
\--- org.slf4j:slf4j-api:1.7.25
可以清楚的看到版本被gradle强制变换为最高版本:org.slf4j:slf4j-api:1.7.25
4 版本出现了冲突,我们制定策略,强制构建失败
// 强制构建失败
configurations.all{
resolutionStrategy{
failOnVersionConflict()
}
}
repositories {
// 配置多个仓库,按顺序查找,找到为止
// 本地仓库
mavenLocal()
// 中央仓库
mavenCentral()
}
dependencies {
// 添加hibernate依赖
compile 'org.hibernate:hibernate-core:3.6.3.Final'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
下面是构建失败的结果
FAILURE: Build failed with an exception.
* What went wrong:
Could not resolve all dependencies for configuration ':compileClasspath'.
> A conflict was found between the following modules:
- org.slf4j:slf4j-api:1.6.1
- org.slf4j:slf4j-api:1.7.25
- org.slf4j:slf4j-api:1.5.8
5 通过排除传递性依赖解决上面构建失败问题
sourceCompatibility = 1.8
// 强制构建失败
configurations.all{
resolutionStrategy{
failOnVersionConflict()
}
}
repositories {
// 配置多个仓库,按顺序查找,找到为止
// 本地仓库
mavenLocal()
// 中央仓库
mavenCentral()
}
dependencies {
// 排除hibernate对slf4j-api传递性依赖
compile ('org.hibernate:hibernate-core:3.6.3.Final'){
exclude group:"org.slf4j",module:"slf4j-api"
}
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
查看传递性依赖是否消失
![](https://img-blog.csdnimg.cn/2019112319563384.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw==,size_16,color_FFFFFF,t_70)
再次构建可以成功。
6 强制指定一个版本解决版本冲突
sourceCompatibility = 1.8
configurations.all{
resolutionStrategy{
// 强制构建失败
failOnVersionConflict()
// 强制指定一个版本解决版本冲突
force 'org.slf4j:slf4j-api:1.7.24'
}
}
repositories {
// 配置多个仓库,按顺序查找,找到为止
// 本地仓库
mavenLocal()
// 中央仓库
mavenCentral()
}
dependencies {
// 添加hibernate依赖
compile ('org.hibernate:hibernate-core:3.6.3.Final'){
// exclude group:"org.slf4j",module:"slf4j-api"
}
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
7 查看版本依赖关系
![](https://img-blog.csdnimg.cn/20191123195711123.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw==,size_16,color_FFFFFF,t_70)