avatarKanan Rahimov

Summary

The provided web content outlines how to use Spotless with Maven to enforce code style and formatting rules for Java codebases, including configuration examples for Java, Markdown, and POM files.

Abstract

The article "Using Spotless with Maven" details the integration of the Spotless plugin into Maven builds to maintain consistent code styling and formatting for Java projects. It explains the installation and configuration process, emphasizing the plugin's utility in team environments for upholding code quality standards. The post is structured into two main parts: installing and configuring Spotless with Maven, and setting up Spotless for Java, Markdown, and POM files. It provides sample Maven plugin configurations, including specific rules and integrations with formatting tools like Palantir Java Format and Prettier. The article also covers the use of Spotless in a Spring Boot project and concludes by encouraging readers to adopt Spotless for its benefits in code quality assurance.

Opinions

  • The author believes that Spotless is particularly useful for enforcing a consistent style across a team and for ensuring code meets certain quality standards.
  • Spotless is praised for its ability to automatically check and fix code formatting issues, potentially saving time and effort for developers.
  • The article suggests that Spotless can be a powerful tool in maintaining high-quality code, with its flexibility in configurations and integrations with various formatting tools.
  • The author expresses that following the provided instructions will enable developers to seamlessly integrate Spotless into their Maven build process.
  • By offering additional resources and support through paid memberships, the author indicates a commitment to providing comprehensive assistance to developers looking to implement Spotless in their projects.

Using Spotless with Maven

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

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>
        <execution>
          <id>format</id>
          <phase>process-sources</phase>
          <goals>
            <goal>check</goal>
            <goal>apply</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Here, we add spotless as a build plugin and introduce execution instructions for when this plugin should be run.

In Maven, the execution block is used to specify the configuration for a particular execution of a plugin. It is defined within the executions element of a plugin and can contain a number of different elements that control the behavior of the plugin.

In the example you provided, the execution block contains an id element, a phase element, and a goals element. The id element is used to specify a unique identifier for the execution, which can be used to refer to it elsewhere in the Maven build configuration. The phase element specifies the phase of the Maven build process in which the plugin should be executed. In this case, the process-sources phase is specified, which means that the plugin will be executed as part of the process of compiling the source code of the project.

The goals element is used to specify the goals that should be executed as part of the plugin execution. In this case, the check and apply goals are specified, which will cause the Spotless plugin to check the source code for any style and formatting issues and fix them as necessary.

Overall, the execution block in this example is used to configure the execution of the Spotless plugin as part of the Maven build process, specifying that it should be run during the process-sources phase and execute the check and apply goals.

Now, we can run:

mvn clean install

This will build your project, run any tests, and install any dependencies, including the Spotless Maven Plugin. Spotless will then be run as part of the build process, checking your code for any style and formatting issues and fixing them as necessary.

Running Spotless Maven Commands

Once you have installed and configured Spotless, you can run it by executing the following command:

mvn spotless:check

This command will check the codebase for formatting and styling issues and report them.

We can use the following command to fix any issues automatically:

mvn spotless:apply

Spotless Configurations

In this section, I want to provide some examples of Spotless configurations. At the end of this part, you can find the final configuration for the Maven.

The official documentation provides a helpful list of ready-to-use configurations. You can find them here for Maven.

Java Configuration

<java>
    <toggleOffOn/>
    <importOrder/>
    <removeUnusedImports/>
    <palantirJavaFormat>
        <version>2.27.0</version>
    </palantirJavaFormat>
    <indent>
        <spaces>true</spaces>
        <spacesPerTab>4</spacesPerTab>
    </indent>

    <formatAnnotations/>
</java>

Configurations like palantirJavaFormat and prettier have their own sections. For example, you can configure prettier to use a separate configuration file like this:

<prettier>
    <version>2.4.1</version>
    <configFile>${project.basedir}/.prettierrc</configFile>
</prettier>

Make sure to check the documentation for more details.

Markdown Configuration

<markdown>
  <includes> <!-- You have to set the target manually -->
    <include>**/*.md</include>
  </includes>
  <flexmark/>
</markdown>

Configuration for POM files

<pom>
    <includes>
        <include>pom.xml</include>
    </includes>
    <sortPom>
        <encoding>UTF-8</encoding>
        <keepBlankLines>true</keepBlankLines>
        <nrOfIndentSpace>4</nrOfIndentSpace>
        <indentBlankLines>false</indentBlankLines>
        <indentSchemaLocation>true</indentSchemaLocation>
        <expandEmptyElements>false</expandEmptyElements>
        <sortProperties>true</sortProperties>
    </sortPom>
</pom>

Using the following links, you can find more information about pom and sortPom :

Full Configuration

<build>
    <plugins>
        <plugin>
            <groupId>com.diffplug.spotless</groupId>
            <artifactId>spotless-maven-plugin</artifactId>
            <configuration>
                <java>
                    <toggleOffOn/>
                    <importOrder/>
                    <removeUnusedImports/>
                    <palantirJavaFormat>
                        <version>2.27.0</version>
                    </palantirJavaFormat>
                    <indent>
                        <spaces>true</spaces>
                        <spacesPerTab>4</spacesPerTab>
                    </indent>

                    <formatAnnotations/>
                </java>
                <markdown>
                    <includes>
                        <include>**/*.md</include>
                    </includes>
                    <flexmark/>
                </markdown>
                <pom>
                    <includes>
                        <include>pom.xml</include>
                    </includes>
                    <sortPom>
                        <encoding>UTF-8</encoding>
                        <keepBlankLines>true</keepBlankLines>
                        <nrOfIndentSpace>4</nrOfIndentSpace>
                        <indentBlankLines>false</indentBlankLines>
                        <indentSchemaLocation>true</indentSchemaLocation>
                        <expandEmptyElements>false</expandEmptyElements>
                        <sortProperties>true</sortProperties>
                    </sortPom>
                </pom>
            </configuration>
            <executions>
                <execution>
                    <id>format</id>
                    <goals>
                        <goal>check</goal>
                        <goal>apply</goal>
                    </goals>
                    <phase>process-sources</phase>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Summary

Spotless is a powerful tool that can help ensure that your code is consistent, readable, and high-quality. By integrating it into your Maven build process, you can easily check your code for any style and formatting issues and fix them automatically, saving you time and effort.

There are many different configurations that you can use with Spotless, including built-in rules for enforcing the Google Java style guide, custom rules for enforcing specific style and formatting requirements, and integration with the Checkstyle tool. In a Spring Boot project, you can also use the Spring formatting rules provided by Spotless.

Thanks for checking this post! 👋🏼

📣 For updates, questions, or suggestions, consider following me on 🐦 Twitter.

You can also find me on all other platforms: GitHub, LinkedIn, and YouTube.

I publish additional materials (full source code, video explanation) and support (like ask me anything) via paid memberships at CoderVlogger.com.

Please consider joining and supporting by becoming a member.

Enjoy coding! Kanan Rahimov

Maven
Java
Spring Boot
Code Style
Spotless
Recommended from ReadMedium