Gradle: Override transitive dependency by version classifier

笑着哭i 提交于 2019-12-18 11:40:11

问题


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.


回答1:


currently classifiers are not yet taken into account when it comes to resolutionStrategies. A workaround for you might excluding the transitive guava lib 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"
}



回答2:


I came across a more elegant approach which is simply:

compile ("com.google.guava:guava:15.0:cdi1.0") {
  force = true
}

Explanation

Setting force = true for a dependency tells gradle to use the specified version in case of a version conflict




回答3:


Gradle 4.5.1 has the function DependencySubstitutions. Here an example to replace a dependency:

configurations.each {
    c -> c.resolutionStrategy.dependencySubstitution {
        all { DependencySubstitution dependency ->
            if (dependency.requested.group == 'org.json') {
                dependency.useTarget 'com.vaadin.external.google:android-json:0.0.20131108.vaadin1'
            }
        }
    }
}



回答4:


This will not work if the same dependency is pointed by some other jar. Sureshot way to exclude the dependency

configurations {
 all*.exclude group: 'com.google.guava', module:'guava-jdk5'
}


来源:https://stackoverflow.com/questions/30728533/gradle-override-transitive-dependency-by-version-classifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!