Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass "-J" options to javac through Maven?

I have a Java project which is built using Maven. I would like to add options to the "javac" command line - particularly, I want to pass a few "-J" options.

So normally I would do something like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <compilerArgument>-J-Xdebug</compilerArgument>
        <compilerArgument>-J-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005</compilerArgument>
    </configuration>
</plugin>

However when I try this I get errors of the form:

[ERROR] Failure executing javac,  but could not parse the error:
javac: invalid flag: -J-Xdebug
Usage: javac <options> <source files>
use -help for a list of possible options

On closer investigation, it seems that maven-compiler-plugin writes all the compiler arguments out to an options file, and invokes javac like 'javac @optionfile'. According to the official documentation for javac at http://docs.oracle.com/javase/6/docs/technotes/tools/solaris/javac.html:

@argfiles One or more files that lists options and source files. The -J options are not allowed in these files.

So it looks like the option in maven-compiler-plugin is not going to work - it wants to use arg files, arg files cannot contain the options I want.

I've also seen some suggestions to use the map instead - however this had similar results when I tried it.

Are there any other options?

like image 620
Richard Downer Avatar asked Feb 17 '13 12:02

Richard Downer


1 Answers

The compiler plugin allows you to specify the location of the jdk, so you could use something like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.0</version>
    <configuration>
      <verbose>true</verbose>
      <fork>true</fork>
      <executable><!-- path-to-javac-invoking-script --></executable>
      <compilerVersion>1.3</compilerVersion>
    </configuration>
  </plugin>

and provide it with a path to a script/bat file that would pass all the arguments along to the real javac along with your extra arguments?

EDIT - the original issue has been fixed in compiler plugin 2.4+ and this should just work now without my workaround

like image 70
radai Avatar answered Oct 17 '22 03:10

radai