使用Gradle 1.0里程碑8。
我的项目使用slf4j + Logback进行日志记录,因此我想防止log4j上的任何传递dep污染我的类路径。因此,我添加了全局排除,如下所示:
configurations {
all*.exclude group: "log4j", module: "log4j"
}
但是,我正在使用一个测试库(
hadoop-minicluster
),它对log4j具有运行时依赖关系,因此现在我需要为我的测试运行时允许一个log4j依赖关系。我尝试在log4j上添加直接依赖项:testRuntime group: "log4j", name: "log4j", version: "1.2.15"
并编辑我的排除代码(有点hack):
configurations.findAll {!it.name.endsWith('testRuntime')}.each { conf ->
conf.exclude group: "log4j", module: "log4j"
}
但这是行不通的。将排除项添加到testCompile conf会自动将其也添加到所有继承的配置中,包括testRuntime。而且似乎这种排除甚至覆盖了我添加的显式依赖项。
看来这是Gradle的预期行为。从the docs:
If you define an exclude for a particular configuration, the excluded transitive dependency will be filtered for all dependencies when resolving this configuration or any inheriting configuration.
那么还有其他方法可以实现我想要实现的目标吗?
想法:
myTestRuntime
,并将其用于我的测试类路径。最佳答案
目前,我已经设法解决了这个问题,但是我仍然欢迎任何更好的解决方案。
这是我最终要做的事情:
log4j(group: 'log4j', name: 'log4j', version: '1.2.15') {
transitive = false
}
configurations.findAll {!it.name.endsWith('log4j')}.each { conf ->
conf.exclude group: "log4j", module: "log4j"
}
test {
classpath += configurations.log4j
}
这样,即使将log4j.jar从testRuntime配置中排除,也可以将它放到类路径中。
关于gradle - Gradle-排除配置的依赖项,但不继承配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9918568/