Multi project with different source & target compatibility

I have a multi-project gradle configuration which is using java 8 for building. I am downgrading one of the sub-project to Java 6. My problem is that while specifying sourcecompatibility and targetcompatibility to 1.6 for this project the project is building fine although he is using some Apis from Java 8 (it must not compile because I want a to compile it for java 1.6).

I have created an example of multi-module gradle project with one of the sub-project using Java 6 and another one java 8. I have used org.gradle.java.home attribute in gradle.properties for each of the sub-projects to set the Java home for each of them. Here is the github repo.

The subprojects contain each one class using Apis from Java 8, the subproject using java 6 is expected to fail, but the build runs successfully.

Here is the parent build.gradle file :

group 'com.test'
version '1.0-SNAPSHOT'
repositories {
   mavenCentral()
}

The content of the parent settings.gradle:

rootProject.name = 'Parent'
include 'child2Java6'
include 'child1Java8'

the content of build.gradle of the Java 6 subproject :

group 'com.test'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = JavaVersion.VERSION_1_6
targetCompatibility = JavaVersion.VERSION_1_6
repositories {
    mavenCentral()
}
compileJava.doFirst {
    println "source compatibility  " + sourceCompatibility
    println "target compatibility  " + targetCompatibility
}
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

the content of gradle.properties of the Java 6 subproject :

org.gradle.java.home=/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/
build.gradle for java 8 sub-project:

group 'com.test'
version '1.0-SNAPSHOT'

apply plugin: 'java'


sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
repositories {
    mavenCentral()
}
compileJava.doFirst {
    println "source compatibility  " + sourceCompatibility
    println "target compatibility  " + targetCompatibility
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

It’s gradle.properties :

org.gradle.java.home=/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/
I am printing the sourceCompatibility and targetCompatibility and they have the good values for each project.

I am using gradle clean build for building.

Thegradle -v outputs this :

------------------------------------------------------------
Gradle 2.9
------------------------------------------------------------

Build time:   2015-11-17 07:02:17 UTC
Build number: none
Revision:     b463d7980c40d44c4657dc80025275b84a29e31f

   Groovy:       2.4.4
   Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
   JVM:          1.8.0_65 (Oracle Corporation 25.65-b01)
   OS:           Mac OS X 10.11.1 x86_64

I am using Java 8 as my default Jdk.

Thanks in advance,

You cannot change the java.home on a per-project level, because it applies to the whole multi-project build.

Setting the source level of the compiler only has an effect on which language features you can use (e.g. Lambdas). It has no effect on APIs. That is unrelated to Gradle and true even if you run javac directly. To make sure you are not using the wrong APIs, you’ll have to set the bootClasspath of the compile task.

tasks.withType(JavaCompile) {
  options.fork = true
  options.bootClasspath = "path/to/rt.jar"
}

Exactly what I need thanks @st_oehme it worked perfectly