sbt Scala build tool

Version 1.10 · source

Start here

sbt
Open the interactive shell
compile
Compile main sources
test
Run all tests
run
Run the main class
~test
Re-run on file change
projects
List subprojects
reload
Reload build.sbt
console
Scala REPL on classpath

Concepts

  1. build.sbt settings and tasks
  2. sbt shell keeps state warm
  3. target/ classes and reports

Run and build

sbt
Start the interactive shell
sbt <command>
Run one command and exit (batch mode)
Quote several commands: sbt "clean" "test"
compile
Compile main sources
run <args>
Run the main class in sbt's JVM
runMain <class> <args>
Run a specific main class
clean
Delete target directories
Test/compile
Compile test sources
package
Build the binary jar
doc
Generate Scaladoc
update
Resolve dependencies
exit
Leave the shell (or Ctrl+D)
help <command>
Show help for a command

Testing

test
Run all tests
testOnly <Suite>
Run named suites, * works
testOnly <Suite> -- -z "<text>"
Run tests matching a name
Everything after -- goes to the test framework; flags differ per framework
testQuick
Run new, failed or affected tests
Test/console
REPL with test classpath
IntegrationTest/test
Run the IntegrationTest config

Watch mode

~compile
Recompile on source change
~test
Re-run tests on change
~testQuick
Re-run only affected tests
~run
Restart the app on change
Enter
Stop a ~ watch and return to shell

Projects

projects
List all subprojects
project <id>
Switch the current project
<id>/test
Run a task in one subproject
<id>/compile
Compile one subproject
project /
Switch back to the root project
clean test
Run tasks one after another
; <a> ; <b>
Run b only if a succeeds

Build definition

reload
Reload build files after editing
reload plugins
Switch to the build-definition project
reload return
Return to the main build
set <key> := <value>
Change a setting until reload
session save
Write set changes to build.sbt
show <key>
Print a setting or task value
inspect <key>
Show scope, value and dependencies
inspect tree <key>
Show the dependency tree of a key
tasks
List available tasks
settings
List available settings
eval <expr>
Evaluate a Scala expression
consoleProject
REPL on the build definition

Cross building and REPL

console
Scala REPL with project classpath
consoleQuick
REPL without compiling the project
+test
Run for every crossScalaVersions entry
++<version> test
Switch Scala version for one command
+compile
Compile for all Scala versions
crossScalaVersions
Show the versions used by +

Publish and options

publishLocal
Publish to the local Ivy repository
publish
Publish to the publishTo repository
publishM2
Publish to the local Maven repo
dependencyTree
Print the dependency tree
Needs sbt 1.4+; in older builds add the plugin
sbt -J-Xmx2G
Pass a JVM option to sbt
sbt -Dsbt.offline=true
Do not fetch from repositories
sbt -Dsbt.version=<v>
Use another sbt version for a run
sbt -Dsbt.ci=true
No color or supershell for CI
sbt --client <command>
Send a command to a running server
sbt -Dsbt.task.timings=true
Print how long tasks take

build.sbt basics

Minimal build.sbt
ThisBuild / scalaVersion := "3.3.4"lazy val root = (project in file("."))  .settings(name := "app")
Add a dependency
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.19" % Test
Operators
Operator Meaning
:= Set a value
+= Append one element
++= Append a collection
% Group % artifact % version
%% Add the Scala suffix to the artifact
project/build.properties
sbt.version=1.10.10

Recipes

Fast TDD loop on one suite
sbt~testOnly com.example.UserSpec
Multi-module build, one module
sbt "api/clean" "api/test"
Cross-build and publish locally
sbt +test +publishLocal
CI run without color
sbt -Dsbt.ci=true -J-Xmx2G clean test