查找 Maven 使用的 Java 选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34739321/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:15:47  来源:igfitidea点击:

Find Java options used by Maven

javamavenmaven-3jvm-argumentsjava-opts

提问by Vituel

How can I find which Java options (Xmx, Xms, Xss, etc) are being used by Maven?

如何找到 Maven 正在使用哪些 Java 选项(Xmx、Xms、Xss 等)?

I've found out that a way to setthem is via the environment MAVEN_OPTS. Now I want a way to be assured it's getting the right settings.

我发现设置它们的方法是通过环境 MAVEN_OPTS。现在我想要一种方法来确保它获得正确的设置。

EDIT: I believe it′s different to this questionas 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 实际使用了哪些设置,无论它来自 env var、settings.xml 还是其他方式。

EDIT2: I'm also interested in other ways of setting Java options for a Maven build

EDIT2:我也对为 Maven 构建设置 Java 选项的其他方式感兴趣

回答by A_Di-Matteo

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 compileArgsconfiguration 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 on the third point.

插件配置:仅用于编译
使用Maven Compiler Plugin配置来编译应用程序代码和测试代码,您可以通过compileArgs配置入口设置所需的Xmx、Xms、Xss选项,可用于compiletestCompile目标。一位官员例子可在这里和其他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 argLineconfiguration 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 compileArgsand argLineconfiguration entry or have different properties per configuration (according to your needs).

插件配置:通过属性(和配置文件)
您可以将上面的两个选项(在常见 Java 选项的情况下)作为一个属性值传递给两个compileArgsargLine配置条目,或者每个配置具有不同的属性(根据您的需要)。

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

Global/Project configuration: Options file
Since Maven 3.3.1, you can specifyyour options in a .mvn/jvm.configfile 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 3.3.1 开始,您可以在每个项目的文件中指定您的选项.mvn/jvm.config。它是 MAVEN_OPTS 的替代方案,范围更窄(每个项目)。但是,由于它是Maven的一个新特性,人们应该注意它,否则可能会影响故障排除和维护。

Global/Env configuration: MAVEN_OPTS
Maven well-knownenvironment variable to set global execution options, however applied to all Maven builds sharing that environment (i.e. per logged user).

全局/环境配置:MAVEN_OPTS
Maven众所周知的环境变量,用于设置全局执行选项,但适用于共享该环境的所有 Maven 构建(即每个登录用户)。

When running Maven using the -Xoption (debug enabled), you will have the following output as part of your build:

使用该-X选项(启用调试)运行 Maven 时,您将获得以下输出作为构建的一部分:

[DEBUG] properties used {java.vendor=Oracle Corporation, ... , env.MAVEN_OPTS=-Xmx256M, ...


Update
After all, the executed mvncommand is an OS script. Having a look at it in Windows, I found the possibility of using the MAVEN_BATCH_ECHOoption which, if enabled (value set to on), will echo any command executed by the script and as such also the invocation of the javacommand, where you can see if your options (the MAVEN_OPTS) are picked up correctly together with the full list of parameters passed to it.

更新
毕竟,执行的mvn命令是一个操作系统脚本。有在Windows中看看吧,我发现使用的可能性MAVEN_BATCH_ECHO,如果启用(值设置为选项on),会响应由脚本,并为这些还调用执行任何命令java命令,在那里你可以,如果看到你选项 (the MAVEN_OPTS) 与传递给它的完整参数列表一起被正确选取。

Here is an execution I tested on Windows:

这是我在 Windows 上测试的执行:

set MAVEN_BATCH_ECHO=on
set MAVEN_OPTS=-Xmx256M
mvn compile > output.txt  

NOTE: the output.txtwill contain quite a lot of text, providing build output and additional echos executions. As part of it, it provided:

注意:output.txt将包含相当多的文本,提供构建输出和额外的echos 执行。作为其中的一部分,它提供了:

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

As you can see, the -Xmx256Moption 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 echobefore the javaexecution, showing the command, would also be enough).
You can find the maven script in your maven installation folder, under the binsubfolder.

如您所见,该-Xmx256M选项被正确选择。如果其他操作系统的 maven 脚本没有提供这个选项,那么你可以简单地将它添加到脚本中(echojava执行之前简单地显示命令也足够了)。
您可以在 maven 安装文件夹中的bin子文件夹下找到 maven 脚本。



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 answerby slightly changing the Maven script and use the jinfocommand which should really give you the answer according to this other SO answer.

Update2
此外,由于 Maven 毕竟是一个 Java 工具,并且正如您从它的脚本中看到的那样,它调用了 java 命令,您可以通过稍微更改 Maven 脚本并使用该命令来查看此SO 答案中建议的所有可用选项jinfo真的根据这个其他SO答案给你答案

回答by C-Otto

Maybe it helps to start Maven with verbose debug output (-debug, I think?). Otherwise just do a ps aux | grep javaand check the arguments of the process (assuming *nix).

也许使用详细的调试输出启动 Maven 会有所帮助(我认为是 -debug?)。否则只需执行 aps aux | grep java并检查进程的参数(假设 *nix)。