要从另一个子项目添加对测试源集的简单依赖,我可以这样做:
testCompile project(':subFoo1').sourceSets.test.output
Run Code Online (Sandbox Code Playgroud)
此解决方案有效,但在许多情况下,并不打算将整个源集添加为依赖项.例如,我想只使用测试数据构建器,在这种情况下,像test-logback.xml(和常规测试)这样的文件会污染主模块中的测试类路径.
我尝试了测试JAR的想法(可以有过滤内容,但作为依赖项有问题)和一些组合eachFileRecurse,但没有运气.
我的问题.如何只添加给定源集的子集(例如,只有具有匹配**/*Builder.*模式的构建器的类)作为另一个子项目中的testCompile依赖项?
你会想要的东西:
upstream/build.gradle:
apply plugin: "java"
task testJar(type: Jar) {
classifier = "tests"
from sourceSets.test.output
exclude "**/*Test.class"
}
artifacts {
testRuntime testJar
}
Run Code Online (Sandbox Code Playgroud)
downstream/build.gradle:
apply plugin: "java"
dependencies {
testCompile project(path: ":upstream", configuration: "testRuntime")
}
Run Code Online (Sandbox Code Playgroud)
除了使用之外testRuntime,您还可以声明(例如configurations { testFixture })并使用自定义配置,这将使您可以更好地控制将哪些外部依赖项传递给下游项目.另一种选择是为要传递的测试代码部分声明一个单独的源集.(这也可以为您提供单独的编译和运行时配置.)
PS:进入另一个项目的对象模型(例如project(':subFoo1').sourceSets.test.output)是有问题的,应该尽可能避免.