Gradle Programmer - Ensure source and target compatibility to a specific version of Java

I originally saw this tip in an Andrés Almiray's talk. There is a Gradle forum thread where it is discussed too.

Sometimes you want to run your program in a machine with Java 1.6. How to ensure source and target compatibility in a build.gradle file:

apply plugin: 'java'
apply plugin: 'groovy'

repositories {
    mavenCentral()
}

sourceCompatibility = 1.6
targetCompatibility = 1.6

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.4.7'
}

tasks.withType(JavaCompile) {
    sourceCompatibility = '1.6'
    targetCompatibility = '1.6'
}

Tags: #gradle
Jun 2016, 17.

 

My next events:
🗓 Jul 20 09:00 Workshop Security in the Micronaut Framework
🗓 Sep 20 09:00 JCON Online 2022 - Getting Started with the Micronaut Framework
🗓 Sep 23 09:00 JCON Online 2022 - Micronaut® Framework and Microstream Java-native persistence engine