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

Share
  • October 30, 2018

spring javaSpring 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:


	
		
			io.spring.javaformat
			spring-javaformat-maven-plugin
			0.0.6
		
	

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


	io.spring.javaformat

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.

The post Maintain that “Spring” style of your Java project with the Spring Java Format plugins set appeared first on JAXenter.

Source : JAXenter