Find Java options used by Maven

随声附和 提交于 2019-12-08 15:42:41

问题


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

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.

EDIT: 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.

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


回答1:


You can set Java options for Maven in different points and level (globally or via plugins configuration):

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.

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.

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

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.

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

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

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.

Here is an execution I tested on Windows:

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

NOTE: the output.txt will contain quite a lot of text, providing build output and additional echos 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 

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




回答2:


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



来源:https://stackoverflow.com/questions/34739321/find-java-options-used-by-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!