Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. `<name>-<version>-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

Expand Down
8 changes: 5 additions & 3 deletions docs/publishing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class PublishingTest : BasePluginTest() {
}

@Test // #1665
fun dontInjectTargetJvmVersionWhenAutoTargetJvmDisabled() {
fun fallBackToTargetCompatibilityWhenAutoTargetJvmDisabled() {
projectScript.appendText(
publishConfiguration(
projectBlock =
Expand All @@ -158,27 +158,26 @@ 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 =
"""
|shadow {
| addTargetJvmVersionAttribute = false
|}
|tasks.named('compileJava') {
| options.release = 8
|}
"""
.trimMargin(),
shadowBlock =
Expand All @@ -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,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ public interface ShadowExtension {
public val addShadowVariantIntoJavaComponent: Property<Boolean>

/**
* 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<Boolean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Comment on lines +92 to +94
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(
Expand All @@ -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() {
Expand Down
Loading