Standardize code formatting with Spotless

ankityadav33

Ankit Yadav

Posted on July 31, 2022

Standardize code formatting with Spotless

Organizing and formatting code as a standard for the whole team is important and useful. Without a standard being followed for formatting code within the project following problems:

  • Conflicts during updates from repository and during merge
  • Developers using their preferred formatting
  • Different auto format settings in IDEA showing more changes as when committing code

In order to setup a standard in regards to code formatting, we need to setup either a common configuration file to be imported by everyone or we can use something like Spotless which could be configured to use existing code formatting standards including Google Java Format and others.

Spotless is available for lots of languages like: Java, Groovy, Kotlin, Scala, C/C++, Python, Sql, Typescript 


Here I am going to explain the usage of Spotless which we are using in our applications.

The setup is really simple and comprises of only two steps no matter you use Maven or Gradle:

  1. Include the spotless dependency: "com.diffplug.spotless"
  2. Configure the plugin based on what your formatting preferences
  3. Make it part of build (optional but preferred)

Include the spotless dependency

Maven

<dependency>
    <groupId>com.diffplug.spotless</groupId>
    <artifactId>spotless-maven-plugin</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Gradle

dependencies {
    implementation("com.diffplug.spotless:spotless-lib:${spotlessLibVersion}")
    implementation("com.diffplug.spotless:spotless-plugin-gradle:${spotlessVersion}")
}
Enter fullscreen mode Exit fullscreen mode

Configure the plugin

Maven

<plugin>
    <groupId>com.diffplug.spotless</groupId>
    <artifactId>spotless-maven-plugin</artifactId>
    <configuration>
        <ratchetFrom>origin/develop</ratchetFrom>
        <java>
            <includes>
                <include>src/main/java/**/*.java</include>
                <include>src/test/java/**/*.java</include>
            </includes>
            <importOrder />
            <removeUnusedImports />
            <toggleOffOn/>
            <trimTrailingWhitespace/>
            <endWithNewline/>
            <indent>
                <tabs>true</tabs>
                <spacesPerTab>4</spacesPerTab>
            </indent>
            <palantirJavaFormat/>
        </java>
    </configuration>
</plugin>
Enter fullscreen mode Exit fullscreen mode

Gradle

spotless {
    java {
        target fileTree('.') {
            include '**/*.java'
            exclude '**/build/**', '**/build-*/**'
        }
        toggleOffOn()
        palantirJavaFormat()
        removeUnusedImports()
        trimTrailingWhitespace()
        endWithNewline()
    }
}
Enter fullscreen mode Exit fullscreen mode

Common configurations used in the above example for formatting

  • importOrder- All imports are ordered
  • removeUnusedImports - Unused imports are removed
  • toggleOffOn - toggle formatting in code by a comment
  • trimTrailingWhitespace - All trailing whitespaces in the code are removed
  • endWithNewline - The file ends with a new line

Execute spotless on code

Maven
mvn spotless:apply - To implement automated code formatting
mvn spotless:check - To validate if code was formatted

Gradle

.\gradlew clean build spotlessApply - To implement automated code formatting
.\gradlew clean build spotlessCheck - To validate if code was formatted​​​​​​​

ReferenceGithub/spotless

💖 💪 🙅 🚩
ankityadav33
Ankit Yadav

Posted on July 31, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related