Search code examples
mavenmaven-pluginmaven-surefire-pluginjvm-arguments

Does maven surfire argLine override ./mvn/jvm.config?


I have a project with the following ./mvn/jvm.config :

-Xms32g -Xmx64g -XX:MaxDirectMemorySize=20g

I was wondering if I configure my maven surfire plugin as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.9</version>
    <configuration>
        <argLine>-Xms12g -Xmx30g -XX:MaxDirectMemorySize=30g</argLine>
        <suiteXmlFiles>
            <suiteXmlFile>src/test/resources/RunMmoTests.xml</suiteXmlFile>
        </suiteXmlFiles>
    </configuration>
</plugin> 

I know that my jvm.config will set the MAVEN_OPTS environment variable, but I can't find any documentation stating the precedence between MAVEN_OPTS and surfire argLine. Which one override the other? After running some tests, I have the feeling that the surfire argLine override whatever argument defined in MAVEN_OPTS. Is my guess correct??

Many thanks


Solution

  • If argLine does not define JVM options, will surefire forked jvm inherit the jvm options defined in .mvn/jvm.config?

    It will not. You may have completely different requirements for building the project than for running the tests.

    Here the comment in the source code of maven-surefire:

    For the default, the JVM will be a new instance of the same VM as the one used to run Maven. JVM settings are not inherited from MAVEN_OPTS.

    When we invoke the mvn command (mvn.cmd or bin/mvn) Maven reads and sets different environment variables and does it differently in Linux and Windows.

    • On Linux, the mvn command sets the MAVEN_OPTS variable by concatenating the existing MAVEN_OPTS with the $MAVEN_PROJECTBASEDIR/.mvn/jvm.config file and then invokes java passing the MAVEN_OPTS as a parameter.
    • On Windows, the mvn command sets JVM_CONFIG_MAVEN_PROPS with the content of jvm.config and then invokes java passing MAVEN_OPTS and JVM_CONFIG_MAVEN_PROPS

    The surefire plugin forks a separate process for executing test by invoking java command but it will not pass the MAVEN_OPTS or JVM_CONFIG_MAVEN_PROPS as parameters to JVM.

    If you want to set the same JVM parameters for running test as for running maven then you can use ${parameters} in argLines. For example like this:

    <configuration>
      <argLine>${MAVEN_OPTS}</argLine>
    </configuration>