简体   繁体   English

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

[英]Unable to pass java compiler parameters using maven

As the title says I am unable to pass command line parameters to the java compiler using maven, I am using the maven-compiler-plugin to do it, and accordingly to this (specifically for the compilerArgs option of the pluging) I am using the "latest way" to speficy the arguments passed to the compiler. 正如标题所说,我无法使用maven将命令行参数传递给java编译器,我正在使用maven-compiler-plugin来执行此操作,因此对此 (特别是对于plull的compilerArgs选项)我正在使用“最新方式”来表示传递给编译器的参数。 Well enough talk, more code, this is my maven configuration for the plug-in and I am not sure what am I doing wrong: 足够的谈话,更多的代码,这是我的插件的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>

I am following the instructions for the usage of the tool which says that <fork> have to be set to true, and I do not know what am I missing... a little bit of help please? 我正在按照使用该工具的说明进行操作,该工具说<fork>必须设置为true,我不知道我错过了什么...请帮助一下吗?

May or may not be helpful to mention that: I need the parameters argument as specified here because I want to get the name of the arguments in my methods in runtime using reflection; 可能或者可能没有帮助提到:我需要这里指定的parameters参数,因为我想在运行时使用反射获取我的方法中的参数的名称; I use the -X argument when calling maven to see the debug and I shows me the "fork" call that it does and I cannot se ANYWHERE the arguments I am passing (maybe I need to enable the plug-in; but I think In this case is automatically enabled since it is not part of any profile, I am not a maven expert so please correct me if I am wrong). 我在调用maven时使用-X参数来查看调试,我向我展示了它所做的“fork”调用,而且我无法查询我传递的参数(也许我需要启用插件;但我认为In这个案例是自动启用的,因为它不是任何个人资料的一部分,我不是专家,所以如果我错了请纠正我)。

EDIT: I have tried in several ways with and without the dash I have even tried the "old way" to do it: 编辑:我已经尝试了几种方式,有没有破折号我甚至尝试过“老方法”来做:

<compilerArguments>
  <parameters />
</compilerArguments>

And: 和:

<compilerArgument>-parameters</compilerArgument>

PFFF Guys forget it, and sorry for bothering you :( PFFF伙计们忘了它,很抱歉打扰你:(

My mistake: I created the code first previous to modify my pom file, and run it USING MAVEN to check that is was actually working, after that I modified my pom to include the -parameters flag to print the name of my my parameters in my method to see that it was actually working. 我的错误:我之前创建了代码以修改我的pom文件,并运行它使用MAVEN来检查实际是否正常工作, 之后我修改了我的pom以包含-parameters标志来打印我的参数名称方法,看它实际上是在工作。 The code had already been compiled once WITHOUT the flag being set and THE CODE WAS NEVER MODIFIED AFTER therefor maven "said" 'well there are no changes on this file, I do not need to re-compile it, I'll just use the one already compiled before' (which usually is a good thing, it reduces compilation time) but not in this case because the -parameters flag compiles the code in a way that is a "fat class" (or at least heavier since it includes the name of the parameters) but maven did not know about that. 代码已经编译一次没有设置标志和代码没有修改,因此maven “说” '这个文件没有变化,我不需要重新编译它,我只是使用一个已经编译过'(这通常是一件好事,它减少了编译时间)但在这种情况下不是因为-parameters标志以一种“胖类”的方式编译代码(或者至少更重,因为它包括参数的名称)但是maven并不知道。 TT TT

SOLUTION execute a mvn clean or delete the compiled classes or deleate the /target folder (in summary DO WHAT EVER IS NECESSARY TO ENSURE THAT THE FILES ARE COMPILED AGAIN ) 解决方案执行mvn clean或删除已编译的类或删除/ target文件夹(总结为什么有必要确保文件再次编译

Please mention, that you always have to write a '-' letter before the parameters. 请注意,您必须在参数之前写一个' - '字母。

Below you can see a configuration for your plugin with some sample compiler arguments. 下面您可以看到插件的配置以及一些示例编译器参数。

<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>

From the maven compile plugin main page : maven编译插件主页

The Compiler Plugin is used to compile the sources of your project. Compiler Plugin用于编译项目的源代码。 Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. 从3.0开始,默认编译器是javax.tools.JavaCompiler(如果您使用的是java 1.6)并且用于编译Java源代码。 If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse. 如果要使用javac强制插件,则必须配置插件选项forceJavacCompilerUse。

I'm guessing the javax.tools.JavaCompiler doesn't works the same way javac does with the -parameters option. 我猜javax.tools.JavaCompilerjavac-parameters选项的工作方式不同。

Try forcing the javac use 尝试强制使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM