moznion's tech blog

moznion's technical memo

Released sbt-spotless: an sbt plugin for Spotless

github.com

I've released sbt-spotless, this is an sbt plugin for Spotless.

Spotless is currently supporting plugins for maven and gradle, but it seems there had been no sbt's one, so I made this. This plugin works on sbt 1.3.x and legacy sbt 0.13.x *1.

The usage is quite simple. This plugin has already been published the Maven Central, so you just only put a dependency definition into your plugins.sbt:

addSbtPlugin("net.moznion.sbt" % "sbt-spotless" % "0.1.3")

next, configure your build.sbt for code formatter(s):

import net.moznion.sbt.spotless.config._

lazy val root = (project in file("."))
  .settings(
    name := "Example",
    spotlessJava := JavaConfig(
      googleJavaFormat = GoogleJavaFormatConfig("1.7")
    ),
  )

finally, you can run sbt spotlessCheck and sbt spotlessApply commands to check and apply the code formatter.

And – this feature is only available on sbt 1.3.x – you can use checkOnCompile and applyOnCompile options like the following:

lazy val root = (project in file("."))
  .settings(
    name := "Example",
    spotless := SpotlessConfig(
      checkOnCompile = true,
      // applyOnCompile = true,
    ),
  )

then, it checks or applies the code formatter automatically on compiling, automatically.

If you'd like to know configuration in detail, please refer to the following documentation: Configurations · moznion/sbt-spotless Wiki · GitHub

This plugin supports Java, Scala, Kotlin, Groovy, C++, and SQL formatters so far, but Spotless core supports various formatters furthermore. Please feel free to ask me through an issue or pull-request if you feel this should support another formatter.

Enjoy!

*1:but on sbt 0.13.x, the features are limited