Rob Watts Rob Watts - 3 years ago 143
Java Question

How do I replace a dependency of a dependency in gradle?

I'm working with a Java project that is using a library that has a dependency on a library with a security vulnerability. Unfortunately, the updated version of the vulnerable library does not have the same group. Basically, the library

depends on
, but the vulnerability has been fixed in

In Gradle, how do I tell
to use
instead of

Answer Source

You can explicitly declare a dependency on like implementation("") in your build file. The dependency version you specified will be have precedence over the transitive dependency.

Another option might be to specify an exclude rule to make sure that the dependency is not brought in (see example)

I think the best pattern would we to use the tools provided from the Dependency Management section of the user guide. In this case, you should be able to use the resolutionStrategy API. You can substitute the desired dependency for the originally requested one.

This example configures a resolution rule for every Configuration:

configurations.all {
  resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    if ( == "org.vulnerable.dependency"
        && == "dependency"
        && details.requested.version == "1.0") {

The Gradle user guide also has an example where only the version is changed, and an example which is very similar to the above snippet (and your use case).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download