使用spring-javaformat格式化项目代码
rehoni / 2023-11-01
参考
maven
1、根pom下引入插件
<build>
<plugins>
<plugin>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>0.0.39</version>
</plugin>
</plugins>
</build>
2、修改.m2/settings.xml
<pluginGroups>
<pluginGroup>io.spring.javaformat</pluginGroup>
</pluginGroups>
3、支持java8
根pom同级下新增.springjavaformatconfig文件
java-baseline=8
4、安装idea的插件
这个网址下载,https://mvnrepository.com/artifact/io.spring.javaformat/spring-javaformat-intellij-idea-plugin/0.0.39
然后idea安装插件,选择下载好的jar包,导入插件
5、验证
-
有此插件
-
格式化代码
ctrl+alt+L
能生效 -
有此标志
checkStyle
1、maven引入插件(此步骤好像非必须)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.1</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>9.3</version>
</dependency>
<dependency>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-checkstyle</artifactId>
<version>0.0.39</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>checkstyle-validation</id>
<phase>validate</phase>
<inherited>true</inherited>
<configuration>
<configLocation>io/spring/javaformat/checkstyle/checkstyle.xml</configLocation>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
2、IDEA 安装 plugin
3、下载 checkstyle 和 config 的jar包
checkstyle 包下载地址 https://mvnrepository.com/artifact/io.spring.javaformat/spring-javaformat-checkstyle/0.0.39
config 包下载地址 https://mvnrepository.com/artifact/io.spring.javaformat/spring-javaformat-config/0.0.39
3、IDEA 配置 plugin
-
注意checkstyle的配置最好和官方一致,9.3。试过10.12.2,无法生效。
-
创建配置文件到本地
按照官方文档提供的配置
<?xml version="1.0"?> <!DOCTYPE module PUBLIC "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN" "https://checkstyle.org/dtds/configuration_1_3.dtd"> <module name="com.puppycrawl.tools.checkstyle.Checker"> <module name="io.spring.javaformat.checkstyle.SpringChecks" /> </module>
-
导入第三方检查
4、验证