compileJava - JDTCompile with Gradle 3.5

Hi there

We are using JDTCompiler to compile a java project. Therefore we extended compileJava Task.

compileJava {
options.with {
encoding = ‘utf-8’
fork = true
}

options.forkOptions.with {
executable = ‘java’
jvmArgs = [‘-cp’, configurations.ecj.asPath, ‘org.eclipse.jdt.internal.compiler.batch.Main’]
}
}

With Gradle 3.5 forkOptions.executable feature is deprecated. Is there any different approach, which is not using executable?

thanks for help
regards
Cyrill

I don’t use the JDTCompiler for anything I do, but it seems like using the JavaCompile task wasn’t the correct domain modeling here. Even though it worked, it seems like it might have been more of a work-around. The JavaCompile task provides an API that models the options of the JDK’s compiler. The change from specifying executable to Java home should be more conventional for a user wanting to point to a specific JDK, rather than drilling down the executable.

As the JDTCompiler batch compiler is a Java executable, I would expect that a better model would be to create a JDTCompile task that extends AbstractCompile and calls javaexec to handle which models this particular specification of a compiler. The properties available from the superclasses should have most of what you need that was not too specific to javac. Anything additional that is specific to the JDTCompiler can then be in the class specifically for it.

This also seems like a pretty good case for plugin that could be published to the portal.

Hi, I’m having the same problem and try to use jdtcompile with java by using the plugin https://github.com/TwoStone/gradle-eclipse-compiler-plugin and I’m running into this problem -> “Unrecognized option : -h”

note that I follow the suggestions from here -. https://github.com/gradle/gradle/issues/12904
to avoid -h being passed into JavaCompile.
So I have a subprojects configuration like the below:

subprojects {
  plugins.withType(JavaPlugin) {
    compileJava {
              options.debug = true
              options.debugOptions.debugLevel = "source,vars,lines"
              options.encoding = 'UTF-8'
              options.headerOutputDirectory.convention(null)
    }
  }
}

However, I can still see -h passed into the JavaCompile for some subprojects? is there something I missed?

Is it possible you have some JavaCompile tasks not named “compileJava”? Does the issue go away if you change that to:

subprojects {
  tasks.withType(JavaCompile) {
              options.debug = true
              options.debugOptions.debugLevel = "source,vars,lines"
              options.encoding = 'UTF-8'
              options.headerOutputDirectory.convention(null)
  }
}

thansk @Gary_Hale, it does fix the problem