我有多模块项目。从其中一个模块中,我需要从其他模块中引用测试类。我试图这样配置:
// core project
val testJar by tasks.registering(Jar::class) {
archiveClassifier.set("tests")
from(project.the<SourceSetContainer>()["test"].output)
}
val testArtifact by configurations.creating
artifacts.add(testArtifact.name, testJar)
我正在尝试参考其他项目的配置:
dependencies {
// other dependencies ommited
api(project(":core"))
testImplementation(project(path = ":core", configuration = "testArtifact"))
}
但是这种配置不起作用。其他项目的汇编失败了,因为它没有看到core
项目所需的测试类。依赖性见解:
./gradlew :service:dependencyInsight --dependency core --configuration testCompileClasspath
它给出以下内容:
project :core
variant "apiElements" [
org.gradle.usage = java-api
]
variant "testArtifact" [
Requested attributes not found in the selected variant:
org.gradle.usage = java-api
]
我正在努力了解如何使配置工作,以便我可以编译service
项目的测试类。用Kotlin DSL在Gradle 5.2.1上运行。
因此,上面描述的内容在命令行上有效,但在IDE中不起作用。
这是一个变体,它建立在变体感知依赖关系管理上:
plugins {
`java-library`
}
repositories {
mavenCentral()
}
group = "org.test"
version = "1.0"
val testJar by tasks.registering(Jar::class) {
archiveClassifier.set("tests")
from(project.the<SourceSetContainer>()["test"].output)
}
// Create a configuration for runtime
val testRuntimeElements by configurations.creating {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class, Usage.JAVA_RUNTIME_JARS))
}
outgoing {
// Indicate a different capability (defaults to group:name:version)
capability("org.test:lib-test:$version")
}
}
// Second configuration declaration, this is because of the API vs runtime difference Gradle makes and rules around valid multiple variant selection
val testApiElements by configurations.creating {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
// API instead of runtime usage
attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class, Usage.JAVA_API_JARS))
}
outgoing {
// Same capability
capability("org.test:lib-test:$version")
}
}
artifacts.add(testRuntimeElements.name, testJar)
artifacts.add(testApiElements.name, testJar)
由于具有变化的依赖性管理规则,上面看起来很多。但是,如果将其提取在插件中,则大多数可能会纳入。
在消费者方面:
testImplementation(project(":lib")) {
capabilities {
// Indicate we want a variant with a specific capability
requireCapability("org.test:lib-test")
}
}
请注意,这需要Gradle 5.3.1。该项目在Intellij 2019.1中的导入适当地介绍了依赖项,并且测试代码可以在IDE中编译。
哪个IDE?它在终端和IDE中都对我有用:我使用Intellij(2019.3)和Gradle(6.0.1,而不是Kotlin DSL)