无法使用 Maven 传递 java 编译器参数

Unable to pass java compiler parameters using maven

正如标题所说,我无法使用 maven 将命令行参数传递给 java 编译器,我正在使用 maven-compiler-plugin to do it, and accordingly to this(专门用于插件的 compilerArgs 选项)我正在使用"latest way" 指定传递给编译器的参数。好吧,更多代码,这是我对 plug-in 的 Maven 配置,我不确定我做错了什么:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <fork>true</fork>
                <compilerArgs>
                    <arg>-parameters</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

我正在按照该工具的使用说明进行操作,其中说明必须将 <fork> 设置为 true,但我不知道我缺少什么...请提供一点帮助?

提及以下内容可能有帮助,也可能没有帮助:我需要指定的 parameters 参数 here 因为我想在运行时使用反射获取我的方法中的参数名称;我在调用 Maven 查看调试时使用 -X 参数,我向我展示了它所做的 "fork" 调用,我无法在任何地方使用我传递的参数(也许我需要启用 plug-in;但我认为在这种情况下是自动启用的,因为它不是任何配置文件的一部分,我不是 maven 专家所以如果我错了请纠正我)。

编辑: 我已经尝试了几种有和没有破折号的方法我什至尝试过 "old way" 来做它:

<compilerArguments>
  <parameters />
</compilerArguments>

并且:

<compilerArgument>-parameters</compilerArgument>

请注意,您总是必须在前面写一个“-”字母 参数。

下面你可以看到你的插件的配置和一些示例 编译器参数。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <fork>true</fork>
                <compilerArgs>
                    <arg>-verbose</arg>
                    <arg>-Xlint:all,-options,-path</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

来自maven compile plugin main page

The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse.

我猜 javax.tools.JavaCompiler 的工作方式与 javac-parameters 选项的工作方式不同。

尝试强制 javac 使用

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <forceJavacCompilerUse>true</forceJavacCompilerUse>
            <fork>true</fork>
            <compilerArgs>
                <arg>-parameters</arg>
            </compilerArgs>
        </configuration>
    </plugin>
</plugins>
</build>

我的错误: 我在修改我的 pom 文件之前创建了代码,运行 它 使用 maven 检查实际上是在工作。 在那之后,我修改了我的 pom 以包含 -parameters 标志。代码已经编译 没有 那个标志并且 之后没有被修改。所以maven看到代码没有变化,没有重新编译文件。

SOLUTION 执行 mvn clean,删除已编译的 类,删除 target 文件夹,或任何确保文件被重新编译。