Skip to the content.

Spotless

Information

Introduction

Spotless is a general-purpose code formatter that can enforce a consistent coding style across a variety of programming languages and build tools. It is designed to “keep your code spotless” by automatically formatting source files according to defined rules.

Spotless is particularly popular in the Java and Kotlin ecosystems, but it supports many other languages including Scala, Groovy, C++, Python, JavaScript, and more. It integrates seamlessly with build tools like Gradle and Maven, allowing for easy enforcement of styling rules in CI/CD pipelines.

What is it for?

Spotless is used to:

Main Functionalities and Features

Installation and Setup

Maven Setup

To use Spotless with Maven, add the spotless-maven-plugin to your pom.xml.


<plugin>
    <groupId>com.diffplug.spotless</groupId>
    <artifactId>spotless-maven-plugin</artifactId>
    <version>2.43.0</version>
    <configuration>
        <java>
            <googleJavaFormat>
                <version>1.17.0</version>
                <style>GOOGLE</style>
            </googleJavaFormat>
            <licenseHeader>
                <content>/* (C) $YEAR */</content>
            </licenseHeader>
        </java>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>check</goal>
            </goals>
            <phase>compile</phase>
        </execution>
    </executions>
</plugin>

Gradle Setup

To use Spotless with Gradle, apply the plugin and configure it in your build.gradle or build.gradle.kts.

plugins {
    id("com.diffplug.spotless") version "6.25.0"
}

spotless {
    java {
        googleJavaFormat("1.17.0")
        licenseHeader("/* (C) $YEAR */")
    }
}

Usage

Common Commands

For Maven:

For Gradle:

Tips and Tricks

See also