Search code examples
javagradlebuild.gradle

Can Gradle use the toolchain for buildscript dependencies?


In other words, can Gradle use the toolchain setting to run itself?

I have configured a toolchain in my gradle build and the required JDK is nicely downloaded when missing. I also need the buildscript dependencies to be affected by the toolchain but this doesn't happen.

Example:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'some:lib:1.0' // This lib requires Java 17
    }
}
java {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

On the system I only have java 8 so Gradle downloads java 17 to run the application but tries to fetch a java 8 library for 'some:lib:1.0' and being it compiled with 17 it fails with something like

No matching variant of some:lib:1.0 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar


Solution

  • You are talking about the Daemon JVM feature, which is currently incubating.

    Execute ./gradlew updateDaemonJvm --jvm-version=17 to let gradle generate the necessary properties file and commit the changes. This will execute the Gradle Build itself with the configured Java version using the same discovery features as the toolchain.

    However:

    The daemon JVM discovery process does not support auto-provisioning of new JVM installations. This will be added in a future release.