It seems that javac compiler options is not passed to compileJava task on Gradle2.6 with JDK8u60

Hi.

I’m checking JDK8u60’s bug fix of JDK-8079044 javac -parameters does not emit parameter names for lambda expressions with a project, which requires javac option -parameters to work correctly, as subprojects. But It seems that Gradle’s compiler setting compileJava.options.compilerArgs is not passed to the subproject’s compileJava task.

The project structure is as follows.

rootProject
+-setting.gradle
+-build.gradle
+-src
| +-main
| | +-groovy
| |   +-my.pkg
| +-test
|   +-groovy
|     +-my.pkg
+-type-ref
  +-src
    +-main
    | +-java
    |   +-com.benjiweber.typeref
    +-test
      +-java
        +-com.benjiweber.typeref

My build script is…

plugins {
    id 'groovy'
    id 'idea'
}

ext {
    jdkLevel = 1.8
    encoding = 'UTF-8'
}

subprojects {
    apply plugin: 'java'
    repositories {
        mavenCentral()
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile project(':type-ref')
    compile 'org.codehaus.groovy:groovy:2.3.10'
    testCompile 'junit:junit:4.12'
    testCompile ('org.spockframework:spock-core:1.0-groovy-2.3') {
        exclude module: 'groovy-all'
        exclude module: 'junit'
    }
}

tasks.withType(JavaCompile).each {
    it.sourceCompatibility = jdkLevel
    it.targetCompatibility = jdkLevel
    it.options.encoding = encoding
}

project(':type-ref') {
    dependencies {
        testCompile 'junit:junit:4.12'
        testCompile 'org.mockito:mockito-all:1.9.5'
    }
    compileJava {
        sourceCompatibility = jdkLevel
        targetCompatibility = jdkLevel
        // this subproject requires -parameters option
        options.compilerArgs << '-parameters'
        options.encoding = encoding
    }
}

(The type-ref subproject is the project, I mentioned before.)

The root project has a test using type-ref project library, and when I run the tests, it fails with error message…

Caused by: java.lang.IllegalStateException: You need to compile with javac -parameters for parameter reflection to work; You also need java 8u60 or newer to use it with lambdas
	at com.benjiweber.typeref.NamedValue.checkParametersEnabled(NamedValue.java:13)
	at com.benjiweber.typeref.NamedValue.name(NamedValue.java:8)

The error message says that the subproject is not compiled with option -parameters.

How do I pass compiler option(compileJava.options.compilerArgs)?


OS | Mac OSX 10.10.4
JDK | 8u60
Gradle | 2.6

The tests failed initially for me because the classes being tested were not compiled with -parameters (the test sources, not the main sources).

You should change your type-ref project to configure all of the compile tasks of type JavaCompile to use -parameters since the tests are looking for parameter names from test classes:

tasks.withType(JavaCompile) { 
     sourceCompatibility = jdkLevel
     targetCompatibility = jdkLevel
     // this subproject requires -parameters option
     options.compilerArgs << '-parameters'
     options.encoding = encoding
}

compileJava only configures the task used to compile the main sources (there’s a corresponding compileTestJava too).

I would probably set the source/target compatibility on the project level vs on the compile task.

I would also not use tasks.withType(JavaCompile).each because that iterates over the task container at that point in time vs over all of the tasks of type JavaCompile that are ever created. Just remove the .each and that will be better.

Thanks!

It works fine.