也许有更好,更简单的方法,更干净的方法,但我认为你在这里有三个选择。
第一个选项
由于类实际上是可重用测试库的一部分(您在两个项目中都使用它),因此您可以简单地创建一个子项目,其中BaseTest是在src / main / java中定义的,而不是src / test / java中定义的。其他两个子项目的配置都将依赖于 。BaseTest
testing
testCompile
project('testing')
第二个选项(针对 Gradle 7.3 进行了更新)
在第二个选项中,您将在第一个项目中定义其他工件和配置:
configurations {
testClasses {
extendsFrom(testImplementation)
}
}
task testJar(type: Jar) {
archiveClassifier.set('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) {
archiveClassifier.set('test')
from sourceSets.test.output
}
artifacts {
testRuntime testJar
}
和
dependencies {
testCompile project(path: ':one', configuration: 'testRuntime')
}