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 {

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