This article will cover shortly how to set Java source compatibility and target compatibility in Gradle.
When we use Gradle to build our Java project, we often have to define the Java source language level and the target level of the bytecode that is generated. Gradle provides us a plugin called Gradle Java plugin which has two properties for that:
- sourceCompatibility: The Java version compatibility to use when compiling Java source
- targetCompatibility: Java version to generate classes for
2. Set Java source compatibility and target compatibility in Gradle
We can define the sourceCompatibility and targetCompatibility in the gradle.build file. Below is an example that we set Java 8 for both of them:
apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8
If we don’t set values for those properties, the default value of sourceCompatibility will be the version of the current JVM in use, and the default value of targetCompatibility will be the same with sourceCompatibility.
In similar to Maven or other build tools, we can set Java source compatibility and target compatibility in Gradle. This is very flexible for us in controlling the level of source code and target generated output. Below are other related articles related to Gradle, you can reference if you’re interested in