Skip to content

Instantly share code, notes, and snippets.

@petarov
Forked from cr7pt0gr4ph7/README.md
Created January 22, 2016 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petarov/82a0a11e40c74993cd53 to your computer and use it in GitHub Desktop.
Save petarov/82a0a11e40c74993cd53 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