How to set compiler options?

I have read the other posts related to compiler options but it doesn’t seem to work. The problem is that we are using a Sun private class and it will not compile using Gradle. It does compile in Eclipse with no special settings.

Output:
:Project1:compileJava
\MyClass.java:37: error: package sun.security.pkcs10 does not exist
import sun.security.pkcs10.PKCS10;
1 error
FAILED

We are planning to migrate off of the Sun private implementation but in the mean time we need the project to compile.

I have used (in addition to the suggestions in other threads):

compileJava {
    options.warnings = false
    options.deprecation = false
    options.compilerArgs += ["-Xdoclint:none", "-Xlint:none", "-nowarn"]
}

tasks.withType(JavaCompile) {
    println 'Compiler args: ' + options.compilerArgs
}

The compiler options print twice, once with the options I specified and then an empty list.

Does this have anything to do with “access restrictions” on the JDK jars? If so, how to set in Gradle?

This project is part of a multi-project. Running Gradle from the command line does not work at all because we have common Groovy/Java code in buildSrc of the master project. This is a spearate issue. Using the Gradle Eclipse plugin seems to deal with multi-projects ok.

a usual java project has two tasks of type JavaCompile. One is for production code (compileJava) one is for compiling unit tests (compileTestJava)

So I take it from the lack of response that there is no current support for setting compiler options or maybe its just not working correctly?

Ok, I found the solution. You need more options to make this error go away.

options.compilerArgs += ["-XDignore.symbol.file", "-Xdoclint:none", "-Xlint:none", "-nowarn"]
options.fork = true
options.forkOptions.executable = 'javac'

Not sure you need to turn off all the warnings. The question is, why is it that your compiler options are only used if you fork?? That seems odd to me.

And I won’t go into why you shouldn’t use Sun private classes, you can read that info elsewhere, but this is the kind of problem it causes.

2 Likes

There are some options that are non-standard and not supported by the Java Compiler API which Gradle uses when you are running in the same JVM. I

In particular, -XDignore.symbol.file is definitely a non-standard option and you must fork if you want to use it.