Skip to content

Instantly share code, notes, and snippets.

@cr7pt0gr4ph7
Last active October 13, 2019 15:02
Show Gist options
  • Save cr7pt0gr4ph7/6cd6339eabc2056bc3e7 to your computer and use it in GitHub Desktop.
Save cr7pt0gr4ph7/6cd6339eabc2056bc3e7 to your computer and use it in GitHub Desktop.
Gradle Dependency Resolution

Gradle Dependency Resolution

Normal Gradle behavior

The default behavior of Gradle to pick the newest version also applies if a lower version has been declared locally, but another dependency transitively pulls in a newer version. This is in contrast with Maven, where a locally declared version will always win.

For example, if your build.gradle specifies the dependency org.springframework:spring-tx:3.2.3.RELEASE, and another dependency declares 4.0.5.RELEASE as a transitive dependency, then 4.0.5.RELEASE will take precedence:

dependencies {
    compile("org.springframework.data:spring-data-hadoop:2.0.0.RELEASE")
    compile("org.springframework:spring-tx:3.2.3.RELEASE")
    // will select org.springframework:spring-tx:4.0.5.RELEASE
}

Forcing specific versions

One option to force Gradle to use the specified version, thus obtaining a behavior similar to Maven, is to use ResolutionStrategy.force(Object...):

configurations.all {
    resolutionStrategy {
        force "org.springframework:spring-tx:3.2.3.RELEASE"
    }
}

When there are multiple conflicting ResolutionStrategy.force calls, the last one to be executed wins:

configurations.all {
    resolutionStrategy {
        force "org.springframework:spring-tx:3.2.5.RELEASE"
        force "org.springframework:spring-tx:3.2.7.RELEASE"
        force "org.springframework:spring-tx:3.2.6.RELEASE"
        // will select org.springframework:spring-tx:3.2.6.RELEASE (the last one)
    }
}

Interestingly, force is implemented as a conflict resolution strategy, too (see ModuleForcingResolveRule and DefaultResolutionStrategy). It is executed before the other resolution strategies.

The other option is to use force on the dependency itself:

dependencies {
    compile("org.springframework.data:spring-data-hadoop:2.0.0.RELEASE")
    compile("org.springframework:spring-tx:3.2.3.RELEASE") {
        force = true
    }
}

This is perhaps the option that is most similar to Maven.

Note that if both ResolutionStrategy.force and ExternalDependency.force are used, the former takes precedence over the latter.

@WindySha
Copy link

WindySha commented Jun 9, 2018

This help me a lot, thank you

@wangchauyan
Copy link

Good sharing, thank you so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment