我正在使用Gradle来构建和测试我的项目。我有两个项目:
ProjA contains src\test\java\BaseTest.java
ProjB contains src\test\java\MyTest.java
MyTest extends BaseTest
当我运行ProjB.gradle
时,如何让它看到ProjA的BaseTest
课程?
我尝试添加:
dependencies {
testCompile project('ProjA')
}
但它不起作用。
答案 0 :(得分:17)
也许有更好,更简单的方法,更清洁的方式,但我认为你有三种选择。
由于BaseTest
是一个实际上是可重用测试库的一部分(在两个项目中都使用它),因此您可以简单地创建一个testing
子项目,其中BaseTest在src /中定义main / java而不是src / test / java。其他两个子项目的testCompile
配置都依赖于project('testing')
。
在第二个选项中,您将在第一个项目中定义另一个工件和配置:
configurations {
testClasses {
extendsFrom(testRuntime)
}
}
task testJar(type: Jar) {
classifier = 'test'
from sourceSets.test.output
}
// add the jar generated by the testJar task to the testClasses dependency
artifacts {
testClasses testJar
}
并且您将依赖于第二个项目中的此配置:
dependencies {
testCompile project(path: ':ProjA', configuration: 'testClasses')
}
基本上与第二个相同,只是它没有为第一个项目添加新配置:
task testJar(type: Jar) {
classifier = 'test'
from sourceSets.test.output
}
artifacts {
testRuntime testJar
}
和
dependencies {
testCompile project(path: ':one', configuration: 'testRuntime')
}