-source and -target are only passed to javac when java version is different than gradle java version


(Ryan Ernst) #1

Setting targetCompatibility and sourceCompatibility for a java project doesn’t actually do anything if the JavaVersion.current() is the same as the target/source compatibility. While that “works” most the time, it is an unnecessary “optimization” that breaks down when the JavaCompile executable is set, for example to a java 9 javac (ie gradle is running on java 8, but javac is java 9, which steps around the issues gradle has with java 9 at the moment).


(Ryan Ernst) #2

See https://github.com/elastic/elasticsearch/issues/18039


(Schalk Cronjé) #3

This is a long standing issue. The work around is to do something like:

sourceCompatibility = 1.6 
targetCompatibility = 1.6

plugins.withType(JavaPlugin) {

    project.tasks.withType(JavaCompile) { task ->
        task.sourceCompatibility = project.sourceCompatibility
        task.targetCompatibility = project.targetCompatibility
    }

    project.tasks.withType(GroovyCompile) { task ->
        task.sourceCompatibility = project.sourceCompatibility
        task.targetCompatibility = project.targetCompatibility
    }
}

(Ryan Ernst) #4

I know about that, but that is the not the issue here. Read my explanation carefully. The version of java used to compile is not the same as that gradle is running on in my case.


(Schalk Cronjé) #5

I understand now. I think it is valid use case.

Also I remember from Twitter the other day that @CedricChampeau mentioned he has a version in his Gradle clone that works with Java 9.