# Using toolchains Sample

version 8.14.5

- Groovy DSL

- Kotlin DSL

| 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:

buildSrc/src/main/kotlin/myproject.java-conventions.gradle.kts

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

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.

list/build.gradle.kts

    tasks.withType<JavaCompile>().configureEach {
        javaCompiler = javaToolchains.compilerFor {
            languageVersion = JavaLanguageVersion.of(8)
        }
    }

    tasks.register<Test>("testsOn17") {
        javaLauncher = javaToolchains.launcherFor {
            languageVersion = JavaLanguageVersion.of(17)
        }
    }

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.