Maven项目JDK8升级至JDK11后测试用例执行失败求助
JDK8升级至11后Maven测试用例执行失败的解决方案
问题描述
将Maven项目JDK版本从1.8升级至11后,测试用例执行失败,错误如下:

项目相关配置:
Profiles配置
<profiles> <profile> <id>surefire</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <suiteXmlFiles> <suiteXmlFile>TestNG.xml</suiteXmlFile> </suiteXmlFiles> <argLine> --add-opens java.xml/jdk.xml.internal=ALL-UNNAMED -Xmx1024m</argLine> <forkCount>1</forkCount> </configuration> </plugin> </plugins> </build> </profile> </profiles>
PluginManagement配置
<pluginManagement> <plugins> <!-- not required this plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <skipTests>false</skipTests> <argLine> --add-opens java.xml/jdk.xml.internal=ALL-UNNAMED </argLine> <excludes> <exclude>**/*RegressionTest.java</exclude> </excludes> <forkCount>1</forkCount> </configuration> </plugin> <plugin> <groupId>org.sonarsource.scanner.maven</groupId> <artifactId>sonar-maven-plugin</artifactId> <version>3.3.0.603</version> </plugin> </plugins> </pluginManagement>
已尝试修改maven-surefire-plugin版本,但问题未彻底解决。
解决方案
针对JDK11与Maven Surefire插件的兼容问题,按以下步骤修复:
1. 升级Maven Surefire插件到JDK11兼容版本
2.22.2版本对JDK11支持有限,建议升级至3.0.0-M7及以上稳定版(如3.2.5),新版本优化了模块化JDK的适配逻辑。
修改PluginManagement和Profiles中的插件版本:
<version>3.2.5</version>
2. 统一argLine配置避免冲突
当前Profiles与PluginManagement中重复配置argLine,会导致参数叠加或覆盖。可通过以下方式统一:
- 若使用
surefireProfile,移除PluginManagement中的argLine,仅在Profile中保留完整配置; - 或使用
@{argLine}继承全局参数,避免重复定义:
Profile中修改argLine:
<argLine>@{argLine} -Xmx1024m</argLine>
PluginManagement中保留模块开放参数:
<argLine>--add-opens java.xml/jdk.xml.internal=ALL-UNNAMED</argLine>
3. 配置Maven编译插件适配JDK11
确保maven-compiler-plugin也适配JDK11,添加到build/plugins中:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>11</source> <target>11</target> <encoding>UTF-8</encoding> </configuration> </plugin>
4. 验证TestNG版本兼容性
若使用TestNG,需确保版本支持JDK11,推荐7.0.0及以上版本,更新依赖:
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.7.1</version> <scope>test</scope> </dependency>
5. 清理缓存重新构建
执行命令清理Maven缓存并重新运行测试:
mvn clean install -Psurefire
内容的提问来源于stack exchange,提问作者user20574819
相关产品推荐
相关产品推荐

