Passing arguments to compiler and javadoc


(Dalibor Novak) #1

I am trying to provide Gradle as an alternative build system on an existing project. That project makes use of sun.misc.Unsafe which leads to warnings like this one:

warning: Unsafe is internal proprietary API and may be removed in a future release
import sun.misc.Unsafe;

In order to suppress these warnings in Gradle I had to resort to what I feel are ‘dirty’ hacks that I am not too comfortable with.

apply plugin: 'java'
  compileJava {
    // I only managed to get this working when I forked
     // and passed 'javac' as an executable
    options.compilerArgs << '-XDignore.symbol.file'
    options.fork = true
    options.forkOptions.executable = 'javac'
}
    javadoc {
    // These get ignored
    // options.addStringOption('XDignore.symbol.file', null)
    // options.addStringOption('XDignore.symbol.file')
      // This one fails the build - I am assuming it's trying to parse ''
    // options.addStringOption('XDignore.symbol.file')
      // This works, but it's an ugly hack
    options.addStringOption('XDignore.symbol.file', '-quiet')
}

Eventhough this works for me at the moment, I hope that it can be made more elegant.

Here ( https://gist.github.com/3772416 ) is a more complete example with an accompanying ant script and a sample class that causes the problem.

I have tested this with both gradle 1.1 and 1.2. Here is my typical setup:

------------------------------------------------------------
Gradle 1.2
------------------------------------------------------------
  Gradle build time: Wednesday, 12 September 2012 10:46:02 o'clock UTC
Groovy: 1.8.6
Ant: Apache Ant(TM) version 1.8.4 compiled on May 22 2012
Ivy: 2.2.0
JVM: 1.7.0_07 (Oracle Corporation 23.3-b01)
OS: Linux 3.2.0-31-generic amd64

I have raised this as a question on stackoverflow ( http://stackoverflow.com/questions/12554829/passing-arguments-to-compiler-and-javadoc-in-gradle ) and was advised to try to use the following to replace my javadoc “solution”

javadoc.options.jflags "-XDignore.symbol.file"

When trying this the build failed with

Unrecognized option: -XDignore.symbol.file

Thank you for support


(Peter Niederwieser) #2

Regarding the compilation problem, the fact that the flag doesn’t take effect appears to be a problem of the JDK’s JavaCompiler API, which is used by Gradle to invoke the compiler. I’ve verified that the flag gets passed, but it doesn’t have any effect. Other flags like ‘-Xlint:all’ work as expected.

Setting ‘options.forkOptions.executable’ forces Gradle into running the command-line compiler, which doesn’t have this problem. I don’t have a better solution at this point.

Regarding the Javadoc problem, does ‘javadoc.options.addStringOption(‘XDignore.symbol.file’, ‘true’)’ work? If not, can you provide the exact error message? Another potential solution is to point the task to an option file containing the flag with ‘javadoc.options.optionFiles(file(’/path/to/option/file’))’.


(Dalibor Novak) #3

Hello,

As far as compilation goes this is what I suspected and is the reason why I tried forking and setting executable. It is not an ideal workaround, but it does the job for now.

I tried the first option of pasing true as the value of string option and that produced ‘javadoc: warning - No source files for package true’. I didn’t even try the second option with using a file as that felt even less desirable to me. If you want me to I can give it a try though.

Thanks for explanations. Now I at least know that I’m not doing anything exceptionally silly.


(Peter Niederwieser) #4

I think the option file is the only supported way to pass extra arguments at the moment. The ‘addStringOption’ is supposed to be an internal method.