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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30728533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:04:50  来源:igfitidea点击:

Gradle: Override transitive dependency by version classifier

javamavengradleguavadependency-management

提问by dinup24

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.

我的项目中声明的依赖项之一对'com.google.guava:guava:15.0'. 但是我部署在 WAS/Weblogic 上的应用程序由于已在'com.google.guava:guava:15.0:cdi1.0'. (相同版本,但使用分类器)我需要告诉 gradle 在构建和打包期间使用这个 jar。我试图弄清楚我们如何使用特定于 jar 的版本分类器来覆盖这种传递依赖。

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.

  1. 显式添加了依赖项:compile 'com.google.guava:guava:15.0:cdi1.0'. 但是这两个 jar 都包含在最终的 WAR 中。
  2. 显式添加依赖项并定义解析策略:

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

    即使这样也没有用。

  3. 定义了一个解决策略来检查和更改版本。

    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"
            }
        }
    }
    

    即使这样也没有用。

Need your suggestions on how this issue can be tackled.

需要您就如何解决此问题提出建议。

回答by Rene Groeschke

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:

目前,当涉及到 resolutionStrategies 时,尚未考虑分类器。在声明依赖项并显式添加 guava cdi1.0 版本时,您的解决方法可能会排除可传递的 guava lib:

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

回答by Abhijit Mazumder

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

如果其他 jar 指向相同的依赖项,这将不起作用。排除依赖的 Sureshot 方法

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

回答by Sobvan

I came across a more elegant approach which is simply:

我遇到了一种更优雅的方法,它很简单:

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

Explanation

解释

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

设置force = true依赖项告诉 gradle 在版本冲突的情况下使用指定的版本

回答by koppor

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

Gradle 4.5.1 具有DependencySubstitutions功能。这是替换依赖项的示例:

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'
            }
        }
    }
}

回答by uptoyou

Since force = trueis deprecated, relevant solution is to use strictly(...)version, e.g.:

由于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")
    }
}

P.S. tested on Gradle 6.3

PS 在 Gradle 6.3 上测试