Using Spotless with Maven

Enforcing code style and formatting rules for a Java codebase with Maven and Spotless.

Kanan Rahimov
4 min readJan 2, 2023
Using Spotless for formatting Java code

Spotless is a general-purpose formatting plugin and can be used to enforce code style and formatting rules in your Java projects. It is particularly useful for enforcing a consistent style across a team and for ensuring that your code meets certain standards of quality.

In this post, we will look at how to use Spotless with Maven in a Spring Boot project. In fact, it can be applied to any Java project with Maven. The plugin itself also has support for Gradle.

This post consists of two parts:

  • Part 1: Install and configure Spotless with Maven;
  • Part 2: Configure Spotless for Java, Markdown, and POM files;

Installing Spotless

To run Spotless as part of your Maven build, you will need to add the Spotless Maven Plugin to your pom.xml file.

Adding Spotless as Maven Build Plugin

Here is the configuration we need to add as a Maven build plugin:

<build>
<plugins>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<executions>…

--

--