# Using toolchains Sample

version 7.3

- 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(11)
        }
    }

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

    java {
        toolchain {
            languageVersion.set(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

    tasks.withType(JavaCompile).configureEach {
        javaCompiler = javaToolchains.compilerFor {
            languageVersion = JavaLanguageVersion.of(8)
        }
    }
    task('testsOn14', type: 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.register<Test>("testsOn14") {
        javaLauncher.set(javaToolchains.launcherFor {
            languageVersion.set(JavaLanguageVersion.of(14))
        })
    }

    $ ./gradlew check

    BUILD SUCCESSFUL
    9 actionable tasks: 9 executed

For more information, see Toolchains chapter.