Programming for fun and profit

Programming tutorials, problems, solutions. Always with code.

Gradle Java version

Gradle is using default Java version when compiling source code and producing bytecode. How to tell Gradle version of Java source code and expected version of bytecode?

Gradle Java Plugin

Java Plugin for Gradle has two properties for handling Java source and target (compiled code) versions:

  • sourceCompatibility
    Specifies Java version of source code.
  • targetCompatibility
    Specifies target version of JVM bytecode.

Default Java version

By default Gradle will use version of the current JDK for sourceCompatibility and will use value of sourceCompatibility as targetCompatibility. Therefore, when we use the same version for Java source and target bytecode, then it is sufficient to set only version of sourceCompatibility.

Gradle Java version setup

Here’s Gradle configuration, where we set Java source version to 1.7 and version of compiled/target code to 1.8:

// Enable Java project settings:
apply plugin: 'java'

// Source and target are exposed by the Java plugin
// and are passed to the Java compiler:
sourceCompatibility = 1.7
targetCompatibility = 1.8
Share with the World!