java - 如何通过 Maven 将 "-J"选项传递给 javac?

标签 java maven javac maven-compiler-plugin

我有一个使用 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/

相关文章:

java - 在 Maven 构建中跳过运行 PITest

mysql - 从源代码构建 MySql JDBC 驱动程序时出错

java - 在自定义 javax.security.auth.spi.LoginModule 中访问 HttpServletRequest

java - 如何中断/暂停图像的读取?

java - Maven 排除不起作用

java - 在 AnnotationProcessor 中访问 javac 选项

javac 为已编译的类创建意外目录

java - 如何从 InputStream 创建基于行的可观察对象?

java - Java静态属性会影响性能吗?

java - 获取最新的 Maven 依赖项。