源码
Maven 配置
<build>
<plugins>
<plugin>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>0.0.47</version>
<executions>
<execution>
<phase>validate</phase>
<inherited>true</inherited>
<goals>
<!-- 强制代码必须符合要求,否则不允许构建(打包),会提示需要运行 mvn spring-javaformat:apply 命令 -->
<goal>validate</goal>
<!-- 自动格式化,不推荐(如果代码打包时,使用了此选项,可能导致生产中的异常堆栈行号与 git 中的代码行号无法对应) -->
<!--<goal>apply</goal>-->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
基于 JDK 8
- 在项目根目录添加
.springjavaformatconfig 文件:
# 兼容使用 JDK 8 的 Java 代码格式化配置
java-baseline=8
执行命令进行代码格式化
mvn spring-javaformat:apply
其他命令
# 跳过所有 spring-javaformat 配置
mvn clean package -Dspring-javaformat.skip=true
# 跳过验证(跳过上方配置中的 validate)
mvn clean package -Dspring-javaformat.validate.skip=true
# 跳过自动格式化(跳过上方配置中的 apply)
-Dspring-javaformat.apply.skip=true
跳过某个模块
<properties>
<!-- 跳过该 Maven 模块的 所有 spring-javaformat 配置 -->
<spring-javaformat.skip>true</spring-javaformat.skip>
<!-- 跳过该 Maven 模块的 验证 -->
<!--<spring-javaformat.apply.skip>true</spring-javaformat.apply.skip>-->
<!-- 跳过该 Maven 模块的 自动格式化 -->
<!--<spring-javaformat.validate.skip>true</spring-javaformat.validate.skip>-->
</properties>
IDEA 插件