# Using toolchains Sample

version 6.7.1

- Groovy DSL

- Kotlin DSL

| You can open this sample inside an IDE using the IntelliJ native importer or Eclipse Buildship.

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/groovy/myproject.java-conventions.gradle

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

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

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

While most modules are fine with the defaults, the one of our modules overrides the defaults as it has more specific requirements.

list/build.gradle

    tasks.withType(JavaCompile).configureEach {
        javaCompiler = javaToolchains.compilerFor {
            languageVersion = JavaLanguageVersion.of(8)
        }
    }
    test {
        javaLauncher = javaToolchains.launcherFor {
            languageVersion = JavaLanguageVersion.of(14)
        }
    }

list/build.gradle.kts

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

    tasks.withType<Test>().configureEach {
        javaLauncher.set(javaToolchains.launcherFor {
            languageVersion.set(JavaLanguageVersion.of(14))
        })
    }

    $ ./gradlew check

    BUILD SUCCESSFUL
    9 actionable tasks: 9 executed

For more information, see Toolchains chapter.