mavensurefireplugin运行时的JVM跟踪与分析

操作系统:CentOS

平台:Jenkins

1. 启动Jenkins时,会启动一个JVM:

jenkins 12280 117 16:29 ?        00:00:54 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon --httpPort=8080 --ajp13Port=8009 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20

2. 当运行JOB时,Jenkins进程会另外启动一个JVM:

jenkins  14119 12280 96 17:01 ?        00:01:38 java -Xms128m -Xmx256m -XX:PermSize=256m -XX:MaxPermSize=256m -cp /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-agent-1.2.jar:/usr/local/apache-maven-3.0.3/boot/plexus-classworlds-2.4.jar org.jvnet.hudson.maven3.agent.Maven3Main /usr/local/apache-maven-3.0.3 /var/cache/jenkins/war/WEB-INF/lib/remoting-2.22.jar /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-1.2.jar 52626

这里的“-Xms128m -Xmx256m -XX:PermSize=256m -XX:MaxPermSize=256m”是在“Jenkins系统设置”-->“Maven项目配置”-->“全局MAVEN_OPTS”里配置的。见下图:

3. 当mavn-surefire-plugin启动时,JOB进程也会另外启动一个JVM:

jenkins 14348 14119 0 17:02 ? 00:00:00 /bin/sh -c cd /var/lib/jenkins/workspace/<job name>/<module name> && /usr/j2sdk/jre/bin/java -jar /var/lib/jenkins/workspace/<job name>/<module name>/target/surefire/surefirebooter5980952236884430359.jar /var/lib/jenkins/workspace/<job name>/<module name>/target/surefire/surefire5154789630052038357tmp /var/lib/jenkins/workspace/<job name>/<module name>/target/surefire/surefire4664955520833144490tmp
jenkins 14351 14348 71 17:02 ? 00:00:17 /usr/j2sdk/jre/bin/java -jar /var/lib/jenkins/workspace/<job name>/<module name>/target/surefire/surefirebooter5980952236884430359.jar /var/lib/jenkins/workspace/<job name>/<module name>/target/surefire/surefire5154789630052038357tmp /var/lib/jenkins/workspace/<job name>/<module name>/target/surefire/surefire4664955520833144490tmp

可以看到,MAVEN_OPTS并没有被继承过来。这是maven的默认设置,官网的说明如下:

jvm  String 2.1 Option to specify the jvm (or path to the java executable) to use with the forking options. 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.
User property isjvm.
forkCount String 2.14 Option to specify the number of VMs to fork in parallel in order to execute the tests. When terminated with "C", the number part is multiplied with the number of CPU cores. Floating point value are only accepted together with "C". If set to "0", no VM is forked and all tests are executed within the main process.

Example values: "1.5C", "4"

The system properties and the argLine of the forked processes may contain the place holder string${surefire.forkNumber}, which is replaced with a fixed number for each of the parallel forks, ranging from 1to the effective value of forkCount times the maximum number of parallel Surefire executions in maven parallel builds, i.e. the effective value of the -T command line argument of maven core.
Default value is1.
User property isforkCount.
reuseForks boolean 2.13 Indicates if forked VMs can be reused. If set to "false", a new VM is forked for each test class to be executed. If set to "true", up to forkCount VMs will be forked and then reused to execute all tests.
Default value istrue.
User property isreuseForks.

备注:

  • 这里的"reuseForks"指的是在一个Test结束后,其JVM不会立马关闭,下一个Test会继续重用这个JVM。直到maven-surefire-plugin结束后,JVM再关闭。
  • 在多module的maven项目结构中,每个叶子节点上的子module,都会有各自的maven-surefire-plugin。也就说,每个子moduel在运行maven-surefire-plugin时,都会重新创建一个新的JVM。

结论:

在mvn命令行或者系统环境变量中设置的“MAVEN_OPTS”,并不能影响到maven-surefire-plugin创建的JVM,必须直接在maven-surefire-plugin里配置“argLine”。如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>-Xms256m -Xmx256m -XX:PermSize=256M -XX:MaxPermSize=256M</argLine>
    </configuration>
</plugin>
原文地址:https://www.cnblogs.com/zhangqingsh/p/2966441.html