Skip to content

Instantly share code, notes, and snippets.

@Hazer
Forked from cr7pt0gr4ph7/README.md
Created July 14, 2016 22:00
Show Gist options
  • Save Hazer/e8737a228a493d47c11e3c68e706b677 to your computer and use it in GitHub Desktop.
Save Hazer/e8737a228a493d47c11e3c68e706b677 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.

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