跳到主要内容

Spring Java Format

Gradle 项目中使用

build.gradle 配置

官方文档

build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "io.spring.javaformat:spring-javaformat-gradle-plugin:0.0.41"
}
}

plugins {
id "checkstyle"
}

checkstyle {
toolVersion = "9.3"
}

allprojects {
repositories {
mavenCentral()
}

apply plugin: 'checkstyle'
apply plugin: 'io.spring.javaformat'

dependencies {
checkstyle("io.spring.javaformat:spring-javaformat-checkstyle:0.0.41")
}
}

Spring Java Format 配置文件

.springjavaformatconfig
indentation-style=spaces

checkstyle 配置文件

config/checkstyle/checkstyle.xm
<?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" >
<property name="tabWidth" value="4" />
<property name="headerType" value="unchecked" />
<property name="excludes" value="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck" />
</module>
</module>

IntelliJ IDEA 使用

Spring Javaformat 插件

官方文档说明

  1. Maven Central 下载 spring-javaformat-intellij-idea-plugin-0.0.39.jar

  2. IntelliJ IDEA | Settings | Plugins | Install Plugin from Disk... 选择刚刚下载的 jar 文件,安装 Plugin

  3. 如果编辑器状态栏显示 图标,表示 Spring Formatter 正常启用

CheckStype-IDEA 插件

  1. 下载安装插件 https://plugins.jetbrains.com/plugin/1065-checkstyle-idea

  2. 下载 spring-javaformat-checkstyle-0.0.39.jar

  3. 下载 spring-javaformat-config-0.0.39.jar

  4. IntelliJ IDEA | Settings | Tools | Checkstyle 设置

  • Checkstyle version: 9.3
  • Configuration File Add config/checkstyle/checkstyle.xml
  • Third-Party Checks Add spring-javaformat-checkstyle-0.0.39.jar && spring-javaformat-config-0.0.39.jar