# 简介

spring风格格式化代码

官网地址 https://github.com/spring-io/spring-javaformat (opens new window)

# maven项目整合的3中方式

spring-javaformat-maven-plugin依赖于https://simulation.tudelft.nl/maven/这个仓库中包,要配置到settings.xml

# 1. 手动执行命令

在pom.xml中添加plugin

<build>
	<plugins>
		<plugin>
			<groupId>io.spring.javaformat</groupId>
			<artifactId>spring-javaformat-maven-plugin</artifactId>
			<version>0.0.22</version>
		</plugin>
	</plugins>
</build>

或者在settings.xml中配置

<pluginGroups>
	<pluginGroup>io.spring.javaformat</pluginGroup>
</pluginGroups>

此时,就可以执行mvn spring-javaformat:apply命令来格式化代码了

# 强制格式化代码

<build>
	<plugins>
		<plugin>
			<groupId>io.spring.javaformat</groupId>
			<artifactId>spring-javaformat-maven-plugin</artifactId>
			<version>0.0.22</version>
			<executions>
				<execution>
					<phase>validate</phase>
					<inherited>true</inherited>
					<goals>
						<goal>validate</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

这种方式并不会根本性的格式化代码,比如不会改变import的顺序等,只会格式化代码中的缩进和空格

# 使用checkstyle强制格式化代码格式

<build>
	<plugins>
		<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>8.32</version>
				</dependency>
				<dependency>
					<groupId>io.spring.javaformat</groupId>
					<artifactId>spring-javaformat-checkstyle</artifactId>
					<version>0.0.22</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>
	</plugins>
</build>

# eclipse插件

The Eclipse plugin provides a custom formatter implementation and automatically applies project specific settings. The plugin is automatically activated whenever the Maven or Gradle plugins are discovered in a project build script.

If you need to customize the project specific settings that the plugin applies you should add a .eclipse folder in the root of your project. All .prefs files from this folder will be copied to the project .settings folders. Usually you’ll provide your own org.eclipse.jdt.core.prefs and org.eclipse.jdt.ui.prefs files.

You can also add a .eclipse/eclipse.properties file to customize the following items:

copyright-year= # The copyright year to use in new files

To install the plugin use the io.spring.javaformat.eclipse.site zip file. You can download the latest version from (repo.spring.io)[https://repo.spring.io/release/io/spring/javaformat/io.spring.javaformat.eclipse.site/0.0.22] or use the (update site)[https://dl.bintray.com/spring/javaformat-eclipse/].

# idea插件

安装spring-javaformat-intellij-plugin,要求gradle插件必须启用

此时直接格式化代码就是用的spring-javaformat

同样,只是格式化缩进而已

下载地址 repo.spring.io (opens new window)