From 6e9bdf15aabdeb5a3bc6effe4440a97e509ef30b Mon Sep 17 00:00:00 2001 From: Goooler Date: Sat, 7 Nov 2026 15:20:27 +0800 Subject: [PATCH] Add target JVM version attribute lazily without afterEvaluate Use `attributeProvider` for `org.gradle.jvm.version` of `shadowRuntimeElements` so the JVM version is not read eagerly (#2086) and the configuration is not mutated after it may have been locked by other plugins (#2054). `attributeProvider` must always provide a value, so the attribute is always added now: setting `addTargetJvmVersionAttribute` to `false` or calling `java.disableAutoTargetJvm()` makes it fall back to `java.targetCompatibility`. --- CHANGELOG.md | 4 + docs/publishing/README.md | 8 +- .../gradle/plugins/shadow/PublishingTest.kt | 23 +++--- .../gradle/plugins/shadow/ShadowExtension.kt | 7 +- .../gradle/plugins/shadow/ShadowJavaPlugin.kt | 80 ++++++++----------- 5 files changed, 57 insertions(+), 65 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18b1cccf62..a818a15523 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,10 @@ to reference the shadowed JAR only, or set `generateSourcesJar = false` on `shadowJar` to opt out. - The `shadow` component, and the `java` component when `addShadowVariantIntoJavaComponent` is enabled (the default), now publish an additional shadowed sources variant (e.g. `--all-sources.jar`). +- **POTENTIALLY BREAKING:** Always add the `org.gradle.jvm.version` attribute to the shadowed variant lazily, and remove the last `afterEvaluate` usage. + This fixes the conflicts when other plugins lock the `shadowRuntimeElements` configuration early (#2054), without eagerly locking `toolchain.languageVersion` (#2086). + - Setting `addTargetJvmVersionAttribute` to `false` no longer removes the attribute, the value falls back to `java.targetCompatibility` instead of being inferred from the compile tasks. + - The value falls back to `java.targetCompatibility` as well when `java.disableAutoTargetJvm()` is called, instead of being skipped. ### Deprecated diff --git a/docs/publishing/README.md b/docs/publishing/README.md index cc756a35f5..b2ddcb36dd 100644 --- a/docs/publishing/README.md +++ b/docs/publishing/README.md @@ -106,9 +106,11 @@ the `shadow` extension to `false`. If you want to publish the standard JAR only, } ``` -The target JVM version attribute (`org.gradle.jvm.version`) of the shadowed variant is added by default, it is useful -for consumers to select the correct variant based on their target JVM version. But it may cause issues in some cases, -you can disable this by setting the `addTargetJvmVersionAttribute` property in the `shadow` extension to `false`: +The target JVM version attribute (`org.gradle.jvm.version`) of the shadowed variant is always added, it is useful +for consumers to select the correct variant based on their target JVM version. By default, the value is inferred from +the compile tasks (e.g. `options.release` of `compileJava`), the same as the `java` component does. If it causes issues +in some cases, you can set the `addTargetJvmVersionAttribute` property in the `shadow` extension to `false`, then the +value falls back to `java.targetCompatibility`: === ":material-language-kotlin: build.gradle.kts" diff --git a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/PublishingTest.kt b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/PublishingTest.kt index bd7c5a0026..2ecd9e8062 100644 --- a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/PublishingTest.kt +++ b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/PublishingTest.kt @@ -139,7 +139,7 @@ class PublishingTest : BasePluginTest() { } @Test // #1665 - fun dontInjectTargetJvmVersionWhenAutoTargetJvmDisabled() { + fun fallBackToTargetCompatibilityWhenAutoTargetJvmDisabled() { projectScript.appendText( publishConfiguration( projectBlock = @@ -158,20 +158,16 @@ class PublishingTest : BasePluginTest() { ), ) - val result = publish(infoArgument) + publish() - assertThat(result.output) - .contains( - "Cannot set the target JVM version to Int.MAX_VALUE when `java.autoTargetJvmDisabled` is enabled or in other cases.", - ) assertShadowVariantCommon( "my/maven-all/1.0/maven-all-1.0.module", - variantAttrs = shadowVariantAttrsWithoutTargetJvm, + variantAttrs = shadowVariantAttrs, ) } @Test - fun dontInjectTargetJvmVersionWhenOptingOut() { + fun useTargetCompatibilityWhenOptingOut() { projectScript.appendText( publishConfiguration( projectBlock = @@ -179,6 +175,9 @@ class PublishingTest : BasePluginTest() { |shadow { | addTargetJvmVersionAttribute = false |} + |tasks.named('compileJava') { + | options.release = 8 + |} """ .trimMargin(), shadowBlock = @@ -190,15 +189,11 @@ class PublishingTest : BasePluginTest() { ), ) - val result = publish(infoArgument) + publish() - assertThat(result.output) - .contains( - "Skipping setting org.gradle.jvm.version attribute for shadowRuntimeElements configuration.", - ) assertShadowVariantCommon( "my/maven-all/1.0/maven-all-1.0.module", - variantAttrs = shadowVariantAttrsWithoutTargetJvm, + variantAttrs = shadowVariantAttrs, ) } diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowExtension.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowExtension.kt index 784dc4db74..d73a439089 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowExtension.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowExtension.kt @@ -15,10 +15,15 @@ public interface ShadowExtension { public val addShadowVariantIntoJavaComponent: Property /** - * If `true`, adds a [TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE] attribute to the Gradle + * The [TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE] attribute is always added to the Gradle * Module Metadata of the shadowed JAR. This affects how consumers resolve the published artifact * based on the target JVM version. * + * If `true`, the value is inferred from the compile tasks (e.g. `options.release` of + * `compileJava`), the same as the `java` component does. If `false`, or the value can't be + * inferred (e.g. `java.disableAutoTargetJvm()` is called), the value falls back to + * `java.targetCompatibility`. + * * Defaults to `true`. */ public val addTargetJvmVersionAttribute: Property diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.kt index 50150b7943..aa1cf7263d 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.kt @@ -75,21 +75,40 @@ constructor(private val softwareComponentFactory: SoftwareComponentFactory) : Pl configurations.named(COMPILE_CLASSPATH_CONFIGURATION_NAME) { compileClasspath -> compileClasspath.extendsFrom(shadowConfig) } - val shadowRuntimeElements = - registerConsumableConfiguration(SHADOW_RUNTIME_ELEMENTS_CONFIGURATION_NAME) { - extendsFrom(shadowConfig) - attributes { attrs -> - attrs.attribute( - Category.CATEGORY_ATTRIBUTE, - objects.named(Category::class.java, Category.LIBRARY), - ) - attrs.attribute( - LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, - objects.named(LibraryElements::class.java, LibraryElements.JAR), - ) - } - outgoing.artifact(tasks.shadowJar) + registerConsumableConfiguration(SHADOW_RUNTIME_ELEMENTS_CONFIGURATION_NAME) { + extendsFrom(shadowConfig) + attributes { attrs -> + attrs.attribute( + Category.CATEGORY_ATTRIBUTE, + objects.named(Category::class.java, Category.LIBRARY), + ) + attrs.attribute( + LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, + objects.named(LibraryElements::class.java, LibraryElements.JAR), + ) + // Must be lazy, reading the JVM version eagerly locks `toolchain.languageVersion` (#2086), + // and mutating attributes in `afterEvaluate` may fail if the configuration has been + // locked by other plugins (#2054). + attrs.attributeProvider( + TARGET_JVM_VERSION_ATTRIBUTE, + provider { + val compileJvmVersion = + if (shadow.addTargetJvmVersionAttribute.get()) { + compileClasspathConfig + .get() + .attributes + .getAttribute(TARGET_JVM_VERSION_ATTRIBUTE) + // `Int.MAX_VALUE` is set when `java.disableAutoTargetJvm()` is called. + ?.takeIf { it != Int.MAX_VALUE } + } else { + null + } + compileJvmVersion ?: javaPluginExtension.targetCompatibility.majorVersion.toInt() + }, + ) } + outgoing.artifact(tasks.shadowJar) + } registerConsumableConfiguration(SHADOW_SOURCES_ELEMENTS_CONFIGURATION_NAME) { attributes { attrs -> attrs.attribute( @@ -111,39 +130,6 @@ constructor(private val softwareComponentFactory: SoftwareComponentFactory) : Pl }, ) } - - // See more details in #2086. - afterEvaluate { - if (shadow.addTargetJvmVersionAttribute.get()) { - // This eager call will lock `toolchain.languageVersion`, so we must defer it by - // `afterEvaluate`. - val compileJvmVersion = - compileClasspathConfig.get().attributes.getAttribute(TARGET_JVM_VERSION_ATTRIBUTE) - val targetJvmVersion = - compileJvmVersion ?: javaPluginExtension.targetCompatibility.majorVersion.toInt() - if (targetJvmVersion != Int.MAX_VALUE) { - logger.info( - "Setting target JVM version to {} for {} configuration.", - targetJvmVersion, - shadowRuntimeElements.name, - ) - shadowRuntimeElements - .get() - .attributes - .attribute(TARGET_JVM_VERSION_ATTRIBUTE, targetJvmVersion) - } else { - logger.info( - "Cannot set the target JVM version to Int.MAX_VALUE when `java.autoTargetJvmDisabled` is enabled or in other cases.", - ) - } - } else { - logger.info( - "Skipping setting {} attribute for {} configuration.", - TARGET_JVM_VERSION_ATTRIBUTE, - shadowRuntimeElements.name, - ) - } - } } protected open fun Project.configureComponents() {