how does gradle resolve conflicting dependency versions

不问归期 提交于 2020-03-23 15:41:04

问题


Say I have 3 modules with 3 different build.gradle property files.

Module A v1 has the following entries in build.gradle

ATLAS_VERSION = 1

Module B v1 has the following entries in build.gradle

ATLAS_VERSION = 2
MODULE_A_VERSION = 1

Module C v1 has the following entries in its build.gradle

ATLAS_VERSION = 3
MODULE_B_VERSION = 1

So my question is: what ATLAS version will be resolved during runtime?


回答1:


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:

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'
    }
}

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

./gradlew C:dependencies

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


来源:https://stackoverflow.com/questions/56135182/how-does-gradle-resolve-conflicting-dependency-versions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!