我的 gradle 项目使用 application
插件来构建一个 jar 文件。作为运行时传递依赖的一部分,我最终引入了 org.slf4j:slf4j-log4j12
。 (它在至少 5 或 6 个其他传递依赖项中被引用为子传递依赖项 - 这个项目使用 spring 和 hadoop,所以除了厨房水槽之外的所有东西都被拉进来......不用等待......那也是 :) )。
我想从我构建的 jar 中全局排除 slf4j-log4j12
jar。所以我试过这个:
configurations {
runtime.exclude group: "org.slf4j", name: "slf4j-log4j12"
}
但是,这似乎排除了所有 org.slf4j
工件,包括slf4j-api
。在 Debug模式下运行时,我看到如下行:
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
block 。
更新:
我也试过这个:
configurations {
runtime.exclude name: "slf4j-log4j12"
}
最终将一切排除在构建之外!好像我指定了 group: "*"
.
更新 2:
我为此使用 Gradle 1.10 版。
最佳答案
啊,下面的作品和我想要的:
configurations {
runtime.exclude group: "org.slf4j", module: "slf4j-log4j12"
}
似乎 Exclude Rule只有两个属性 - group
和 module
。
因此,对于仅排除单个依赖项,我们可以执行以下操作:
dependencies {
compile ('org.springframework.data:spring-data-hadoop-core:2.0.0.M4-hadoop22') {
exclude group: "org.slf4j", module: "slf4j-log4j12"
}
}
但是,上述语法并不妨碍您将任意属性指定为谓词。当试图从单个依赖项中排除时,您不能指定任意属性。例如,这会失败:
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:
指定排除项!?!
也许是一个单独的问题,那么究竟是什么模块?我可以理解 groupId:artifactId:version 的 Maven 概念,我理解它在 Gradle 中转换为 group:name:version。但是,我怎么知道一个特定的 Maven 工件属于哪个模块(在 gradle 中)?
关于java - 使用 Gradle 时如何排除传递依赖项的所有实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21764128/