我有一个使用 Maven 构建的 Java 项目。我想向“javac”命令行添加选项 - 特别是,我想传递一些“-J”选项。
所以通常我会做这样的事情:
<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>
然而,当我尝试这个时,我得到了以下形式的错误:
[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
经过仔细调查,似乎 maven-compiler-plugin 将所有编译器参数写入选项文件,并调用 javac,如“javac @optionfile”。根据 http://docs.oracle.com/javase/6/docs/technotes/tools/solaris/javac.html 处的 javac 官方文档:
@argfiles One or more files that lists options and source files. The -J options are not allowed in these files.
所以看起来 maven-compiler-plugin 中的选项不起作用 - 它想使用 arg 文件,arg 文件不能包含我想要的选项。
我也看到了一些使用 map 代替的建议 - 但是当我尝试时这有类似的结果。
还有其他选择吗?
最佳答案
编译器插件允许您 specify the location of the jdk , 所以你可以使用这样的东西:
<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>
并为其提供脚本/bat 文件的路径,该文件会将所有参数与您的额外参数一起传递给真正的 javac?
编辑 - the original issue已在编译器插件 2.4+ 中修复,现在无需我的解决方法,这应该可以正常工作
关于java - 如何通过 Maven 将 "-J"选项传递给 javac?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14920921/