Spotless¶
With Gradle, Spotless formats Java with open-java-format as
soon as the build applies our plugin. With Maven it formats through its own palantirJavaFormat
step, whose output is not quite the same as on the other routes.
Gradle¶
Apply both plugins. Ours adds an open-java-format step to the java format of Spotless by itself,
so the spotless block needs no formatter step. If it still calls palantirJavaFormat(), remove
that line.
In an Android module the step formats the Java files of the Android
source sets. A target in the spotless block replaces them.
The step runs the formatter inside the Gradle JVM, so it needs the --add-exports flags. The native
formatter property from the Gradle plugin page does not help here: the Spotless step
always runs on the Gradle JVM.
org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
./gradlew spotlessApply # format every Java file
./gradlew spotlessCheck # fail when a file is not formatted
Without the flags spotlessJava fails and names them. The step works with Gradle's
configuration cache and build cache, and in parallel builds.
Up to 21.0.1: an error that stays after the fix
Without the flags the step fails with NoClassDefFoundError: Could not initialize class
com.palantir.javaformat.java.ImportOrderer. Spotless remembers that result, so after adding
the flags run ./gradlew spotlessCheck --rerun-tasks once.
From Spotless 8.12.0, its palantirJavaFormat() step can run open-java-format too, with
groupArtifact('dev.openjavaformat:open-java-format') as with Maven below, and with the
same differences in its output. The step of our plugin has none of them.
Maven¶
The palantirJavaFormat step of Spotless runs open-java-format when groupArtifact names it. The
option comes with Spotless Maven plugin 3.12.0
(diffplug/spotless#3179). The formatter has to be
21.0.2 or later, because the step asks for the PALANTIR style, and Maven has to run on Java 21 or
later. Unlike the Gradle step, it needs no --add-exports flags.
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>3.12.0</version>
<configuration>
<java>
<palantirJavaFormat>
<groupArtifact>dev.openjavaformat:open-java-format</groupArtifact>
<version>21.1.1</version>
</palantirJavaFormat>
</java>
</configuration>
</plugin>
Long strings stay on one line
The step runs the formatter through Spotless's own code for palantir-java-format, and that code
never calls StringWrapper, which splits a string literal that runs past the column limit into
pieces joined with +. IntelliJ IDEA, the native binary, the command line and our build plugins
split it. spotless:check accepts a file they formatted, but the checks of the other routes, the
GitHub Action among them, fail on a file that Spotless formatted. Where the
two have to match, use our Maven plugin instead.
A step of its own¶
The pull request diffplug/spotless#3084 adds an
openJavaFormat step to both Spotless plugins, for Gradle and for Maven, which formats the way every
other route does.
Ned Twigg, who maintains Spotless, closed it on 16 September 2026. In his comment he called the project "not differentiated enough from other similar projects" and added that he considers formatters less important in the age of LLMs. He also left the door open: the pull request can be reopened if the project becomes clearly different from the formatters Spotless already supports.
Vote for the pull request
If the pull request collects 100 👍, we will try to reopen it. Add yours to its description on GitHub.