[英]How to access tests jar in gradle in multi-module project
I have multimodule project.我有多模块项目。 From one of the modules I need to refer the test classes from other module.
我需要从其中一个模块引用其他模块的测试类。 I tried to configure like this:
我试着这样配置:
// 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)
And I'm trying to refer to that configuration from other project:我试图从其他项目中引用该配置:
dependencies {
// other dependencies ommited
api(project(":core"))
testImplementation(project(path = ":core", configuration = "testArtifact"))
}
But this configuration doesn't work.但是这个配置不起作用。 The compilation of other project is failing as it doesn't see the required tests classes from
core
project.其他项目的编译失败,因为它没有从
core
项目中看到所需的测试类。 The dependency insight:依赖洞察:
./gradlew :service:dependencyInsight --dependency core --configuration testCompileClasspath
It gives following:它给出了以下内容:
project :core
variant "apiElements" [
org.gradle.usage = java-api
]
variant "testArtifact" [
Requested attributes not found in the selected variant:
org.gradle.usage = java-api
]
I'm struggling to understand how to make configuration to work so that I can compile the service
project's test classes.我正在努力理解如何使配置工作以便我可以编译
service
项目的测试类。 Running on Gradle 5.2.1 with Kotlin DSL.使用 Kotlin DSL 在 Gradle 5.2.1 上运行。
So what is described above works on the command line but not in the IDE.因此,上述内容适用于命令行,但不适用于 IDE。
Here is a variation, that builds on the variant-aware dependency management:这是一个基于变体感知依赖管理的变体:
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)
Because of the variant-aware dependency management rules, the above looks like a lot.由于变量感知依赖管理规则,上面看起来很多。 But if it was extracted in a plugin, most of this could be factored out.
但是如果它是在插件中提取的,那么大部分都可以被分解出来。
And on the consumer side:而在消费者方面:
testImplementation(project(":lib")) {
capabilities {
// Indicate we want a variant with a specific capability
requireCapability("org.test:lib-test")
}
}
Note that this requires Gradle 5.3.1.请注意,这需要 Gradle 5.3.1。 The import of the project in IntelliJ 2019.1 properly wires up the dependencies and test code can compile in the IDE.
在 IntelliJ 2019.1 中导入项目正确连接依赖项,测试代码可以在 IDE 中编译。
Which IDE ?哪个IDE? It works for me both in terminal and IDE : I use IntelliJ (2019.3) and Gradle (6.0.1, not Kotlin DSL)
它在终端和 IDE 中都适用于我:我使用 IntelliJ(2019.3)和 Gradle(6.0.1,而不是 Kotlin DSL)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.