我的项目是遵循标准约定的Java项目(src / main,src / test ..)。我正在尝试编写一个简单的Gradle任务,它会发出两个jar文件:
Gradle发出的“默认”jar包含我的应用代码
一个jar,只包含运行它们所需的测试类和依赖项(例如Junit)。此jar不应包含非测试代码仅需要的依赖项(例如下面示例中的Guava)。
我尝试在下面的testJar任务中使用“testCompile”,但这也会选择非测试Jars。所以我创建了一个单独的配置,列出了测试依赖项,但现在正如我所料,由于'testCompile'配置未正确填充,因此无法编译测试。
我没有在DependencyHandler API中看到一种方法(我对Gradle的知识有限,是依赖块中定义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) } }
}
答案 0 :(得分:2)
假设自定义testJars
配置是内置testCompile
配置的子集,您应该能够将configurations
块更改为:
configurations {
testJars
testCompile {
extendsFrom testJars
}
}
这会增加testCompile
以包含testJars
所拥有的所有内容。我认为这将为您提供编译测试代码所需的类路径。