简体   繁体   English

如何在 Gradle 中遮蔽传递依赖?

[英]How to shade a transitive dependency in Gradle?

Is there a way to shadow a particular (transitive) dependency in Gradle?有没有办法在 Gradle 中隐藏特定(传递)依赖项? My situation: I have a project that depends directly on com.amazonaws:aws-java-sdk-emr:1.10.33 and org.apache.hadoop:hadoop-aws:2.7.1 , but hadoop-aws in turns depends on com.amazonaws:aws-java-sdk-emr:1.7.4 which screws the final JAR, but I need both anyway.我的情况:我有一个项目直接依赖于com.amazonaws:aws-java-sdk-emr:1.10.33org.apache.hadoop:hadoop-aws:2.7.1 ,但hadoop-aws反过来又依赖于com.amazonaws:aws-java-sdk-emr:1.7.4了最终的 JAR,但无论如何我都需要。

Is it currently possible to do something like this?目前有可能做这样的事情吗?

shadowJar {
    relocate('com.amazonaws', 'shadowedstuff.awsjdk') {
        include(dependency('com.amazonaws:aws-java-sdk:1.7.4'))
    }
}

Or a not-so-dirty workaround for it?或者一个不太脏的解决方法?

Thanks!谢谢!

NOTE : shading the aws-sdk which my projects depends on directly is not an option.注意:对我的项目直接依赖的aws-sdk进行着色不是一种选择。 This is a simplification and in the original setup some reflection is going on.这是一种简化,在原始设置中进行了一些反射。

Yes, you can use the shadow plugin for Gradle to which has a very similar syntax to your example:是的,您可以使用 Gradle 的shadow插件,它的语法与您的示例非常相似

// Configuring Filtering for Relocation
shadowJar {
   relocate('junit.textui', 'a') {
       exclude 'junit.textui.TestRunner'
   }
   relocate('junit.framework', 'b') {
       include 'junit.framework.Test*'
   }
}

Apologies if I've misunderstood your situation and it is in fact more complex but it looks like the exclusion can just be provided in the dependency declaration?如果我误解了您的情况,并且实际上更复杂,但看起来排除只能在依赖声明中提供,我深表歉意?

dependencies {
    ...
    compile('org.apache.hadoop:hadoop-aws:2.7.1') {
        exclude group: 'com.amazonaws', module: 'aws-java-sdk'
    }
    ...
}

You can try the resolutionStrategy ( https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html ) to force a particular version.您可以尝试使用resolutionStrategy ( https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html ) 来强制特定版本。

Example from a projet:来自项目的示例:

configurations {
    testImplementation.extendsFrom compileOnly

    all {
        resolutionStrategy {
            // Force jackson 2.4.4 for Spark
            force 'com.fasterxml.jackson.core:jackson-core:2.4.4', 'com.fasterxml.jackson.core:jackson-databind:2.4.4', 'com.fasterxml.jackson.core:jackson-annotations:2.4.4'
            force 'com.google.guava:guava:23.6-jre'
        }
    }
}

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

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