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的单元测试类不会参与打包

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

相关推荐
梦梦代码精10 小时前
电商系统不是技术堆叠:LikeShop如何用分层Hold住复杂业务?
java·docker·代码规范
负责的蛋挞10 小时前
异步HttpModule的实现方式
java·服务器·前端
AC赳赳老秦10 小时前
防火墙规则批量配置实战:OpenClaw 自动生成模板、批量下发与合规性校验全解析
java·开发语言·人工智能·python·github·php·openclaw
Tian_Hang10 小时前
Eclipse Ditto 物模型相关代码
java·运维·服务器·ide·eureka·eclipse
Mr-Wanter11 小时前
wsl2 jdk管理工具之sdkman
java·开发语言·sdkman
唐青枫12 小时前
Java Future 与 CompletableFuture 实战指南:从异步结果到任务编排
java
长孙豪翔12 小时前
在.net中读写config文件的各种方法
java·数据库·.net
tachibana212 小时前
hot100 回文链表(234)
java·网络·数据结构·leetcode·链表
可乐ea12 小时前
【Java八股|第10篇】Java 中的包装类和自动拆装箱
java·面试题·包装类·java八股
zfoo-framework12 小时前
mongo最佳实战(from mongo中文社区)
java