我的项目是一个遵循标准约定(src/main、src/test ..)的 Java 项目。我正在尝试编写一个简单的 Gradle 任务,它将发出两个 jar 文件:
Gradle 发出的包含我的应用程序代码的“默认”jar
一个仅包含测试类和运行它们所需的依赖项的 jar(例如 Junit)。此 jar 不应包含仅由非测试代码需要的依赖项(例如下面示例中的 Guava)。
我尝试在下面的 testJar 任务中使用“testCompile”,但它也会选择非测试 Jar。因此,我创建了一个单独的配置,仅列出测试依赖项,但现在正如我所料,测试无法编译,因为“testCompile”配置未正确填充。
我还没有在 DependencyHandler API 中看到一种方法(以我对 Gradle 的有限了解,它是在 dependencies block 中定义 DSL 的)将一种配置分配给另一种配置。 我如何将 testJars 依赖集添加到 testCompile 集(而不是替换)以便一切“正常工作”?我在下面的代码中放置了一条评论,我假设缺少间隙是,但我可能错了!
apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
mavenCentral()
}
configurations {
testJars
}
dependencies {
compile group: 'com.google.guava', name: 'guava', version: '14.0.1'
testJars group: 'junit', name: 'junit', version: '4.+'
//"testCompile append testJars"??
}
task testJar(type: Jar) {
classifier = 'tests'
from sourceSets.test.output
from { configurations.testJars.collect { it.isDirectory() ? it : zipTree(it) } }
}
最佳答案
假设自定义 testJars
配置是内置 testCompile
配置的子集,您应该能够更改您的 configurations
阻止这个:
configurations {
testJars
testCompile {
extendsFrom testJars
}
}
这将扩充 testCompile
以包含 testJars
的所有内容。我认为这将为您提供编译测试代码所需的类路径。
关于java - 从另一个配置附加依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24287370/