gradle - Gradle-排除配置的依赖项,但不继承配置

标签 gradle dependencies

使用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.



那么还有其他方法可以实现我想要实现的目标吗?

想法:
  • 创建一个不从testCompile扩展的新conf myTestRuntime,并将其用于我的测试类路径。
  • 但是,我必须为testCompile和myTestRuntime复制所有依赖项。
  • 删除配置级别的排除项。对于除了testRuntime之外的所有conf,请遍历依赖关系并手动删除log4j(或在log4j上添加dep级排除)。
  • 这甚至可能吗? Configuration.allDependencies为只读。
  • 最佳答案

    目前,我已经设法解决了这个问题,但是我仍然欢迎任何更好的解决方案。

    这是我最终要做的事情:

  • 仅为log4j添加新配置:
    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"
    }
    
  • 将log4j配置添加到测试的类路径中:
    test {
        classpath += configurations.log4j
    }
    

  • 这样,即使将log4j.jar从testRuntime配置中排除,也可以将它放到类路径中。

    关于gradle - Gradle-排除配置的依赖项,但不继承配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9918568/

    相关文章:

    grails - Grails 3 set-version替换

    gradle - 从 gradle/groovy 中的路径创建目录结构

    android库项目: R. id无法解析或不是字段

    dependencies - 如何找出存储库中的哪些 RPM 依赖于特定的 RPM?

    带有时间戳的 Gradle distZip 存档

    android - FaSTLane android 构建 : index. android.bundle 丢失

    android-studio - Bintray 响应冲突,错误代码 409

    ruby - 降级 gem 时,bundler 是否可以自动降级 gem 的依赖项?

    Maven 构建 : How to resolve the workspace artifacts without installing them to repo and without using m2eclipse

    java - 重新创建没有依赖项的 GitHub 项目 (Java)