Wednesday, March 12, 2025

How to Use Google Java Format with Spotless

 

Introduction

An abstract digital illustration of a futuristic robotic arm applying structured formatting to chaotic Java code, symbolizing the automation and efficiency of Google Java Format and Spotless.
Bringing Order to Chaos: Google Java Format and Spotless in Action—Automating Code Formatting for a Cleaner, More Efficient Codebase.


We've all been there. You push your code, feeling like a rockstar, only to be met with a wave of nitpicky code review comments: "Indentation is off," "Use spaces, not tabs," "Dude, who puts a curly brace on the same line?"

It's a never-ending battle—unless you have an enforcer. Enter Google Java Format, the ultimate formatting referee that settles these debates once and for all. But who has time to manually run a formatter on every file? That's where Spotless comes in, automating the entire process and ensuring every commit is clean, compliant, and free of formatting drama.

In this guide, we’ll walk you through setting up Google Java Format with Spotless in both Gradle and Maven projects. By the end, you'll wonder how you ever coded without it!

Why Use Google Java Format?

Imagine merging your feature branch and seeing a pull request filled with hundreds of unnecessary diffs—tabs switched to spaces, misplaced brackets, random newlines. It's like your code went on a formatting rollercoaster.

With Google Java Format, you:

  • Automate Code Formatting – Because life's too short for manual indentation fixes.

  • Ensure Consistency – No more "tabs vs. spaces" holy wars.

  • Simplify Integration – Works like a charm with Gradle and Maven.

  • Reduce Merge Conflicts – Fewer unnecessary changes mean happier developers.

So let’s set it up and finally move on to actual coding.

Setting Up Google Java Format with Spotless in Gradle

Step 1: Add the Spotless Plugin to build.gradle

First, install the Spotless plugin.

If using Kotlin DSL (build.gradle.kts):

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

If using Groovy DSL (build.gradle):

plugins {
    id 'com.diffplug.spotless' version '6.0.0'
}

Step 2: Configure Spotless to Use Google Java Format

Now, set Spotless to auto-format your Java files.

Kotlin DSL (build.gradle.kts)

spotless {
    java {
        target("src/**/*.java")
        googleJavaFormat()
    }
}

Groovy DSL (build.gradle)

spotless {
    java {
        target 'src/**/*.java'
        googleJavaFormat()
    }
}

Step 3: Apply Formatting

Run this to format all Java files:

gradle spotlessApply

Check for compliance before committing:

gradle spotlessCheck

If it fails, just run spotlessApply to fix everything automagically. 🚀

Setting Up Google Java Format with Spotless in Maven

Step 1: Add the Spotless Plugin to pom.xml

For Maven users, add this to your <build> section:

<build>
    <plugins>
        <plugin>
            <groupId>com.diffplug.spotless</groupId>
            <artifactId>spotless-maven-plugin</artifactId>
            <version>2.35.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>apply</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <formats>
                    <format>
                        <includes>
                            <include>src/**/*.java</include>
                        </includes>
                        <googleJavaFormat/>
                    </format>
                </formats>
            </configuration>
        </plugin>
    </plugins>
</build>

Step 2: Apply Formatting

Run:

mvn spotless:apply

To check compliance:

mvn spotless:check

Now your Java files are always in top shape!

Customizing Spotless

Need more control? Here are some tweaks.

Use a Specific Google Java Format Version

By default, Spotless grabs the latest version, but you can lock it down:

Gradle

spotless {
    java {
        googleJavaFormat("1.15.0")
    }
}

Maven

<configuration>
    <googleJavaFormat>
        <version>1.15.0</version>
    </googleJavaFormat>
</configuration>

Exclude Certain Files

Want to keep specific files untouched? No problem.

Gradle

spotless {
    java {
        target("src/**/*.java")
        googleJavaFormat()
        exclude("src/main/java/com/example/IgnoreThisFile.java")
    }
}

Maven

<excludes>
    <exclude>src/main/java/com/example/IgnoreThisFile.java</exclude>
</excludes>

Enforce Formatting in CI/CD

Make sure no rogue formatting sneaks into your codebase!

GitHub Actions Integration

Create a .github/workflows/spotless.yml file:

name: Spotless Check
on: [push, pull_request]
jobs:
  check-format:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-java@v3
        with:
          distribution: 'temurin'
          java-version: '17'
      - name: Set up Gradle
        uses: gradle/gradle-build-action@v2
      - name: Run Spotless Check
        run: ./gradlew spotlessCheck

For Maven:

- name: Run Spotless Check
  run: mvn spotless:check

Now every PR will be checked before it gets merged. No more formatting surprises! 🎉

Conclusion

Imagine a world where formatting discussions never waste another second of your time. That world is here. Google Java Format + Spotless means you spend more time writing great code and less time arguing over curly braces.

Now, it’s your turn! Have you tried Google Java Format with Spotless? What’s your biggest formatting headache? Drop a comment below—we’d love to hear your thoughts!

🚀 Looking for more Java productivity tips? Check out our other posts:

Happy coding, and may your commits always be spotless! 😎

No comments: