使用 Gradle 添加提供的依赖以测试类路径

tom*_*ozb 3 java gradle build.gradle

我有provided如下配置的依赖范围。我的问题是,提供的依赖项在测试运行时不可见。如何配置它以保持依赖项provided但在测试类路径上可用?

apply plugin: 'java'

configurations {
    provided
}

sourceSets {
    main {
        compileClasspath += configurations.provided
    }
}

dependencies {
    provided 'com.google.guava:guava:18.0'
    provided 'org.apache.commons:commons-lang3:3.3.2'

    // Tests
    testCompile 'junit:junit:4.11'
    testCompile 'org.assertj:assertj-core:1.7.0'

    // Additional test compile dependencies
    testCompile 'joda-time:joda-time:2.2'
}
Run Code Online (Sandbox Code Playgroud)

一种解决方案是添加具有testCompile范围的 joda-time 库之类的依赖项,但我不想复制任何条目。我相信它可以通过适当的配置来实现。

Mar*_*ira 6

有两种方法可以做到这一点。首先,让testRuntime配置从provided.

configurations {
    provided
    testRuntime.extendsFrom(provided)
}
Run Code Online (Sandbox Code Playgroud)

其次,您可以将provided配置添加到test任务的类路径中。

test {
    classpath += configurations.provided
}
Run Code Online (Sandbox Code Playgroud)