Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

Using ResolutionStrategy.force(Object...):

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.

@WindySha

This comment has been minimized.

Copy link

@WindySha WindySha commented Jun 9, 2018

This help me a lot, thank you

@wangchauyan

This comment has been minimized.

Copy link

@wangchauyan wangchauyan commented Jul 11, 2018

Good sharing, thank you so much.

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