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.
Add the plugin to project/plugins.sbt:
// Default plugin:
addSbtPlugin("com.github.sbt" % "sbt-java-formatter" % --latest version---)
// Alternative for Java 17+: wraps formatter commands in a fresh sbt JVM,
// so you do not need to configure `--add-opens` manually, see below.
addSbtPlugin("com.github.sbt" % "sbt-java-formatter-add-opens" % --latest version---)For available versions see releases.
The following commands are available:
javafmtformats Java filesjavafmtAllformats Java files for all configurations (CompileandTestby default)javafmtCheckfails if files need reformattingjavafmtCheckAllfails if files need reformatting in any configuration (CompileandTestby default)
The sbt-java-formatter-add-opens plugin wraps the above commands and, on Java 17+, runs them in a fresh sbt JVM with the required jdk.compiler module access flags. From a user perspective, the commands stay the same and no manual JVM flags need to be configured.
- 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.
This plugin requires sbt 1.3.0+.
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.
If you depend on sbt-java-formatter-add-opens, the formatter commands (javafmt, javafmtAll, javafmtCheck, javafmtCheckAll) automatically relaunch in a JVM with the required module flags, instead of requiring manual -J--add-opens=... setup.
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.
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