You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用native-maven-plugin打包Spring Boot 3.0时遇ClassNotFoundException的解决方法

解决方案

问题根源

Spring Boot 3.0基于Spring Framework 6.x,org.springframework.test.context.web.WebAppConfiguration 这个类在Spring Framework 5.2就已被标记为弃用,Spring Boot 3.0中已完全移除该类,你的测试代码或依赖中仍在引用它,导致原生打包时触发ClassNotFoundException。

具体修复步骤

  1. 替换测试类中的旧注解
    找到项目中所有使用@WebAppConfiguration的测试类,移除该注解,改用Spring Boot 3.0支持的web环境配置方式:

    // 移除@WebAppConfiguration
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) // 按需选择合适的web环境
    public class YourTestClass {
        // 测试代码
    }
    

    可选的web环境参数:RANDOM_PORT、DEFINED_PORT、MOCK、NONE,根据测试场景选择。

  2. 检查并统一依赖版本
    打开pom.xml,确保所有Spring相关依赖(包括Spring Test)的版本与Spring Boot 3.0的版本一致,避免引入旧版本依赖:

    • 确认spring-boot-starter-parent的版本为3.0.x
    • 移除任何单独声明的spring-test依赖(通过starter自动引入即可)
    • 执行mvn dependency:tree命令排查依赖冲突,排除掉第三方依赖间接引入的旧版本Spring相关依赖。
  3. 清理缓存并重新构建
    执行以下命令清理Maven缓存,避免旧依赖残留:

    mvn clean install -DskipTests
    

    完成后再执行原生打包命令:

    mvn -Pnative package
    

内容的提问来源于stack exchange,提问作者Bob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 11:55:45