简体   繁体   English

Gradle:通过版本分类器覆盖传递依赖

[英]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' .我的项目中声明的依赖项之一对'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' .但是由于'com.google.guava:guava:15.0:cdi1.0'中已修复的 CDI 问题,我在 WAS/Weblogic 上部署的应用程序无法运行。 (same version, but with classifier) I need to tell gradle to use this jar during build and packaging. (相同版本,但带有分类器)我需要告诉 gradle 在构建和打包过程中使用这个 jar。 I am trying to figure out how we can override this transitive dependency with a jar specific version classifier.我试图弄清楚我们如何使用 jar 特定版本分类器来覆盖这种传递依赖。

Tried the following approaches:尝试了以下方法:

  1. Added the dependency explicitly: compile 'com.google.guava:guava:15.0:cdi1.0' .显式添加了依赖项: compile 'com.google.guava:guava:15.0:cdi1.0' But both jars got included in the resultant WAR.但是 jars 都包含在生成的 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.即使这样也没有用。

  1. 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.需要您就如何解决此问题提出建议。

Currently classifiers are not yet taken into account when it comes to resolutionStrategies .目前,当涉及到resolutionStrategies时,分类器尚未考虑在内。 A workaround for you might excluding the transitive Guava library when declaring your dependencies and adding the Guava cdi1.0 version explicitly:在声明依赖项并显式添加 Guava cdi1.0版本时,您可能会排除可传递的 Guava 库,这是一种解决方法:

dependencies {
    compile ("org.acme:someDependency:1.0"){
        exclude group: 'com.google.guava', module: 'guava'
    }       
    compile "com.google.guava:guava:15.0:cdi1.0"
}

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为依赖项设置force = true告诉 gradle 在版本冲突的情况下使用指定的版本

Gradle 4.5.1 has the function DependencySubstitutions . Gradle 4.5.1 具有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'
            }
        }
    }
}

This will not work if the same dependency is pointed by some other jar.如果其他 jar 指向相同的依赖项,这将不起作用。 Sureshot way to exclude the dependency排除依赖的 Sureshot 方法

configurations {
 all*.exclude group: 'com.google.guava', module:'guava-jdk5'
}
implementation( group: 'commons-codec', name: 'commons-codec'){
      version{
        strictly "[1.15]"
      }
   }

This works for me with gradle 6.6.1这对我来说适用于 gradle 6.6.1

The documentation link for strictly can found here https://docs.gradle.org/current/userguide/rich_versions.html#rich-version-constraints严格的文档链接可以在这里找到https://docs.gradle.org/current/userguide/rich_versions.html#rich-version-constraints

Since force = true is deprecated, relevant solution is to use strictly(...) version, eg:由于force = true已弃用,相关解决方案是使用strictly(...)版本,例如:

dependencies {
    // no need to exclude transitive spring-data-relational from this dependency
    implementation("org.springframework.data", "spring-data-r2dbc", "1.1.0.RC1")

    implementation("org.springframework.data", "spring-data-relational").version {
        strictly("2.0.0.RC1")
    }
}

PS tested on Gradle 6.3 PS 在 Gradle 6.3 上测试

Try this:尝试这个:

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->

        // https://docs.gradle.org/current/userguide/resolution_rules.html
        if (details.requested.group == 'com.google.guava' && details.requested.name == 'guava') {
            details.useVersion '15.0:cdi1.0'
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM