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
}
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.
Using ExternalDependency.force
:
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.
Good sharing, thank you so much.