Java 如何使用 pom.xml 插件添加 VM 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39750348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add VM args using pom.xml plugin
提问by Rahul sharma
I have tried below ways but nothing work... i am trying to access jmx remotely from server.
我尝试了以下方法,但没有任何效果……我正在尝试从服务器远程访问 jmx。
<jvmArgs>
<jvmArg>-Dcom.sun.management.jmxremote.port=9999</jvmArg>
<jvmArg>-Dcom.sun.management.jmxremote.authenticate=false</jvmArg>
<jvmArg>-Dcom.sun.management.jmxremote.ssl=false</jvmArg>
</jvmArgs>
<!-- <systemPropertyVariables>
<com.sun.management.jmxremote.port>9999</com.sun.management.jmxremote.port>
<com.sun.management.jmxremote.authenticate>false</com.sun.management.jmxremote.a uthenticate>
<com.sun.management.jmxremote.ssl>false</com.sun.management.jmxremote.ssl>
</systemPropertyVariables> -->
<!-- <jvmArguments>
<jvmArgument>- Dcom.sun.management.jmxremote.port=9999</jvmArgument>
<jvmArgument>- Dcom.sun.management.jmxremote.authenticate=false</jvmArgument>
<jvmArgument>- Dcom.sun.management.jmxremote.ssl=false</jvmArgument>
</jvmArguments> -->
I also tried
我也试过
<options>
<option>-Dcom.sun.management.jmxremote.port=9999</option>
<option>-Dcom.sun.management.jmxremote.authenticate=false</option>
<option>-Dcom.sun.management.jmxremote.ssl=false</option>
</options>
采纳答案by RITZ XAVI
You can set Java options for Maven in different points and level (globally or via plugins configuration):
您可以在不同的点和级别(全局或通过插件配置)为 Maven 设置 Java 选项:
Plugin configuration: just for Compilation
Using the Maven Compiler Pluginconfiguration for compiling application code and test code, you can set the required Xmx, Xms, Xss options via the compileArgs configuration entry, available for both compileand testCompilegoals. An official example is available hereand on other SO answers like this one.
An example is also shown below.
插件配置:仅用于编译
使用Maven Compiler Plugin配置来编译应用程序代码和测试代码,您可以通过compileArgs 配置条目设置所需的Xmx、Xms、Xss 选项,可用于compile和testCompile目标。一位官员例子可在这里和其他SO答案,像这一个。下面还显示了一个示例。
Plugin configuration: just for Tests execution
Using the Maven Surefire Pluginconfiguration for tests executions, you can set the required Java options to be used at runtime via the argLine configuration entry of the testgoal. An official example is available here.
An example is also shown below on the third point.
插件配置:仅用于测试执行
使用Maven Surefire 插件配置进行测试执行,您可以通过测试目标的 argLine 配置条目设置在运行时使用的所需 Java 选项。这里提供了一个官方示例。下面还显示了关于第三点的示例。
Plugin configuration: via Properties (and profiles)
You can combine the two options above (in case of common Java options) as a property value to pass to both compileArgs
and argLine
configuration entry or have different properties per configuration (according to your needs).
插件配置:通过属性(和配置文件)
您可以将上面的两个选项(在常见 Java 选项的情况下)作为一个属性值传递给两个compileArgs
和argLine
配置条目,或者每个配置具有不同的属性(根据您的需要)。
<property>
<jvm.options>-Xmx256M</jvm.options>
</property>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<compilerArgs>
<arg>${jvm.options}</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<argLine>${jvm.options}</argLine>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
Using properties gives you also an two extra advantages (on top of centralization): you can use profiles then to personalize it based on different desired behaviours (and example in this SO answer) and you can override them via command line as well, like:
使用属性还为您提供了两个额外的优势(在集中化之上):您可以使用配置文件然后根据不同的所需行为(以及此SO 答案中的示例)对其进行个性化设置,您也可以通过命令行覆盖它们,例如:
mvn clean install -Djvm.options=-Xmx512