核心是通过pom.xml配置surefire插件或properties实现自动跳过:在中设true,或在中声明true,spring boot项目可直接生效;如需彻底跳过编译与执行,则用true。

在 Maven 构建中自动跳过单元测试,核心是让跳过行为成为默认动作,无需每次手动加参数。关键在于区分“临时跳过”和“自动跳过”——前者靠命令行,后者靠配置固化。
通过 pom.xml 配置 surefire 插件(项目级生效)
这是最常用、最可靠的自动跳过方式。Maven 会读取配置并在每次执行 package、install 等生命周期时自动跳过测试执行(但保留编译):
- 在
<build><plugins></plugins></build>中添加或修改maven-surefire-plugin - 设置
<skiptests>true</skiptests> - Spring Boot 项目通常已内置该插件,只需补充 configuration 即可
<plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-surefire-plugin</artifactid><configuration><skiptests>true</skiptests></configuration></plugin>
用 properties 统一控制(推荐用于多模块或 Spring Boot)
把开关提到 <properties></properties> 里,更简洁且便于 Profile 切换:
在 Java 中初始化和管理阿里云 SDK客户端。包括单例模式、线程安全、endpoint 与 region 配置、VPC 终端节点、同步与异步等。
- 在
<properties></properties>中声明<skiptests>true</skiptests> - surefire 插件会自动识别该 property,无需额外配置插件
- 配合 Profile 可实现“开发环境跳过,CI 环境启用”的灵活策略
<properties><skiptests>true</skiptests></properties>
永久禁用测试编译与执行(更彻底的跳过)
如果连测试代码都不想编译(比如 CI 流水线中明确不需要测试产物),可用 maven.test.skip 属性:
- 在 pom.xml 的
<properties></properties>中设为true - 效果等同于命令行加
-Dmaven.test.skip=true - 注意:这会导致
src/test/**不参与编译,target/test-classes为空
<properties><maven.test.skip>true</maven.test.skip></properties>
IDE 中设为默认行为(开发环境友好)
避免每次右键运行都手输参数:
- IntelliJ IDEA:Settings → Build, Execution, Deployment → Maven → Runner → 勾选 “Skip Tests”
- Eclipse:Run Configurations → Maven Build → Goals 输入
package -DskipTests,并保存为默认 - VS Code + Maven Extension:在
settings.json中配置"maven.executable.options": "-DskipTests"
Java免费学习笔记:立即使用
解锁 Java 大师之旅:从入门到精通的终极指南










