一次由 Maven 可重复构建引发的 Spring Boot 运行时类加载问题排查
背景
在一个 Spring Boot 3.x 项目中,为了满足可重复构建(Reproducible Build)的要求(可查看我另一篇文章了解可重复构建),我在 pom.xml 中新增了如下配置:
<project.build.outputTimestamp>2026-01-01T00:00:00Z</project.build.outputTimestamp>该配置用于固定构建产物的时间戳,确保多次构建生成的 JAR 文件在字节级别保持一致。
然而,加上该配置后,应用在运行时出现了异常:
- 启动正常
- 业务请求触发时抛出运行时错误
- 错误与 okhttp 相关
jakarta.servlet.ServletException: Handler dispatch failed: java.lang.NoClassDefFoundError: Could not initialize class com.xxx.common.util.HttpUtil
...
Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.xxx.common.util.HttpUtil
...
Caused by: java.lang.ExceptionInInitializerError: Exception java.lang.NoSuchFieldError: Companion [in thread "http-nio-80-exec-9"]
at okhttp3.internal.Util.<clinit>(Util.kt:70) ~[okhttp-4.10.0.jar!/:?]而在未添加该配置时,应用运行完全正常。