Skip to content

Commit d1ce72b

Browse files
committed
archrules runtime configurations should inherit attributes from the corresponding source set's runtime classpath (in addition to dependencies)
this prevent unresolvable dependency ambiguity when using archrules alongside other plugins that produce and consume custom variants increase minimum supported gradle version to 9.1.0
1 parent 0a1c6a7 commit d1ce72b

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

nebula-archrules-gradle-plugin/src/main/kotlin/com/netflix/nebula/archrules/gradle/ArchrulesRunnerPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class ArchrulesRunnerPlugin : Plugin<Project> {
104104
project.configurations.getByName("archRules"),
105105
configurations.getByName(sourceSet.runtimeClasspathConfigurationName)
106106
)
107+
attributes.addAllLater(project.configurations.getByName(sourceSet.runtimeClasspathConfigurationName).attributes)
107108
attributes {
108109
attribute(ArchRuleAttribute.ARCH_RULES_ATTRIBUTE, project.objects.named(ARCH_RULES))
109110
attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(ARCH_RULES))

nebula-archrules-gradle-plugin/src/test/kotlin/com/netflix/nebula/archrules/gradle/ArchrulesRunnerPluginTest.kt

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import nebula.test.dsl.rootProject
1111
import nebula.test.dsl.run
1212
import nebula.test.dsl.settings
1313
import nebula.test.dsl.src
14+
import nebula.test.dsl.subProject
1415
import nebula.test.dsl.test
1516
import nebula.test.dsl.testProject
1617
import org.gradle.kotlin.dsl.findByType
@@ -46,7 +47,7 @@ class ArchrulesRunnerPluginTest {
4647
) {
4748
properties {
4849
buildCache(true)
49-
property("org.gradle.configuration-cache", "true")
50+
configurationCache(true)
5051
}
5152
settings {
5253
name("consumer")
@@ -182,7 +183,7 @@ class ArchrulesRunnerPluginTest {
182183
setupConsumerProject()
183184
}
184185

185-
val result = runner.run("outgoingVariants") {
186+
val result = runner.run("outgoingVariants", "--stacktrace") {
186187
withGradleVersion(gradleVersion.version)
187188
forwardOutput()
188189
}
@@ -633,6 +634,83 @@ archRules {
633634
assertThat(deprecationResult!!.rule.priority).isEqualTo(Priority.LOW)
634635
}
635636

637+
/**
638+
* This test is for making sure archrules will interop with other plugins that create and consume multiple variants
639+
*/
640+
@Test
641+
fun `archrules runtime classpaths inherit sourceset runtime attributes`() {
642+
val runner = testProject(projectDir) {
643+
properties {
644+
buildCache(true)
645+
configurationCache(true)
646+
}
647+
subProject("multi-variant-library") {
648+
plugins {
649+
id("java-library")
650+
}
651+
javaToolchain(17)
652+
rawBuildScript(
653+
// language=kotlin
654+
"""
655+
val myAttribute = Attribute.of("com.example.my-attribute", String::class.java)
656+
val otherSourceSet = java.sourceSets.create("other2")
657+
val otherJar = project.tasks.register<Jar>("otherJar") {
658+
archiveClassifier.set("v2")
659+
from(otherSourceSet.output)
660+
}
661+
configurations.named("runtimeElements") {
662+
attributes {
663+
attribute(myAttribute, "v1")
664+
}
665+
}
666+
667+
configurations.consumable("customRuntimeElements") {
668+
attributes {
669+
attribute(myAttribute, "v2")
670+
attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements.JAR))
671+
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.EXTERNAL))
672+
attribute(Usage.USAGE_ATTRIBUTE, objects.named( Usage.JAVA_RUNTIME))
673+
attribute(Category.CATEGORY_ATTRIBUTE, objects.named( Category.LIBRARY))
674+
attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 17)
675+
}
676+
}
677+
artifacts {
678+
add("customRuntimeElements", otherJar)
679+
}
680+
"""
681+
)
682+
}
683+
subProject("consumer") {
684+
plugins {
685+
id("java")
686+
id("com.netflix.nebula.archrules.runner")
687+
}
688+
dependencies(
689+
"""implementation(project(":multi-variant-library"))"""
690+
)
691+
// language=kotlin
692+
rawBuildScript(
693+
"""
694+
val myAttribute = Attribute.of("com.example.my-attribute", String::class.java)
695+
configurations.named("runtimeClasspath") {
696+
attributes {
697+
attribute(myAttribute, "v2")
698+
}
699+
}
700+
configurations.named("testRuntimeClasspath") {
701+
attributes {
702+
attribute(myAttribute, "v2")
703+
}
704+
}
705+
"""
706+
)
707+
}
708+
}
709+
710+
val result = runner.run("archRulesConsoleReport", "--stacktrace")
711+
assertThat(result.task(":consumer:archRulesConsoleReport")).hasOutcome(TaskOutcome.SUCCESS)
712+
}
713+
636714
private fun containsInOrder(actual: String, vararg expected: String) {
637715
var i = 0
638716
for (e in expected) {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.netflix.nebula.archrules.gradle
22

33
enum class SupportedGradleVersion(val version: String) {
4-
MIN("9.0.0"), MAX("9.2.0")
5-
}
4+
MIN("9.1.0"), MAX("9.3.1")
5+
}

0 commit comments

Comments
 (0)