Spotless: Keep your code spotless
Spotless can format <antlr | c | c# | c++ | css | flow | graphql | groovy | html | java | javascript | json | jsx | kotlin | less | license headers | markdown | objective-c | protobuf | python | scala | scss | shell | sql | typeScript | vue | yaml | anything> using <gradle | maven | sbt | anything>.
You probably want one of the links below:
❇️ Spotless for Gradle (with integrations for VS Code and IntelliJ)
user@machine repo % ./gradlew build
:spotlessJavaCheck FAILED
The following files had format violations:
src\main\java\com\diffplug\gradle\spotless\FormatExtension.java
-\t\t····if·(targets.length·==·0)·{
+\t\tif·(targets.length·==·0)·{
Run './gradlew spotlessApply' to fix these violations.
user@machine repo % ./gradlew spotlessApply
:spotlessApply
BUILD SUCCESSFUL
user@machine repo % ./gradlew build
BUILD SUCCESSFUL
❇️ Spotless for Maven
user@machine repo % mvn spotless:check
[ERROR] > The following files had format violations:
[ERROR] src\main\java\com\diffplug\gradle\spotless\FormatExtension.java
[ERROR] -\t\t····if·(targets.length·==·0)·{
[ERROR] +\t\tif·(targets.length·==·0)·{
[ERROR] Run 'mvn spotless:apply' to fix these violations.
user@machine repo % mvn spotless:apply
[INFO] BUILD SUCCESS
user@machine repo % mvn spotless:check
[INFO] BUILD SUCCESS
❇️ Spotless for SBT (external for now)
Other build systems
How it works (for potential contributors)
Ideally, a code formatter can do more than just find formatting errors - it should fix them as well. Such a formatter is just a Function<String, String>
, which returns a formatted version of its potentially unformatted input.
It's easy to build such a function, but there are some gotchas and lots of integration work (newlines, character encodings, idempotency, git ratcheting, and build-system integration). Spotless tackles those for you so you can focus on just a simple Function<String, String>
which can compose with any of the other formatters and build tools in Spotless' arsenal.
Current feature matrix
Feature / FormatterStep | gradle | maven | sbt | (Your build tool here) |
---|---|---|---|---|
Automatic idempotency safeguard | :+1: | :+1: | :+1: | :white_large_square: |
Misconfigured encoding safeguard | :+1: | :+1: | :+1: | :white_large_square: |
Toggle with spotless:off and spotless:on | :+1: | :+1: | :white_large_square: | :white_large_square: |
Ratchet from origin/main or other git ref | :+1: | :+1: | :white_large_square: | :white_large_square: |
Define line endings using git | :+1: | :+1: | :+1: | :white_large_square: |
Fast incremental format and up-to-date check | :+1: | :+1: | :white_large_square: | :white_large_square: |
Fast format on fresh checkout using buildcache | :+1: | :white_large_square: | :white_large_square: | :white_large_square: |
generic.EndWithNewlineStep | :+1: | :+1: | :white_large_square: | :white_large_square: |
generic.IndentStep | :+1: | :+1: | :white_large_square: | :white_large_square: |
generic.Jsr223Step | :white_large_square: | :+1: | :white_large_square: | :white_large_square: |
generic.LicenseHeaderStep | :+1: | :+1: | :+1: | :white_large_square: |
generic.NativeCmdStep | :+1: | :+1: | :white_large_square: | :white_large_square: |
generic.ReplaceRegexStep | :+1: | :+1: | :white_large_square: | :white_large_square: |
generic.ReplaceStep | :+1: | :+1: | :white_large_square: | :white_large_square: |
generic.TrimTrailingWhitespaceStep | :+1: | :+1: | :white_large_square: | :white_large_square: |
antlr4.Antlr4FormatterStep | :+1: | :+1: | :white_large_square: | :white_large_square: |
biome.BiomeStep | :+1: | :+1: | :white_large_square: | :white_large_square: |
cpp.ClangFormatStep | :+1: | :white_large_square: | :white_large_square: | :white_large_square: |
cpp.EclipseFormatterStep | :+1: | :+1: | :+1: | :white_large_square: |