Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different java versions in gradle sub-projects

Tags:

gradle

I have a multi project build, and I need one project to use Java 8 when the rest use Java 6 (I know but at the moment I am unable to upgrade them all)

Is there any way to have a different version of Java for one sub-project in a multi-project build?

Thanks

like image 764
Klunk Avatar asked Mar 15 '26 12:03

Klunk


1 Answers

Gradle 6.7 introduced toolchains support. Though this was primarily defined to allow specifying the Java version a project is compiled with independent of the Java version a Gradle build is executed with, it can be used to specify different Java versions for different sub projects of a multi-module project.

For instance, given two sub projects a and b, if we want to specify that a should compile with Java 11 and b with Java 17, your builds scripts would contain the following:

project a build.gradle

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

project b build.gradle

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}
like image 186
twwwt Avatar answered Mar 18 '26 01:03

twwwt