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

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

相关推荐
fly-phantomWing2 小时前
Maven的安装与配置的详细步骤
java·后端·maven·intellij-idea
梦想blog5 小时前
搭建 Nexus3 私服并配置第三方 Maven 仓库(阿里云等)和优先级
maven·proxy·私服·aliyun·nexus3·settings.xml
2401_841495645 小时前
【数据结构】红黑树的基本操作
java·数据结构·c++·python·算法·红黑树·二叉搜索树
学编程的小鬼5 小时前
SpringBoot 自动装配原理剖析
java·spring boot·后端
@@神农7 小时前
maven的概述以及在mac安装配置
java·macos·maven
杜子不疼.7 小时前
【C++】玩转模板:进阶之路
java·开发语言·c++
夜晚中的人海7 小时前
【C++】异常介绍
android·java·c++
Le1Yu7 小时前
2025-9-28学习笔记
java·笔记·学习
C++chaofan8 小时前
项目中为AI添加对话记忆
java·数据结构·人工智能·redis·缓存·个人开发·caffeine
老华带你飞8 小时前
机电公司管理小程序|基于微信小程序的机电公司管理小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·微信小程序·小程序·机电公司管理小程序