Different ways to configure java or JDK version in Gradle project

This tutorial talks about different ways of configuring the java version in Gradle projects.

In java projects, Source projects are compiled and generated class files.

In Gradle, For the java plugin, We need to provide the java version in build Gradle for compiling and generating class files.

Java installation is required for building the Gradle project and running the Gradle build.

How to configure source and target JDK version in gradle build

to compile files in java, we use the javac command and which has source and target arguments for the javac command.

javac Emp.java -source 1.9 -target 1.8

source tells the compiler to compile with a specific version target tells the compiler to support the lowest java version to support

In the above program, Emp.java code is compiled with the java 1.9 version, and generated class file has compatible with 1.8 or more, but not 1.7 or newer versions.

Does Gradle support this property? Yes, It supports the same approach with different properties.

It has sourceCompatibility and targetCompatibility properties.

To configure in build.gradle

sourceCompatibility = '1.10'
targetCompatibility = '1.8'

sourceCompatibility uses the java version to compile java files targetCompatibility tells generate class supports minimum java version that has support

This option enables the running of all tasks with specific versions.

Suppose you want different versions for each task, you can configure the build script as follows

tasks.withType<JavaCompile> {
    options.compilerArgs.addAll(arrayOf("--release", "12"))
}

The above script tells us to use java version 12 to compile java files in a Gradle project.

How to install and configure the java version with gradle build?

By default, Gradle uses the java version from the JAVA_HOME environment variable path configured in the machine, and JAVA_HOME points to JDK installed on the machine.

For example, the machine is installed with the JDK 1.8 version and the Gradle project needs a java 11 version.

In that case, How do you handle this?

  • install the java 11 version on the machine and it is straightforward
  • Gradle project has a configuration to install the required Java version and configure.

How do you configure gradle build with java installation and configuration?

with Gradle 6.7 version, It has support for java toolchains

What does the java toolchain do?

  • It ignores the java version on the machine
  • In the build file, configure the java version
  • It installs JDK if not installed
  • Gradle build runs with that version.
  • No configuration required for sourceCompatibility and targetCompatibility properties

How do you configure the java toolchain in the gradle project?

  • First configure plugins for java-library
  • next, configure the toolchain with the required java version

In the build.gradle file, configure the below code.

plugins {
    id("java-library")  //  id("application")
}

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

So Gradle does the following things.

  • Installs java 12 versions from remote locations, Locations can be configurable
  • check for valid installation
  • All java related tasks use the java 12 version to compile and generate class files
  • All test-related tasks use the java 12 version
  • documentation relate tasks use the java 12 version.

It has the flexibility to configure the java version at the task level.

For example, Configure java Compilation requires 12 versions and test requires 15 versions.

tasks.register<Test>("extraTests") {
    javaLauncher.set(javaToolchains.launcherFor {
        languageVersion.set(JavaLanguageVersion.of(15))
    })
}

Conclusion

In this tutorial, You learned different ways to configure the java version.

  • using sourceCompatibility and targetCompatibility options in the build.gradle
  • java toolchain support

With the above two options, You can choose to use the java version differently either at the project level or task level with examples.