Useful sbt plugins
sbt is a build tool for Scala. This post lists sbt plugins I have found useful in many projects.
sbt-bloop
A quick feedback loop is essential to productivity. Get quick build feedback directly to your IDE with Bloop. To install:
addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.4.1")
Finally, import your Scala project as a BSP project instead of an sbt project in IntelliJ IDEA.
Bloop is fast and accurate, and removes the need to have a separate terminal window open for compilation purposes.
sbt-scalafmt
Use Scalafmt to format your codebase consistently. To install:
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.0")
I recommend using the reformat on file save option in IntelliJ IDEA as described in Scalafmt's installation instructions.
You can also format the entire codebase in one command with:
sbt scalafmtAll
sbt-buildinfo
You often want to use build metadata in your application. For example, your app may display the current version number, or the git hash from which the app was built. Use sbt-buildinfo for that.
Installation:
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.9.0")
The plugin generates a Scala source file with an object that will be compiled with the rest of your code. You then access the BuildInfo object to read the parameters that you wish to display in your app. Follow the instructions in https://github.com/sbt/sbt-buildinfo for details.
sbt-mdoc
Use sbt-mdoc to typecheck your Scala code samples embedded in Markdown documentation. The plugin can also inject build parameters to the documentation. This is useful when you want your documentation to always refer to the latest version number of your software, for example in your Markdown-based installation instructions like README.md.
Installation:
addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.1.5")
Follow the instructions in mdoc's installation instructions to get started.
sbt-updates
It's professional to keep dependencies in your program up-to-date. Let sbt-updates check for updates to dependencies.
Installation:
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.5.3")
Then run sbt command dependencyUpdates
to list any new versions:
sbt:app> dependencyUpdates
You may also add sbt-updates as a global sbt plugin by creating a file ~/.sbt/1.0/plugins/sbt-updates.sbt
that contains
the plugin installation line. This makes command dependencyUpdates
available to all your sbt projects.
Protip: To check for updates to sbt plugins, open the sbt shell and run reload plugins
followed by dependencyUpdates
:
cmd> sbt
sbt:app> reload plugins
sbt:project> dependencyUpdates
sbt:project> reload return
Return to the build of your app with reload return
.
Build your own plugin
If you find yourself repeating the same sbt configurations over multiple projects, consider refactoring your settings into an sbt plugin.
To get started, add an object to project/MyPlugin.scala
that extends sbt.AutoPlugin
:
object MyPlugin extends AutoPlugin {
object autoImport {
val myGreeting = settingKey[String]("My greeting")
}
override def projectSettings: Seq[Setting[_]] = Seq(
myGreeting := "Hello, World!"
)
}
Now when you enable MyPlugin for your module in build.sbt, you can use the plugin's settings in your build:
val project = project.in(file("."))
.enablePlugins(MyPlugin)
.settings(myGreeting := "Hello, You!")
Enjoy!