gradlebuild.gradledependency-resolution

how does gradle resolve conflicting dependency versions


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?


Solution

  • 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