升级OpenJDK 17后Maven Surefire测试报错求助
解决方案:OpenJDK 17升级后Surefire测试失败问题
针对NoClassDefFoundError: InPluginProcessDumpSingleton错误
原因
useSystemClassLoader=false配置在Java 17+与早期里程碑版Surefire(如3.0.0-M5)组合时,会破坏类加载路径,导致Surefire无法找到自身内部类。Java 17的模块系统与该配置存在兼容性冲突。
修改方案
- 升级Surefire插件到稳定兼容版本:将
maven-surefire-plugin版本从3.0.0-M5升级到3.2.5(当前稳定版,完美支持Java 17)。 - 移除或调整
useSystemClassLoader:默认值为true,移除该配置即可,避免关闭系统类加载器导致的类加载异常。
针对进程退出码137(VM被终止)错误
原因
退出码137通常表示JVM因内存不足被系统OOM Killer终止。结合forkCount=2与-Xmx5g的配置,同时运行2个5G堆内存的JVM,总内存需求超过了构建环境的可用内存配额。
修改方案
- 降低并行fork数量:将
forkCount从2改为1,减少同时运行的JVM实例,降低总内存占用。 - 调整JVM堆内存:根据构建环境的可用内存,将
-Xmx从5g下调到3g或2g,避免内存过载。 - 添加GC日志排查:在
argLine中加入-Xlog:gc*:file=target/gc-test.log,生成GC日志用于确认是否为OOM问题。 - 检查环境内存限制:如果是CI/CD环境,确认平台是否有内存配额限制,确保配置的内存不超过平台允许值。
修改后的完整Maven配置示例
<build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.8</version> <executions> <execution> <id>default-prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> <configuration> <append>true</append> <propertyName>surefireJacocoArgs</propertyName> <includes> <include>org.somepackage.*</include> </includes> </configuration> </execution> <execution> <id>jacoco-report</id> <phase>test</phase> <goals> <goal>report</goal> </goals> <configuration> <outputDirectory>target/jacoco-reports</outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.5</version> <configuration> <forkCount>1</forkCount> <reuseForks>true</reuseForks> <failIfNoTests>false</failIfNoTests> <trimStackTrace>false</trimStackTrace> <redirectTestOutputToFile>true</redirectTestOutputToFile> <excludes> <exclude>**/*File.java</exclude> </excludes> <runOrder>alphabetical</runOrder> <rerunFailingTestsCount>5</rerunFailingTestsCount> <argLine> ${surefireJacocoArgs} ${argline-add-open} -Xmx3g -Xlog:gc*:file=target/gc-test.log </argLine> </configuration> </plugin> </plugins> </build>
内容的提问来源于stack exchange,提问作者Abhinav Manthri
相关产品推荐
相关产品推荐

