查找Maven使用的Java选项 [英] Find Java options used by Maven
问题描述
我如何找到Maven使用的Java选项(Xmx,Xms,Xss等)?
How can I find which Java options (Xmx, Xms, Xss, etc) are being used by Maven?
我发现设置的一种方法是通过环境MAVEN_OPTS.现在,我希望有一种方法可以确保它得到正确的设置.
I've found out that a way to set them is via the environment MAVEN_OPTS. Now I want a way to be assured it's getting the right settings.
I believe it´s different to this question as I don´t want to see the value of an environment variable. I rather want to see which settings are actually being used by Maven, whether it comes from a env var, settings.xml or other means.
我对设置Maven构建的Java选项的其他方式也很感兴趣
I'm also interested in other ways of setting Java options for a Maven build
推荐答案
您可以在不同的点和级别(全局或通过插件配置)为Maven设置Java选项:
You can set Java options for Maven in different points and level (globally or via plugins configuration):
插件配置:仅用于编译
使用 Maven编译器插件配置来编译应用程序代码和测试代码,您可以设置通过compileArgs
配置条目所需的Xmx,Xms,Xss选项,可同时用于编译和 testCompile 目标.可以在此处获得一个官方示例. >以及其他类似此的答案.
下面的第三点也显示了一个示例.
Plugin configuration: just for Compilation
Using the Maven Compiler Plugin configuration for compiling application code and test code, you can set the required Xmx, Xms, Xss options via the compileArgs
configuration entry, available for both compile and testCompile goals. An official example is available here and on other SO answers like this one.
An example is also shown below on the third point.
插件配置:仅用于执行测试
使用 Maven Surefire插件配置来执行测试,您可以设置所需的Java选项通过测试的argLine
配置条目在运行时使用目标. 此处可用.
下面的第三点也显示了一个示例.
Plugin configuration: just for Tests execution
Using the Maven Surefire Plugin configuration for tests executions, you can set the required Java options to be used at runtime via the argLine
configuration entry of the test goal. An official example is available here.
An example is also shown below on the third point.
插件配置:通过属性(和配置文件)
您可以将上面的两个选项(在使用通用Java选项的情况下)组合为一个属性值,以同时传递到compileArgs
和argLine
配置条目,或者每个配置具有不同的属性(根据您的需要).
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).
<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>
<compilerArgs>
<arg>${jvm.options}</arg>
</compilerArgs>
</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:
mvn clean install -Djvm.options=-Xmx512
全局/项目配置:选项文件
从Maven 3.3.1开始,您可以在.mvn/jvm.config
文件中指定您的选项每个项目.它是MAVEN_OPTS的替代方案,范围更窄(每个项目).但是,由于它具有Maven的新功能,因此人们应该意识到这一点,否则可能会影响故障排除和维护.
Global/Project configuration: Options file
Since Maven 3.3.1, you can specify your options in a .mvn/jvm.config
file per project. It's an alternative to MAVEN_OPTS and a more narrowed scope (per project). However, since it sis a new Maven feature, people should be aware of it, otherwise troubleshooting and maintenance may be impacted.
全局/环境配置:MAVEN_OPTS
Maven 著名的环境变量用于设置全局执行选项,但是应用于共享该环境的所有Maven构建(即,每个登录用户).
Global/Env configuration: MAVEN_OPTS
Maven well-known environment variable to set global execution options, however applied to all Maven builds sharing that environment (i.e. per logged user).
使用-X
选项(启用调试)运行Maven时,将在构建中包含以下输出:
When running Maven using the -X
option (debug enabled), you will have the following output as part of your build:
[DEBUG] properties used {java.vendor=Oracle Corporation, ... , env.MAVEN_OPTS=-Xmx256M, ...
更新
毕竟,执行的mvn
命令是OS脚本.在Windows中进行查看后,我发现可以使用MAVEN_BATCH_ECHO
选项,如果启用了该选项(将值设置为on
),该选项将回显脚本执行的任何命令,因此也会调用MAVEN_OPTS
)以及传递给它的完整参数列表.
Update
After all, the executed mvn
command is an OS script. Having a look at it in Windows, I found the possibility of using the MAVEN_BATCH_ECHO
option which, if enabled (value set to on
), will echo any command executed by the script and as such also the invocation of the java
command, where you can see if your options (the MAVEN_OPTS
) are picked up correctly together with the full list of parameters passed to it.
这是我在Windows上测试过的执行程序:
Here is an execution I tested on Windows:
set MAVEN_BATCH_ECHO=on
set MAVEN_OPTS=-Xmx256M
mvn compile > output.txt
注意:output.txt
将包含很多文本,提供构建输出和其他echo
执行.作为其中的一部分,它提供了:
NOTE: the output.txt
will contain quite a lot of text, providing build output and additional echo
s executions. As part of it, it provided:
>"path_to_\jdk1.7\bin\java.exe" -Xmx256M -classpath "path_to\apache-maven-3.1.1\bin\..\boot\plexus-classworlds-2.5.1.jar" "-Dclassworlds.conf=path_to\apache-maven-3.1.1\bin\..\bin\m2.conf" "-Dmaven.home=path_to\apache-maven-3.1.1\bin\.." org.codehaus.plexus.classworlds.launcher.Launcher compile
如您所见,正确选择了-Xmx256M
选项.如果其他操作系统的maven脚本不提供此选项,则可以将其添加到脚本中(在执行java
之前显示一个命令的简单echo
也是足够的).
您可以在bin
子文件夹下的maven安装文件夹中找到maven脚本.
As you can see, the -Xmx256M
option was picked up correctly. If the maven script for other OS doesn't provide this option, then you can simply add it to the script (a simple echo
before the java
execution, showing the command, would also be enough).
You can find the maven script in your maven installation folder, under the bin
subfolder.
Update2
此外,由于Maven毕竟是Java工具,并且从其脚本中可以看到它调用Java命令,因此您可以看到SO答案.
Update2
Furthermore, since Maven is a Java tool after all and as you can see from its script it invokes the java command, you could see all the available options as suggested in this SO answer by slightly changing the Maven script and use the jinfo
command which should really give you the answer according to this other SO answer.
这篇关于查找Maven使用的Java选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!