且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

gradle如何解决冲突的依赖版本

更新时间:2022-06-25 06:11:12

根据此Gradle文档管理传递依赖项,如果您没有为传递依赖项解析指定任何特定的约束,最高版本的ATLAS mo应该选择dules:

According to this Gradle documentation Managing Transitive Dependencies, in case you don't specify any specific constraints for transitive dependencies resolution, the highest version of ATLAS modules should be selected:


当Gradle尝试将对模块版本的依赖项解析为所有依赖项声明以及版本,所有可传递依赖项以及所有该模块的依赖关系约束已考虑在内。选择满足所有条件的最高版本。

When Gradle attempts to resolve a dependency to a module version, all dependency declarations with version, all transitive dependencies and all dependency constraints for that module are taken into consideration. The highest version that matches all conditions is selected.

您可以使用下面的简单多项目构建来快速测试此行为:

You can quickly test this behavior with simple multi-project build below:

settings.gradle

rootProject.name = 'demo'
include "A", "B", "C"

build.gradle

subprojects{
    apply plugin: "java"
    repositories{
        mavenCentral()
    }
}
project(':A') {
    dependencies{
        implementation 'commons-io:commons-io:1.2'
    }
}
project(':B') {
    dependencies{
        implementation project(":A")
        implementation 'commons-io:commons-io:2.0'
    }
}
project(':C') {
    dependencies{
        implementation project(":B")
        implementation 'commons-io:commons-io:2.6'
    }
}

然后您可以检查哪个版本的 commons-io 已被选择,它是 2.6

You can then check which version of commons-io has been selected, which is 2.6 :

./ gradlew C:依赖项

runtimeClasspath - Runtime classpath of source set 'main'.
+--- project :B
|    +--- project :A
|    |    \--- commons-io:commons-io:1.2 -> 2.6
|    \--- commons-io:commons-io:2.0 -> 2.6
\--- commons-io:commons-io:2.6