0

I want to make sure any transitive dependencies (ie dependency of my dependencies) are using the correct version without pulling in the dependency itself.

For instance my project depends on B (and other dependencies) which depends on C. I want C to always be v2.

I tried this code

constraints {
    implementation('C') {
        version {
            require '2'
        }
        because 'I cant use previous versions'
    }
}

I am specifically using constraints because that seems to be gradle's recommended way of dealing with transitive properties.

However, my dependency report looks like:

runtimeClasspath

+--- B
|    \--- C:2
+--- C:2

whereas without the code above, it was just

+--- B
     \--- C:1.X

Meaning the v2 of the C library also got added to my runtimeClasspath directly.

Is there any gradle code that just ensure that my transitive dependencies are set to a particular version and NOT also add the library version as a direct dependency?

Thanks

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
jbu
  • 15,831
  • 29
  • 82
  • 105

1 Answers1

0

Gradle dependency constraints do exactly what you are looking for.

Even if the dependency is listed in the output of gradlew dependencies, it's not strictly included in the build. Notice how dependency C is marked with (c), which clearly states it's only a constraint and not a strict requirement:

+--- B
|    \--- C:1.X -> 2
\--- C:2 -> 2 (c)

(c) - dependency constraint

In fact, it's only listed because at least one other dependency requires it: should you have defined a constraint on a dependency D that was not required transitively by any dependency, it wouldn't ever have been shown.

Excerpt from the official Gradle documentation:

The version definition for commons-codec:1.11 is only taken into account if commons-codec is brought in as transitive dependency, since commons-codec is not defined as dependency in the project. Otherwise, the constraint has no effect.

Thibault Seisel
  • 1,197
  • 11
  • 23