micronaut-projects / micronaut-starter

Generates Micronaut applications
Apache License 2.0
206 stars 94 forks source link

Use Gradle's JavaToolchain support for new Kotlin projects #2532

Open itsandreramon opened 1 month ago

itsandreramon commented 1 month ago

Description

Generating a new JDK 21 Kotlin Gradle project when using JDK 22 locally results in Kotlin target JDK mismatch causing the sync to fail. For example, new Spring Boot projects automatically set the Java language version using Gradle's JavaToolchain support.

Task List

Steps to Reproduce

  1. Create a starter using Java 21 w/o any plugins using Kotlin & Kotlin Gradle
  2. Have JDK 22 as Java Home
  3. Sync project, notice the warning that Kotlin is incompatible with 22 JDK target, it downgrades to 21 automatically Kotlin does not yet support 22 JDK target, falling back to Kotlin JVM_21 JVM target
  4. Change Java sourceCompatability to 17
  5. Try to resync & run the app and notice error: > Inconsistent JVM-target compatibility detected for tasks 'compileJava' (17) and 'kspKotlin' (21).

Expected Behaviour

kspKotlin and compileJava should use the same language version automatically.

Actual Behaviour

kspKotlin and compileJava use different JVM target versions.

Environment Information

itsandreramon commented 1 month ago

This issue is resolved automatically using

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