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

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.

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:

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 () 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.