Introduction to SBT
• stackchief •
SBT (Simple Build Tool) is a popular build tool used with Scala and Java projects. In this article, we introduce SBT including what it is, how it works, and basic commands for using SBT with your project.
What is SBT?
SBT is an open source build tool for Scala and Java. SBT is written in Scala and is recognized as the de facto build tool for Scala projects. SBT is similar to other build tools like Ant and Maven but differs mainly in it's flexibility with project compilations. Using SBT, you can access the same dependencies that you can with Maven with the added advantage of incremental compilation, interactive shells, etc.
While SBT can be used with Scala and non-Scala projects alike, it is most commonly used with Scala projects because of it's seamless integration with Scala technologies.
How SBT works?
SBT follows the same basic directory structure as Maven:
The source code for an SBT project lives in the src/main/scala and src/main/java directories. Non-Java resources such as XML config files and other artifacts are included in the src/main/resources folder so they can be loaded as class path resources.
Like other traditional Java projects, all test files / resources live in a separate test/ directory.
The project/ directory houses helper objects and one-off plugins. It also defines a build.properties file specifying the SBT version to be used, making the project more portable. If the version specified is not available locally, the SBT launcher will download the correct version for you.
At the root of an SBT project is the build.sbt file. This encapsulates the project's build definition and specifies things like project name, version, scala version, etc. The build.sbt file also includes managed dependencies and other custom defined tasks and settings for the project.
SBT Basic Commands
Below are some of the more common commands used with SBT:
starts an interactive SBT shell
runs the main class for the project
reloads the project's build definition
compiles the main sources in /main/scala/ and /main/java/ directories
deletes all the generated files in the target/ directory
generates the jar file for the project
compiles and runs all the tests for the project
SBT is a flexible build tool for Scala and Java projects. You can use SBT in the same way you use Maven or Ant but with the added benefits of incremental compilation and custom configurations through code.