Maven打包SpringBoot项目,因包含SpringBootTest单元测试和Java预览版特性导致打包失败

SpringBoot启用Java预览版特性(无测试类)

在pom.xml文件中加入以下配置表示启用Java预览版

xml 复制代码
 <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <configuration>
         <release>${java.version}</release>
         <encoding>UTF-8</encoding>
         <compilerArgs>
             <arg>--enable-preview</arg>
         </compilerArgs>
     </configuration>
 </plugin>

当项目中包含此配置,若无SpringBootTest测试类,则能正常编译打包

SpringBoot启用Java预览版特性(含测试类)

若项目中包含单元测试则需要额外配置--enable-preview参数

xml 复制代码
 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <release>${java.version}</release>
        <encoding>UTF-8</encoding>
        <compilerArgs>
            <arg>--enable-preview</arg>
        </compilerArgs>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>--enable-preview</argLine>
    </configuration>
</plugin>

Maven package与单元测试的关系

Maven执行package前会执行一次test

由于spring-boot-starter-test依赖的作用域是test,SpringBootTest的单元测试类不会参与打包

但若是单元测试不通过,则会导致打包失败

相关推荐
千寻技术帮6 小时前
微信小程序+Spring Boot 烘焙商城系统(附完整源码+数据库+部署文档)50053
数据库·spring boot·微信小程序·烘焙商城
会编程的吕洞宾7 小时前
Spring AI 2.0 工具调用实战:让 AI 自动调用你的 Java 方法
java·人工智能·经验分享·后端·spring
斯维赤7 小时前
SELECT * 为什么是覆盖索引的天敌?
java·后端·mysql
stormsha7 小时前
库存 5,10 个并发各买 1 件:为什么最后不剩 -5 件
java·数据库·mysql
RobinXYuan8 小时前
【Java 字节码探秘】02 ClassFile 骨架:十个字段
java·jvm·源码阅读
炸薯条!8 小时前
Python面向对象基础:类与对象、实例方法、魔法方法和属性详解
java·开发语言·python
SimonKing9 小时前
又一款数据库同步神器,2.3K Star的dbswitch
java·后端·程序员
CoderYanger9 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第二弹):DispatcherServlet 源码分析+适配器模式
java·数据库·hive·spring boot·面试·java-ee·mybatis
Rain的Java大神之路9 小时前
前端转 Java 全栈:用 AI 两天交付一个能跑的 Demo,踩坑全记录
java·前端·后端
她的男孩10 小时前
状态默认填了 DRAFT,同步后却变成 '''DRAFT'':Forge Admin 清洗 PG 的 COLUMN_DEFAULT
spring boot·低代码·postgresql