我遇到的情况是我的源代码同时包含测试和一些测试支持代码,以及通常的源代码。我希望能够将源代码和测试支持都发布为单独的 Artifact (每个 Artifact 都有自己的依赖关系集),但显然不是测试类。
我的目录结构如下所示:
src/main/...
src/test/...
src/testsupport/...
我像往常一样定义了一些依赖项:
dependencies {
compile 'com.sun.jersey:jersey-core:1.17.1'
... more compile dependencies...
testCompile 'com.google.code.gson:gson:2.1'
... more testCompile dependencies...
}
我还需要测试支持sourceSet:
sourceSets {
testSupport {
java {
srcDir 'src/testsupport/java'
}
resources {
srcDir 'src/testsupport/resources'
}
compileClasspath += sourceSets.test.compileClasspath
runtimeClasspath += sourceSets.test.runtimeClasspath
}
}
我必须将测试类路径添加到testSupport中,因为testsupport代码具有相同的依赖关系。
最后,要创建我的 Artifact ,我需要:
task createTestSupportArtifact (type: Jar) {
classifier = 'testsupport'
from sourceSets.testSupport.output
}
artifacts {
archives file: createTestSupportArtifact.archivePath, builtBy: createTestSupportArtifact
}
但是,在我的其他依赖于这些 Artifact 的存储库中,它没有为testsupport Artifact 引入任何依赖关系。我有一些理论来解释为什么(它没有它自己的POM或它不知道范围是在运行时测试的),但是没有什么具体的。
有更好的方法吗?也许聪明地使用
configurations
?
最佳答案
我认为您想在testSupport配置上使用extendsFrom
。我没有创建一组测试项目,但是从逻辑上讲,如果您的代码正常工作,但您的部门却没有,那么您在配置方面会遇到问题,而与sourceSets无关。
声明新的sourceSet时,将根据您的情况自动基于配置名称获得两个新配置:testSupportCompile
和testSupportRuntime
。尝试以下操作将您的常规测试部门与testSupport部门链接。
configurations {
testSupportCompile.extendsFrom testCompile
testSupportRuntime.extendsFrom testRuntime
}
Gradle在发布时肯定会使用配置,并且我猜想是因为您没有向testSupportXYZ配置中明确添加任何内容,所以它决定了以后需要支持 Artifact 时没有要解决的依赖项。上面的代码应以链接源的相同方式链接您的部门,并希望能解决您的问题。
关于dependencies - 使用依赖关系构建测试支持工件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17197885/