Repository: GradleUp/shadow (kotlin)

Issue:
Target JVM version is incorrectly inferred
### Expected and Results

#1199 introduced the missing `"org.gradle.jvm.version"` attribute for the module metadata of the shadowed artifacts. The version is however not resolved to the correct JVM version in all cases.

### Related environment and versions

Shadow: 9.0.0-rc1

### Reproduction steps

Consider the following example:
```kotlin
java {
	toolchain {
		languageVersion.set(JavaLanguageVersion.of(21))
	}
}

tasks.compileJava {
	options.release = 8
}
```
(See [this blog post](https://jakewharton.com/gradle-toolchains-are-rarely-a-good-idea/) for why you'd want to do this)

This produces a jar compatible with Java 8, but when also applying the shadow plugin it claims it was compiled against Java 21.

### Anything else?

The implementation currently only looks at `targetCompatibility` https://github.com/GradleUp/shadow/blob/b224199bfd97b937b1e965d43c71193bac9da360/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.kt#L78-L82

The corresponding implementation in Gradle seems to take a few more cases into consideration:
https://github.com/gradle/gradle/blob/b06b7de4572c102a02ed831ae1f37e2da8255c35/platforms/jvm/plugins-java-base/src/main/java/org/gradle/api/plugins/jvm/internal/DefaultJvmLanguageUtilities.java#L64-L114

Pull request:
Fixes #1501

---

- [x] [CHANGELOG](https://github.com/GradleUp/shadow/blob/main/docs/changes/README.md)'s "Unreleased" section has been updated, if applicable.

Patch:
diff --git a/docs/changes/README.md b/docs/changes/README.md
index 15e7036f8..87634c053 100644
--- a/docs/changes/README.md
+++ b/docs/changes/README.md
@@ -2,6 +2,9 @@
 
 ## [Unreleased](https://github.com/GradleUp/shadow/compare/9.0.0-rc1...HEAD) - 2025-xx-xx
 
+**Fixed**
+- Honor `options.release` for target JVM attribute. ([#1502](https://github.com/GradleUp/shadow/pull/1502))
+
 ## [9.0.0-rc1](https://github.com/GradleUp/shadow/releases/tag/9.0.0-rc1) - 2025-07-02
 
 > This release is a major update from the 8.3.x series. The plugin has been fully rewritten in Kotlin, bringing
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 003a30e6e..af026787a 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,9 +75,12 @@ public abstract class ShadowJavaPlugin @Inject constructor(
           objects.named(LibraryElements::class.java, LibraryElements.JAR),
         )
         attr.attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling::class.java, Bundling.SHADOWED))
-        val targetJvmVersion = provider {
-          javaPluginExtension.targetCompatibility.majorVersion.toInt()
-        }
+        val targetJvmVersion = configurations.named(JavaPlugin.COMPILE_CLASSPATH_CONFIGURATION_NAME)
+          .map { compileClasspath ->
+            compileClasspath.attributes.getAttribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE)
+              ?: javaPluginExtension.targetCompatibility.majorVersion.toInt()
+          }
+
         // Track JavaPluginExtension to update targetJvmVersion when it changes.
         attr.attributeProvider(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, targetJvmVersion)
       }


Test patch:
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 afb861d75..0c740a109 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
@@ -87,6 +87,7 @@ class PublishingTest : BasePluginTest() {
     }.toTypedArray()
     val targetJvmAttr17 = TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE.name to "17"
     val targetJvmAttr11 = TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE.name to "11"
+    val targetJvmAttr8 = TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE.name to "8"
 
     projectScriptPath.appendText(
       """
@@ -118,6 +119,17 @@ class PublishingTest : BasePluginTest() {
     publish()
     // sourceCompatibility doesn't affect the target JVM version.
     assertions(attrsWithoutTargetJvm + targetJvmAttr11)
+
+    projectScriptPath.appendText(
+      """
+        tasks.named('compileJava') {
+          options.release = 8
+        }
+      """.trimIndent() + System.lineSeparator(),
+    )
+    publish()
+    // options.release flag is honored.
+    assertions(attrsWithoutTargetJvm + targetJvmAttr8)
   }
 
   @Test