You can open this sample in an IDE that supports Gradle.

This sample shows how to configure a toolchain for a JVM project in Gradle. Your project usually targets a specific java version. Using toolchains, it is very simple to set the required java version while Gradle handles the setting up the tasks (e.g. compile and test) accordingly.

Concretely, we add a convention plugin in buildSrc to share the setup between multiple subprojects:

KotlinGroovy
buildSrc/src/main/groovy/myproject.java-conventions.gradle
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(11)
    }
}

While most modules are fine with the defaults, the list subproject overrides the defaults as it has more specific requirements.

KotlinGroovy
list/build.gradle
tasks.withType(JavaCompile).configureEach {
    javaCompiler = javaToolchains.compilerFor {
        languageVersion = JavaLanguageVersion.of(8)
    }
}

tasks.register('testsOn17', Test) {
    javaLauncher = javaToolchains.launcherFor {
        languageVersion = JavaLanguageVersion.of(17)
    }
}
$ ./gradlew check

BUILD SUCCESSFUL
9 actionable tasks: 9 executed

For more information, see Toolchains chapter.