在 Gradle 中,从除一个之外的所有依赖项中排除传递依赖项

chr*_*son 5 dependencies gradle

我正在尝试使用 JUnit 和SLF4J Test 测试记录器的行为,这是“SLF4J 的测试实现,它将日志消息存储在内存中并提供检索它们的方法”

来自 SLF4J 测试文档:

SLF4J Test 应该是测试类路径上唯一的 SLF4J 实现

我有一些将 SLF4J 作为传递依赖项的依赖项。我试图将 SLF4J 从测试配置中的所有依赖项中排除,但我仍然需要它来进行SLF4J Test

我可以使用下面的代码将 SLF4J 从所有内容中排除,但这显然也将其从我需要它的 SLF4J 测试中排除。

configurations {
    testCompile.exclude group: "org.slf4j"
}
Run Code Online (Sandbox Code Playgroud)

由于 SLF4J 是我的许多其他依赖项(包括 Spring Boot)的传递依赖项,因此遍历并单独将其从所有依赖项中排除是不实际的(或不可能的?)。

有没有一种(相对轻松的)方法可以从除需要它的依赖项之外的所有依赖项中排除传递依赖项?

VaL*_*VaL 2

SLF4J 测试指南指出,您的测试类路径上应该只有一个SLF4J 实现。这意味着您应该slf4j-test仅在当前项目中添加依赖项并排除其他依赖项(例如 logback、log4j 等)。

例如,Spring Boot 默认使用 Logback,以下是描述如何使用的 gradle 脚本片段slf4j-test

configurations.testCompile {
    exclude group: 'ch.qos.logback', module: 'logback-classic'
}

dependencies {
    compile(group: 'org.springframework.boot', name: 'spring-boot-starter')
    ...

    testCompile(group: 'org.springframework.boot', name: 'spring-boot-starter-test')
    testCompile(group: 'com.github.valfirst', name: 'slf4j-test', version: '1.3.0')
}
Run Code Online (Sandbox Code Playgroud)