Maven 项目在不同 JDK 版本下的兼容性问题及解决方案
引言
在日常开发中,Maven 是 Java 项目构建和管理的核心工具。然而,随着 JDK 版本的不断更新,开发者可能会遇到一些兼容性问题。例如,某些 Maven 插件在特定 JDK 版本下无法正常工作,或者依赖下载失败。本文将通过一个实际案例,分析 Maven 项目在不同 JDK 版本下的兼容性问题,并提供解决方案。
问题描述
最近,我在开发一个 Maven 项目时遇到了一个奇怪的问题:同样的项目,在 OpenJDK 9 下运行 mvn test
时失败,而在 OracleJDK 8 下却能成功运行。以下是问题的详细日志:
OpenJDK 9 的失败日志
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project maven-app: Unable to generate classpath: org.apache.maven.artifact.resolver.ArtifactResolutionException: Unable to get dependency information for org.apache.maven.surefire:surefire-junit4:jar:2.12.4: Failed to retrieve POM for org.apache.maven.surefire:surefire-junit4:jar:2.12.4: Could not transfer artifact org.apache.maven.surefire:surefire-junit4:pom:2.12.4 from/to alimaven (http://maven.aliyun.com/nexus/content/groups/public/): java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
OracleJDK 8 的成功日志
[INFO] BUILD SUCCESS
从日志中可以看出,问题出在 OpenJDK 9 下无法下载依赖,而 OracleJDK 8 下则一切正常。
问题分析
1. OpenJDK 9 的 SSL/TLS 问题
OpenJDK 9 引入了模块化系统(Jigsaw),并对 SSL/TLS 的默认配置进行了一些更改。具体来说,OpenJDK 9 的信任库(cacerts
)可能为空或损坏,导致无法验证远程 Maven 仓库的 SSL 证书。
错误日志中的关键信息:
java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
这表明 OpenJDK 9 的 SSL/TLS 配置与 Maven 插件不兼容。
2. OracleJDK 8 的兼容性
OracleJDK 8 的 SSL/TLS 实现更稳定,且与 Maven 插件(如 maven-surefire-plugin
)兼容性更好。因此,切换到 OracleJDK 8 后,问题得到解决。
3. Maven 插件版本
你使用的 maven-surefire-plugin
版本是 2.12.4,这个版本在 OpenJDK 9 下可能存在兼容性问题。虽然最终是 JDK 8 解决了问题,但插件版本也是一个值得关注的因素。
解决方案
1. 切换到 OracleJDK 8 或更高版本的 JDK
如果你不需要使用 OpenJDK 9 的特定功能,建议继续使用 OracleJDK 8 或更高版本的 JDK(如 OpenJDK 11 或 OpenJDK 17)。确保 Maven 和项目的 JDK 版本一致,避免兼容性问题。
2. 修复 OpenJDK 9 的信任库
如果你仍然需要使用 OpenJDK 9,可以尝试修复其信任库(cacerts
)。
- 使用以下命令检查信任库是否为空:
keytool -list -keystore $JAVA_HOME/lib/security/cacerts
- 如果为空,可以从 OracleJDK 8 或其他可靠来源复制
cacerts
文件。
3. 升级 Maven 插件
保持 Maven 插件(如 maven-surefire-plugin
)的最新版本,以确保与 JDK 的兼容性。例如,将 maven-surefire-plugin
升级到 3.0.0-M5:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>
4. 使用 HTTPS 仓库
如果问题出在 SSL 证书验证上,可以尝试修复 JDK 的信任库,而不是将仓库地址改为 HTTP。
总结
本文通过一个实际案例,分析了 Maven 项目在不同 JDK 版本下的兼容性问题。问题的根本原因是 OpenJDK 9 的 SSL/TLS 配置与 Maven 插件不兼容。通过切换到 OracleJDK 8、修复信任库或升级 Maven 插件,可以有效解决此类问题。
在开发过程中,保持 JDK 版本和 Maven 插件的兼容性非常重要。如果遇到类似问题,可以参考本文的解决方案进行排查和修复。
参考资料
- Maven Surefire Plugin Documentation
- Java Keytool Documentation
- OpenJDK vs OracleJDK: What's the Difference?
希望这篇博客能帮助到遇到类似问题的开发者!如果你有任何疑问或建议,欢迎在评论区留言。
D:\JavaDevelopmentKit\OpenJDK\jdk-9\bin\java.exe -Dmaven.multiModuleProjectDirectory=D:\DevProjects\IDEA2018\maven-app -Dmaven.home=D:\Apache\Maven\apache-maven-3.6.0 -Dclassworlds.conf=D:\Apache\Maven\apache-maven-3.6.0\bin\m2.conf "-javaagent:D:\JetBrains\IntelliJ IDEA\IntelliJ IDEA 2018.1.8\lib\idea_rt.jar=50147:D:\JetBrains\IntelliJ IDEA\IntelliJ IDEA 2018.1.8\bin" -Dfile.encoding=UTF-8 -classpath D:\Apache\Maven\apache-maven-3.6.0\boot\plexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher -Didea.version=2018.1.8 -s D:\Apache\Maven\apache-maven-3.6.0\conf\settings.xml -Dmaven.repo.local=D:\Apache\Maven\apache-maven-3.6.0\maven_repository test -f pom.xml
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.ithero:maven-app >------------------------
[INFO] Building maven-app 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-app ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ maven-app ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-app ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\DevProjects\IDEA2018\maven-app\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ maven-app ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-app ---
[INFO] Surefire report directory: D:\DevProjects\IDEA2018\maven-app\target\surefire-reports
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.pom
[INFO] Failure detected.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.106 s
[INFO] Finished at: 2024-12-17T18:26:29+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project maven-app: Unable to generate classpath: org.apache.maven.artifact.resolver.ArtifactResolutionException: Unable to get dependency information for org.apache.maven.surefire:surefire-junit4:jar:2.12.4: Failed to retrieve POM for org.apache.maven.surefire:surefire-junit4:jar:2.12.4: Could not transfer artifact org.apache.maven.surefire:surefire-junit4:pom:2.12.4 from/to alimaven (http://maven.aliyun.com/nexus/content/groups/public/): java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
[ERROR] org.apache.maven.surefire:surefire-junit4:jar:2.12.4
[ERROR]
[ERROR] from the specified remote repositories:
[ERROR] alimaven (http://maven.aliyun.com/nexus/content/groups/public/, releases=true, snapshots=false)
[ERROR] Path to dependency:
[ERROR] 1) dummy:dummy:jar:1.0
[ERROR]
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Process finished with exit code 1
D:\JavaDevelopmentKit\OracleJDK\Java8\jdk-1.8\bin\java.exe -Dmaven.multiModuleProjectDirectory=D:\DevProjects\IDEA2018\maven-app -Dmaven.home=D:\Apache\Maven\apache-maven-3.6.0 -Dclassworlds.conf=D:\Apache\Maven\apache-maven-3.6.0\bin\m2.conf "-javaagent:D:\JetBrains\IntelliJ IDEA\IntelliJ IDEA 2018.1.8\lib\idea_rt.jar=50160:D:\JetBrains\IntelliJ IDEA\IntelliJ IDEA 2018.1.8\bin" -Dfile.encoding=UTF-8 -classpath D:\Apache\Maven\apache-maven-3.6.0\boot\plexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher -Didea.version=2018.1.8 -s D:\Apache\Maven\apache-maven-3.6.0\conf\settings.xml -Dmaven.repo.local=D:\Apache\Maven\apache-maven-3.6.0\maven_repository test -f pom.xml
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.ithero:maven-app >------------------------
[INFO] Building maven-app 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-app ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ maven-app ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-app ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\DevProjects\IDEA2018\maven-app\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ maven-app ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-app ---
[INFO] Surefire report directory: D:\DevProjects\IDEA2018\maven-app\target\surefire-reports
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.pom
Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.pom (2.4 kB at 2.2 kB/s)
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom
Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom (2.3 kB at 8.4 kB/s)
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.jar
Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.jar (37 kB at 98 kB/s)
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.ithero.test.MavenAppTest
Hello Maven~~~
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.11 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.044 s
[INFO] Finished at: 2024-12-17T18:27:57+08:00
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
标签:INFO,maven,JDK,surefire,Maven,兼容性问题,apache,2.12
From: https://www.cnblogs.com/itcq1024/p/18613225