如何在Maven中传递虚拟机参数

huangapple go评论61阅读模式
英文:

How to pass VM Options in Maven

问题

<properties>
    <vertx.disableFileCPResolving>true</vertx.disableFileCPResolving>
    <log4j.configurationFile>log4j2.xml</log4j.configurationFile>
    <vertx.logger-delegate-factory-class-name>io.vertx.core.logging.Log4j2LogDelegateFactory</vertx.logger-delegate-factory-class-name>
    <APP_ID>9757632</APP_ID>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <compilerArgs>
                    <arg>-Dvertx.disableFileCPResolving=true</arg>
                    <arg>-Dlog4j.configurationFile=log4j2.xml</arg>
                    <arg>-Dlogger-factory-class-name=io.vertx.core.logging.Log4j2LogDelegateFactory</arg>
                    <arg>-DAPP_ID=9757632</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

请注意,以上内容是您所提供的信息的翻译。如果您有任何问题或需要进一步的帮助,请随时提问。

英文:

I need the following vm options to be passed into maven.

-Dvertx.disableFileCPResolving=true  
-Dlog4j.configurationFile=log4j2.xml 
-Dlogger-factory-class-name=io.vertx.core.logging.Log4j2LogDelegateFactory 
-DAPP_ID=9757632

At present I am passing them via the IDE (Intellij) and everything is working fine. (See screenshot)

如何在Maven中传递虚拟机参数

But how can I add them as part of the Maven project, possibly via POM file or a resource file?

Tried the following in my POM file based on the suggestion in this page but it doesn't work.

https://stackoverflow.com/a/39751176/9401029

&lt;properties&gt;
    &lt;vertx.disableFileCPResolving&gt;true&lt;/vertx.disableFileCPResolving&gt;
    &lt;log4j.configurationFile&gt;log4j2.xml &lt;/log4j.configurationFile&gt;
    &lt;vertx.logger-delegate-factory-class-name&gt;io.vertx.core.logging.Log4j2LogDelegateFactory&lt;/vertx.logger-delegate-factory-class-name&gt;
    &lt;APP_ID&gt;9757632&lt;/APP_ID&gt;

&lt;/properties&gt;

        &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
            &lt;configuration&gt;
                &lt;compilerArgs&gt;
                    &lt;arg&gt;${vertx.disableFileCPResolving}&lt;/arg&gt;
                    &lt;arg&gt;${log4j.configurationFile}&lt;/arg&gt;
                    &lt;arg&gt;${vertx.logger-delegate-factory-class-name}&lt;/arg&gt;
                    &lt;arg&gt;${APP_ID}&lt;/arg&gt;
                &lt;/compilerArgs&gt;
            &lt;/configuration&gt;
        &lt;/plugin&gt;

Saw another example using the file JVMARGS (no extension) which holds following values.

-Dvertx.disableFileCPResolving=true  
-Dlog4j.configurationFile=log4j2.xml 
-Dlogger-factory-class-name=io.vertx.core.logging.Log4j2LogDelegateFactory 
-DAPP_ID=9757632

Placed this file at the root level and within resource folder. That doesn't work either.

Please advice how to pass these values into my maven project, preferably via POM file. Thank you.

答案1

得分: 2

Maven有无数种运行Java代码的方式,通过各种插件实现 -- 而且它们并不都使用相同的方法将JVM命令行开关传递给正在运行的程序。

例如,exec插件 1 会 respest 环境变量 MAVEN_OPTS。这是一个 系统 环境变量,不是Java环境变量。

因此,在Linux上,要在Maven下使用特定的 -Xmx 设置运行程序,我可能会这样做

$ MAVEN_OPTS=&quot;-Xmx2000m&quot; mvn exec:java

但请注意,您需要根据您的平台以适当的方式设置 MAVEN_OPTS。对于Windows上的IDE,您可能需要使用控制面板来进行设置。或者您的IDE在运行Maven时可能会提供设置环境变量的方法 -- 这取决于IDE。

因此,简短的答案是,我猜您需要准确地找到您在Maven中使用的执行插件,然后查看该插件的文档,以了解如何配置它以使用适当的JVM设置。

不过,最终您可能不会长期在Maven下运行应用程序。您需要找到一种方法来独立于Maven指定JVM设置 -- 例如在脚本或批处理文件中。

1 https://www.mojohaus.org/exec-maven-plugin/usage.html

英文:

Maven has a gazillion ways to run Java code, through various plug-ins -- and they don't all use the same methods to pass JVM command-line switches to the running program.

For example, the exec plugin 1 respects the environment variable MAVEN_OPTS. This is a system environment variable, not a Java environment variable.

So on Linux, to run a program under Maven with a specific -Xmx setting I might do

$ MAVEN_OPTS=&quot;-Xmx2000m&quot; mvn exec:java 

But note that you'd need to set MAVEN_OPTS in a way that is appropriate for your platform. For an IDE on Windows, you might need to use the Control Panel to do this. Or your IDE might provide a way to set the environment variable when it runs Maven -- that depends on the IDE.

So the short answer, I guess, is that you need to find exactly which execution plug-in you're using in Maven, and look at the documentation for that plug-in to see how to configure it to use the appropriate JVM settings.

In the end, though, you're probably not going to be running your application under Maven in the long term. You'll need to find a way to specific JVM settings independently of Maven -- in a script, or batch file, for example.

1 https://www.mojohaus.org/exec-maven-plugin/usage.html

huangapple
  • 本文由 发表于 2020年9月17日 07:35:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63929286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定