Luo Hao

使用spring-javaformat格式化项目代码

rehoni / 2023-11-01


参考

一步步降低代码复杂度

GitHub仓库

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

image-20230922191109583

然后idea安装插件,选择下载好的jar包,导入插件

image-20230922191658212

5、验证

image-20230831175303072

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

image-20230831183447975

4、验证

image-20240612142717493