如何跨Gradle项目共享测试类?

我正在使用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') } 

但它没有用。

也许有更好,更简单的方法,更清洁的方式,但我认为你有三个选择。

第一种选择

由于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') }