Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: Override transitive dependency by version classifier

One of the dependencies declared in my project has a transitive dependency on 'com.google.guava:guava:15.0'. But my application deployed on WAS/Weblogic doesn't work due to a CDI issue which has been fixed in 'com.google.guava:guava:15.0:cdi1.0'. (same version, but with classifier) I need to tell gradle to use this jar during build and packaging. I am trying to figure on how we can ovrride this transitive dependency with a jar specific version classifier.

Tried the following approches:

  1. Added the dependency explicitly: compile 'com.google.guava:guava:15.0:cdi1.0'. But both jars got included in the resultant WAR.
  2. Added the dependency explicitly and defined a resolution strategy:

    configurations.all {     resolutionStrategy {         force 'com.google.guava:guava:15.0:cdi1.0'     } } 

    Even this didn't work.

  3. Defined a resolution strategy to check and change the version.

    configurations.all {      resolutionStrategy.eachDependency { DependencyResolveDetails details ->          if (details.requested.group + ":" + details.requested.name == 'com.google.guava:guava') {             details.useVersion "15.0:cdi1.0"             //details.useTarget "com.google.guava:guava:15.0:cdi1.0"         }     } } 

    Even this didn't work.

Need your suggestions on how this issue can be tackled.

like image 322
dinup24 Avatar asked Jun 09 '15 09:06

dinup24


People also ask

How do I change transitive dependency version in Gradle?

To override the version of a transitive dependency in Gradle, exclude it from the declared dependency that pulls it in, and then explicitly declare the version that you prefer to use in your build.

How do I override the version of a transitive dependency?

How do you do this if the wrong dependency is a transitive dependency? By taking advantage of Maven's nearest definition logic, developers can override the version of a dependency by declaring it on the root pom. xml file.

How do you remove transitive dependency in Gradle?

When you specify a dependency in your build script, you can provide an exclude rule at the same time telling Gradle not to pull in the specified transitive dependency. For example, say we have a Gradle project that depends on Google's Guava library, or more specifically com. google. guava:guava:30.1.

Does Gradle support transitive dependencies?

Gradle automatically resolves those additional modules, so called transitive dependencies. If needed, you can customize the behavior the handling of transitive dependencies to your project's requirements. Projects with tens or hundreds of declared dependencies can easily suffer from dependency hell.


1 Answers

Currently classifiers are not yet taken into account when it comes to resolutionStrategies. A workaround for you might excluding the transitive Guava library when declaring your dependencies and adding the Guava cdi1.0 version explicitly:

dependencies {     compile ("org.acme:someDependency:1.0"){         exclude group: 'com.google.guava', module: 'guava'     }            compile "com.google.guava:guava:15.0:cdi1.0" } 
like image 165
Rene Groeschke Avatar answered Sep 25 '22 16:09

Rene Groeschke