如何在多模块项目中访问 gradle 中的测试 jar
How to access tests jar in gradle in multi-module project
我有一个多模块项目。我需要从其中一个模块引用其他模块的测试 类。我试过这样配置:
// 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
项目的测试 类。 运行 Gradle 5.2.1 使用 Kotlin DSL。
所以上面描述的内容在命令行中有效,但在 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)
由于variant-aware依赖管理规则,上面看起来很像。但如果它是在插件中提取的,那么大部分都可以分解出来。
在消费者方面:
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)
我有一个多模块项目。我需要从其中一个模块引用其他模块的测试 类。我试过这样配置:
// 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
项目的测试 类。 运行 Gradle 5.2.1 使用 Kotlin DSL。
所以上面描述的内容在命令行中有效,但在 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)
由于variant-aware依赖管理规则,上面看起来很像。但如果它是在插件中提取的,那么大部分都可以分解出来。
在消费者方面:
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)