我的 gradle 子项目之一的测试源中有一些实用程序文件,并且想在其他子项目中使用它们。
我的“源”子项目称为 core
,而使用它的子项目称为 tem
。
我尝试迁移并集成 following example :
In your Server project:
configurations {
testArtifacts.extendsFrom testCompile
}
task testJar(type: Jar) {
classifier "test"
from sourceSets.test.output
}
artifacts {
testArtifacts testJar
}
In your ServerWeb project:
testCompile project(path: ":Server", configuration: 'testArtifacts')
据我所知,正在进行对话。我将以下内容添加到我的 core.gradle.kts
中:
val testConfig = configurations.create("testArtifacts") {
extendsFrom(configurations["testCompile"])
}
tasks.register("testJar", Jar::class.java) {
classifier += "test"
from(sourceSets["test"].output)
}
artifacts {
add("testArtifacts", tasks.named<Jar>("testJar") )
}
并尝试在 tem.gradle.kts
中引用它:
testImplementation(project(":core", "testArtifacts"))
它可以编译,但我仍然无法从核心访问这些类。
我在哪里错过了什么?
最佳答案
你的大部分代码应该没问题
但是你必须为jar定义classesDirs
tasks.register<Jar>("testJar") {
dependsOn("testClasses")
archiveBaseName.set("${project.name}-test")
from(sourceSets["test"].output.classesDirs)
}
我还添加了依赖于 testClasses 以确保类已编译。
您可以通过执行 testJar 任务来测试该 jar 是否正常。然后验证生成的 jar 包含您的类。如果你在 from 方法调用中犯了错误,那么你会得到空 jar 。
关于gradle - 使用其他子项目的 testCompile 输出 (Gradle Kotlin DSL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53335892/