toolbox icon indicating copy to clipboard operation
toolbox copied to clipboard

Java compatibility forced to match Gradle minimum Java compatability

Open MarkRx opened this issue 1 year ago • 3 comments

When specifying a minimum Gradle version the plugin forces the java compatibility to match the Gradle minimum compatibility version here. This prevents plugins from building with a newer JDK because dependencies will fail to get pulled when running with newer versions of Gradle due to variant JDK version missmatches.

- Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 8

Users should be able to set compatibility to a version higher than the minimum Gradle java version.

MarkRx avatar Sep 08 '23 16:09 MarkRx

It looks like this might have been fixed on the main branch. There does not appear to be a release yet with these changes though.

MarkRx avatar Sep 08 '23 16:09 MarkRx

The new and old codes accounted for this by only forcing JVM compatibility if the user didn't specify anything. I checked, and it does work. However... I see the issue. Gradle automatically set the source compatibility when setting the target compatibility, which is what was left out. Could you confirm that you only set the target compatibility?

lacasseio avatar Sep 12 '23 21:09 lacasseio

It is actually still broken on master.

I'm using a java toolchain instead of source/target compatibility

        java {
            toolchain {
                languageVersion = JavaLanguageVersion.of(17)
            }
        }

The problem is that the plugin is setting the source compatibility value to 1.1 in InjectJvmCompatibilityPropertyIntoJavaExtensionRule.onExtension(JavaPluginExtension). That value ends up taking precedence over the toolchain version in DefaultJavaPluginExtension.getSourceCompatibility() meaning the toolchain version is never checked. As a result the logic to set the jvm compatibility version to the minimum version supported by the target version of gradle is called.

MarkRx avatar Sep 13 '23 17:09 MarkRx

Version 1.6.13 should fix the issue. Please reopen if the problem is still present.

lacasseio avatar Jul 08 '24 20:07 lacasseio

The fix in 1.6.13 actually broke the compilation in my case. Here is what I typically do:

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(22)
    }
}

withType<JavaCompile>().configureEach {
    options.release = 8
}

This configuration uses the latest toolchain to compile for a specific Java version. However, as of the changes in 1.6.13, this is no longer supported. The culprit seems to be that the compatibility versions are set to the toolchain versions regardless of whether options.release is specified and then take precedence.

For example, this issue surfaces when writing a plugin in Kotlin:

kotlin {
    compilerOptions {
        jvmTarget = JvmTarget.JVM_1_8
    }
}

Running a build then fails even if options.release is set with:

Inconsistent JVM-target compatibility detected for tasks 'compileJava' (22) and 'compileKotlin' (1.8).

TheMrMilchmann avatar Jul 31 '24 10:07 TheMrMilchmann

Opened #113 for visibility

TheMrMilchmann avatar Aug 12 '24 22:08 TheMrMilchmann