1. 序言
- 之前的《maven实战》学习笔记中,有关于maven test的基础知识
- 在本地对组件代码进行单元测试时,测试完成后
maven-surefire-plugin
会在target目录生成surefire-reports
文件夹,里面记录了测试报告 - 点击index.html文件,直接查看测试报告
- 测试报告中,有统计这次测试总的方法数、pass的方法数、failed的方法数、skipped的方法数
2. OOM:unable to create new thread
- 某个模块在本地执行单元测试后,其测试报告中,大量出现如下异常
java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:658)
at java.util.concurrent.ThreadPoolExecutor.addIfUnderMaximumPoolSize(ThreadPoolExecutor.java:727)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:657)
...
2.1 调整JVM配置
- 一看到OOM就想到的是内存不足,需要修改JVM的配置
- 这是在使用maven进行单元测试,该如何修改maven的JVM配置呢?
- 从网上查阅的资料看,主要有两种途径进行JVM参数配置
2.1.1 方法一: 设置maven-surefire-plugin
- 最简单方法,直接通过argLine为maven-surefire-plugin设置JVM参数
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Xms4g -Xmx4g</argLine>
</configuration>
</plugin>
- 按照网上的说法,argLine有late replacement of properties的问题:当前设置的argLine将会覆盖之前(如继承自父模块)的设置
- 此时,需要使用append的方法进行设置,避免argLine发生覆盖
append argLine方法一
- 使用
@{xxx}
语法实现argLine的late property evaluation
<properties>
<argLine></argLine>
</properties>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>@{argLine} -your -extra -arguments</argLine>
</configuration>
</plugin>
- 如果出现报错:Could not find or load main class @{argLine},可能是late property evaluation 或 jacoco-maven-plugin没有执行,可以提前定义一个空的argLine属性
<properties>
<argLine></argLine>
</properties>
append argLine方法二
- 不通过maven-surefire-plugin --> configuration设置argLine,而是直接创建argLine属性
<properties>
<argLine>-your -extra -arguments</argLine>
</properties>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- no argLine here -->
</configuration>
</plugin>
- 以上设置方法,参考如下连接:
- jacoco:prepare-agent
- Append the value of argLine param in maven-surefire-plugin
- Define argLine property rather than setting it in surefire, to ease integration with other plugins
2.1.2 方法二:设置MAVEN_OPTS
- maven test阶段默认使用maven-surefire-plugin插件,除非需要自定义某些属性,一般在pom.xml文件中,是找不到maven-surefire-plugin
- 如果找不到,又不想更改pom.xml文件,这时可以选择设置MAVEN_OPTS
- 对于linux系统,可以通过修改
/etc/profile
文件,添加MAVEN_OPTS以实现JVM的配置(这是永久性的修改)
export MAVEN_OPTS="-Xss16m -Xms4G -Xmx16G"
- 或者直接执行上述内容,进行一次性修改
- 修改是否生效,可以通过export命令进行查看
2.2 OOM原因排查
- 遇到OOM: unable to create new thread,调整JVM配置不一定是最优的,需要具体情况具体分析
- 分析方法可以参考之前的博客:java.lang.OutOfMemoryError: unable to create new native thread原因排查
3. [INFO] Scanning for projects…卡住
- 在使用mvn clean test进行单元测试时,发现真整个执行过程在输出[INFO] Scanning for projects...就一动不动了
- 通过添加-X选项,查看maven的debug的日志,也看不出明显的问题
- 网上搜索了很多方法,没有一个可以使用的
- Maven “scanning for projects…” freezes, stuck, never ending… It can not write out even the next info line ( ------ )
- 自己还是登陆服务器在跑单元测试,甚至将/etc/profile中的MAVEN_HOME给注释掉了,然后又恢复都没用
- 最后,死马当活马医,把本地配置的repo目录给删除了,竟然ok了 😂
- 不知道这招对其他人是否管用,在这里记录一下