An sbt plugin for formatting Java code. This plugin began as a combination of ideas from this blog post and this maven plugin, though it has evolved since.
google-java-format relies on internal jdk.compiler APIs. On Java 17 and newer, access to those APIs is strongly encapsulated by the module system.
To keep the formatter commands working without requiring manual JVM flags, the plugin runs google-java-format in a forked JVM with the required module access flags.
Add the plugin to project/plugins.sbt:
addSbtPlugin("com.github.sbt" % "sbt-java-formatter" % --latest version---)For available versions see releases.
javafmtformats Java filesjavafmtAllformats Java files for all configurations (CompileandTestby default)javafmtCheckfails if files need reformattingjavafmtCheckAllfails if files need reformatting in any configuration (CompileandTestby default)javafmtFixImportsfixes Java imports only, without applying full formattingjavafmtFixImportsAllfixes Java imports only for all configurations (CompileandTestby default)javafmtFixImportsCheckfails if Java imports need fixingjavafmtFixImportsCheckAllfails if Java imports need fixing in any configuration (CompileandTestby default)
- The
javafmtOnCompilesetting controls whether the formatter kicks in on compile (falseby default). - The
javafmtStylesetting defines the formatting style: Google Java Style (by default) or AOSP style. - The
javafmtSortImportssetting controls whether imports are sorted (trueby default). - The
javafmtRemoveUnusedImportssetting controls whether unused imports are removed (trueby default). - The
javafmtReflowLongStringssetting controls whether long string literals are reflowed (trueby default). - The
javafmtFormatJavadocsetting controls whether Javadoc comments are reformatted (trueby default). - The
javafmtFormatterCompatibleJavaVersionsetting selects whichgoogle-java-formatruntime line to use (21by default). - The
javafmtJavaMaxHeapsetting controls the maximum heap passed to the forkedgoogle-java-formatJVM (Some("256m")by default).
This plugin requires sbt 1.3.0+.
The sbt plugin is enabled by default for the Test and Compile configurations. Use JavaFormatterPlugin.toBeScopedSettings to enable the plugin for the IntegrationTest scope and then use It/javafmt to format.
inConfig(IntegrationTest)(JavaFormatterPlugin.toBeScopedSettings)This plugin uses the Google Java Format library, which makes it quite opinionated and not particularly configurable.
The formatter runs in a forked JVM managed by the plugin.
By default it uses the same Java installation as the sbt process via java.home.
To make the plugin launch the formatter with a different Java installation, set either:
- the
sbt-javafmt.java.homeJVM system property - or the
SBT_JAVAFMT_JAVA_HOMEenvironment variable
If both are set, sbt-javafmt.java.home takes precedence.
The selected Java home must still be compatible with the google-java-format runtime line chosen by javafmtFormatterCompatibleJavaVersion.
This is useful if your build runs sbt on one JDK but needs to launch the formatter on another one. For example, you can keep sbt on Java 11 and still run the formatter on a newer JDK by setting SBT_JAVAFMT_JAVA_HOME or -Dsbt-javafmt.java.home=....
For example:
ThisBuild / javafmtFormatterCompatibleJavaVersion := 17SBT_JAVAFMT_JAVA_HOME=/path/to/jdk-17 sbt javafmtUse javafmtJavaMaxHeap to control the maximum heap size passed to that JVM:
ThisBuild / javafmtJavaMaxHeap := Some("512m")Set it to None to disable the explicit heap cap:
ThisBuild / javafmtJavaMaxHeap := NoneThe plugin also exposes a few google-java-format CLI options directly:
ThisBuild / javafmtFormatterCompatibleJavaVersion := 21
ThisBuild / javafmtSortImports := true
ThisBuild / javafmtRemoveUnusedImports := true
ThisBuild / javafmtReflowLongStrings := true
ThisBuild / javafmtFormatJavadoc := trueSet any of them to false to pass the corresponding --skip-... flag to google-java-format.
javafmtFormatterCompatibleJavaVersion maps to these formatter versions:
11->google-java-format 1.24.017->google-java-format 1.28.021->google-java-format 1.35.0(default)
If the selected formatter runtime is newer than the Java used to launch the formatter JVM, either:
- lower
ThisBuild / javafmtFormatterCompatibleJavaVersion - or point the formatter to a newer JDK via
SBT_JAVAFMT_JAVA_HOMEor-Dsbt-javafmt.java.home=...
javafmtOptions is still available for compatibility, but the preferred sbt-facing configuration is through the dedicated javafmt... settings above.
JavaFormatterOptions.reorderModifiers() currently has no effect in this plugin.
The plugin now runs google-java-format via its CLI in a forked JVM, and the released google-java-format CLI used here does not yet support a corresponding --skip-reordering-modifiers flag.
If you want to tweak the format, take a minute to consider whether it is really worth it, and have a look at the motivations in the Google Java Style Guide. If you decide you really need more flexibility, you could consider other plugins such as the sbt-checkstyle-plugin
Yes, we'll happily accept PRs to improve the plugin.
Take a look at the contributors graph if you want to contact any of the contributors directly.
Apache v2