springboot~代碼風格統計spring-javaformat-maven-plugin插件
Spring Javaformat是Spring團隊開(kai)發的(de)一款基于Google Java Format的(de)代(dai)碼(ma)(ma)(ma)格式化工(gong)具,可以幫(bang)助Java開(kai)發者(zhe)自動(dong)化進行代(dai)碼(ma)(ma)(ma)格式化,提(ti)高(gao)代(dai)碼(ma)(ma)(ma)質量,降低代(dai)碼(ma)(ma)(ma)維(wei)護成(cheng)本。
Spring Javaformat可以(yi)自定(ding)義配(pei)(pei)置文件,支持大量的代碼風格配(pei)(pei)置選項(xiang),滿(man)足不同項(xiang)目和(he)團(tuan)隊的需求。還可以(yi)與(yu)Gradle、Maven、IntelliJ IDEA等常用(yong)(yong)開(kai)發工具集(ji)成(cheng)使用(yong)(yong),方便快速進行代碼格式(shi)化(hua)。
此(ci)外,Spring Javaformat還支(zhi)持CI/CD流程中(zhong)的代碼(ma)格式化,可以與Git、Jenkins等(deng)工具集成,確(que)保(bao)在代碼(ma)合(he)并、構建(jian)和(he)部(bu)署過(guo)程中(zhong),代碼(ma)格式保(bao)持一致。
添加依賴
在你的(de)基礎項目的(de)pom.xml文件(jian)中(zhong),添加javaformat的(de)依(yi)賴
<!--代碼格式插件,默認使用spring 規則 ./mvnw spring-javaformat:apply -->
<plugin>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>${spring.checkstyle.plugin}</version>
<executions>
<execution>
<phase>validate</phase>
<inherited>true</inherited>
<goals>
<goal>validate</goal>
</goals>
</execution>
</executions>
</plugin>
代碼格式化
- 可以使用命令行工具進行格式化
./mvnw spring-javaformat:apply - 也可以使用項目下的插件
spring-javaformat:apply,如圖:

