使用 Gradle 时如何排除传递依赖项的所有实例? [英] How do I exclude all instances of a transitive dependency when using Gradle?
问题描述
我的 gradle 项目使用 application
插件来构建一个 jar 文件.作为运行时传递依赖项的一部分,我最终引入了 org.slf4j:slf4j-log4j12
.(它在至少 5 或 6 个其他传递依赖项中被称为子传递依赖项 - 该项目使用的是 spring 和 hadoop,所以除了厨房水槽之外的所有东西都被拉进来了……不用等……那也是:)).
My gradle project uses the application
plugin to build a jar file. As part of the runtime transitive dependencies, I end up pulling in org.slf4j:slf4j-log4j12
. (It's referenced as a sub-transitive dependency in at least 5 or 6 other transitive dependencies - this project is using spring and hadoop, so everything but the kitchen sink is getting pulled in... no wait... that's there too :) ).
我想从我构建的 jar 中全局排除 slf4j-log4j12
jar.所以我试过这个:
I want to globally exclude the slf4j-log4j12
jar from my built jar. So I've tried this:
configurations {
runtime.exclude group: "org.slf4j", name: "slf4j-log4j12"
}
然而,这似乎排除了所有 org.slf4j
工件,包括 slf4j-api
.在调试模式下运行时,我看到如下几行:
However, this seems to exclude all org.slf4j
artifacts including slf4j-api
. When running under debug mode I see lines such as:
org.slf4j#slf4j-api is excluded from com.pivotal.gfxd:gfxd-demo-mapreduce:1.0(runtime).
org.slf4j#slf4j-simple is excluded from com.pivotal.gfxd:gfxd-demo-mapreduce:1.0(runtime).
org.slf4j#slf4j-log4j12 is excluded from org.apache.hadoop:hadoop-common:2.2.0(runtime).
我不想查找每个 slf4j-log4j12
传递依赖的源代码,然后在其中使用单独的 compile foo { exclude slf4j... }
语句我的 dependencies
块.
I do not want to have to look up the source of each slf4j-log4j12
transitive dependency and then have individual compile foo { exclude slf4j... }
statements in my dependencies
block.
更新:
我也试过这个:
configurations {
runtime.exclude name: "slf4j-log4j12"
}
最终从构建中排除所有内容!好像我指定了 group: "*"
.
Which ends up excluding everything from the build! As though I specified group: "*"
.
更新 2:
为此我使用 Gradle 1.10 版.
I'm using Gradle version 1.10 for this.
推荐答案
啊,下面的工作和我想要的一样:
Ah, the following works and does what I want:
configurations {
runtime.exclude group: "org.slf4j", module: "slf4j-log4j12"
}
似乎有一个 排除规则 只有两个属性 - group
和 module
.但是,上述语法并不阻止您将任意属性指定为谓词.尝试从单个依赖项中排除时,您不能指定任意属性.例如,这失败了:
It seems that an Exclude Rule only has two attributes - group
and module
. However, the above syntax doesn't prevent you from specifying any arbitrary property as a predicate. When trying to exclude from an individual dependency you cannot specify arbitrary properties. For example, this fails:
dependencies {
compile ('org.springframework.data:spring-data-hadoop-core:2.0.0.M4-hadoop22') {
exclude group: "org.slf4j", name: "slf4j-log4j12"
}
}
与
No such property: name for class: org.gradle.api.internal.artifacts.DefaultExcludeRule
因此,即使您可以使用 group:
和 name:
指定依赖项,也不能使用 name:
指定排除项!?!
So even though you can specify a dependency with a group:
and name:
you can't specify an exclusion with a name:
!?!
也许是一个单独的问题,但是究竟是什么模块呢?我可以理解 groupId:artifactId:version 的 Maven 概念,我理解它在 Gradle 中转换为 group:name:version.但是,我怎么知道特定 Maven 工件属于哪个模块(用 gradle 来说)?
Perhaps a separate question, but what exactly is a module then? I can understand the Maven notion of groupId:artifactId:version, which I understand translates to group:name:version in Gradle. But then, how do I know what module (in gradle-speak) a particular Maven artifact belongs to?
这篇关于使用 Gradle 时如何排除传递依赖项的所有实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!