days
-4
-3
hours
-1
-5
minutes
0
-2
seconds
-1
-3
search
Is it Spring yet?

Maintain that “Spring” style of your Java project with the Spring Java Format plugins set

Eirini-Eleni Papadopoulou
spring java
© Shutterstock / suraya puntu

What if I told you there’s a tool that can make that “Spring” style in your Java project more consistent? That’s right! Spring created a set of plugins that can be applied to any Java project and promises to provide a consistent “Spring” style. Let’s take a closer look.

Spring brings you a handy tool to make sure that your Java projects have that consistent “Spring” style! Meet the Spring Java Format plugins set!

The tool was released in May this year and the current version is 0.0.6.

The plugins set to this point consists of:

  • A source formatter that applies wrapping and whitespace conventions
  • A checkstyle plugin that enforces consistency across a codebase

The aim of this project is to provide consistency and, therefore, each plugin is not generally configurable. That means you need to change your code to match the required conventions, you can’t configure the plugin conventions to match your style.

SEE ALSO: Spring Boot 2.1 brings Java 11 support and a bunch of new features and improvements

According to its GitHub repo, most of the coding conventions and style comes from the Spring Framework and Spring Boot projects. Spring Framework manually formats code, whereas Spring Boot uses automatic formatting.

One thing that you should keep in mind is that formatting and Checkstyle alone are not enough to produce truly consistent code. You can find some tips that could be useful when developing Spring Boot here.

Getting started

If you are looking forward to giving it a go, let’s take a look at how you can get started.

Maven

For source formatting, add the spring-javaformat-maven-plugin to your build plugins:

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

And the io.spring.javaformat plugin group in ~/.m2/settings.xml as follows:

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

You can now run ./mvnw spring-javaformat:apply to reformat code.

Gradle

For source formatting, add the spring-javaformat-gradle-plugin to your build plugins:

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

apply plugin: 'io.spring.javaformat'

The plugin adds format and checkFormat tasks to your project. The checkFormat task is automatically applied when running the standard Gradle check task.

Eclipse

With Eclipse, 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.

In order to install the plugin, you can use the io.spring.javaformat.eclipse.site zip file. You can download the latest version from repo.spring.io or use the update site.

IntelliJ IDEA

Much like in the Eclipse case, here the plugin is automatically activated as well whenever the Maven or Gradle plugins are discovered in a project build script. A Spring Java Format icon (formatOn) will also be displayed in the status bar to indicate that the formatter is active. You can use the standard code → reformat code action to format the code.

To install the plugin, you can use the spring-javaformat-intellij-plugin jar file. You’ll find the latest version here.

Author
Eirini-Eleni Papadopoulou
Eirini-Eleni Papadopoulou is an assistant editor for JAXenter.com. Just finished her masters in Modern East Asian Studies and plans to continue with her old hobby that is computer science.

Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of