3. SCALA BUILD TOOL (SBT)

Pankaj Patil
5 min readMay 26, 2022

1. Why sbt?

You can build Scala projects using several build tools such as Maven, Ant, and Gradle, but sbt is the most common build tool in the community. It is a complex but powerful tool that manages your dependencies, and it defines the build cycle of your code. sbt has its syntax and predefined instructions. You can also load plugins, which are programs written using the sbt syntax, to add new commands or modify the build process’s existing behaviors.

Alternatives to sbt
The Scala community has a particular love-hate relationship with sbt: while some people think it is a great tool to work with, others dislike it for its complexity. Although extremely powerful, sbt can become complicated to understand and maintain.

sbt is undoubtedly the most popular choice for Scala projects but not the only one. For example, you could use any build tool with JVM support, such as Maven or Gradle.

Scala also has another community-driven build tool, called Mill. It takes most of its design choices from another build tool called Bazel, and it aims at a syntax that is simple to use and a fast and predictable build. For more information, look at https://github.com/lihaoyi/mill.

2. sbt installation

Before installing sbt, make sure you have Java 8+ JDK installed on your machine. Execute the command java –version to check which Java version your machine is running. You should see a message similar to the following

If missing, look at the previous lesson instructions on installing or upgrading your Java JDK.
Depending on your operating system, you’ll need to use a different package manager to install sbt.

LINUX
On Ubuntu and other Debian-based distributions, you can execute the following command in the terminal:
$ sudo apt-get install sbt

If you are using an RPM-based distribution, such as Red Hat Enterprise, you can use yum:
$ sudo yum install sbt

If you are on Gentoo, type the following command on the terminal:
$ emerge dev-java/sbt

MACOS
You can use Homebrew to install sbt by running the following command in your terminal:
$ brew install sbt

If you prefer, you can also use MacPorts:
$ port install sbt

WINDOWS
Download the MSI installer for sbt at www.scala-sbt.org/1.x/docs/ Installing-sbt-on-Windows. Open the file and follow the instruction on the UI to complete the installation.
After completing the installation, you should be able to open the terminal and execute the command sbt. When running sbt for the first time, it will download some dependencies; this could take a while and display lots of text in the console. It will happen only the first time, as it then saves them in its local cache; the next time you start sbt, it will be a lot faster. Eventually, sbt will be up and ready to receive commands.

Your terminal should look similar to the following:
$ sbt
[info] Loading project definition from /my/path
//
// Lots of output omitted here!
// …
[info] sbt server started at local://some/other/path
sbt>
Congratulations! sbt is now running and ready to accept commands.

3. sbt commands

sbt operates in the folder in which you typed the command. On startup, sbt looks for a specific folder structure to tentatively load your project and start its server. Once done, it will wait for commands to execute. The most common and useful ones are the following:
exit gracefully terminates your session.
about provides general information on the sbt version that you are running.
compile triggers the compilation of your code.
run compiles and runs your program on the JVM.
help lists each sbt command followed by a brief description of their usage.
clean deletes any file produced during the compilation.
reload re-evaluates the configurations provided in the project.
new creates an sbt project using a Giter8 template
console starts the REPL within a sbt project; all your code and dependencies are now accessible from the REPL for you to experiment with them.

Now that you have installed sbt, you are ready to build your first sbt project.

4. Your first sbt project

sbt has dedicated support to Giter8 templates; you can use them to create a skeleton for your Scala project.

What is Giter8?
Giter8 is a project by Foundweekends (see http://www.foundweekends.org) that allows you to host templates on GitHub and apply them to generate skel- eton projects: sbt is one of the supported ones. It is becoming more and more popular in the community as one of the quickest ways to get started with many popular Scala libraries.
Visit https://github.com/foundweekends/giter8/wiki/giter8-templates for a list of some of the templates available. Spark, Spark Job Server, Akka, Akka- HTTP, Play, Lagoom, Scala Native, and http4s are just a few projects that have published an official Giter8 template. See http://www.foundweekends.org/giter8 for more information.

Your first application prints “Hello, World!” in the console. Let’s create it by applying a Giter8 template called scala/hello-world.g8, using the following sbt command:

$ sbt
sbt> new scala/hello-world.g8
A template to demonstrate a minimal Scala applicationname [Hello World template]: //press enter to use the default nameTemplate applied in ./hello-world-template
[info] shutting down server

This command has created a new directory, called hello-world-template, containing the code generated by the template. You can access, compile, and run the code as follows:

$ cd hello-world-template
$ sbt
sbt:hello-world> compile
// ...
// output omitted here!
// …
[info] Compilation completed in 13.992s.
[success] Total time: 16 s, completed 8 Jan 2021, 18:27:20
sbt:hello-world> run
[info] running Main
Hello, World!
[success] Total time: 1 s, completed 8 Jan 2021, 18:34:20
sbt:hello-world>

Congratulations on compiling and running your first Scala program using sbt.

How to change your Scala version using sbt
sbt allows you to change the Scala version of your project on the fly. Imagine you want to change your Scala version from 2.13.6 to 3.0.0; you can achieve this using the following command:

sbt> ++3.0.0!

You can also use sbt to access the Scala REPL of a specific Scala version by combining its ++..! command with console. For example, if you want to start the REPL for scala 3.0.0, you can execute sbt ++3.0.0! console.

--

--

Pankaj Patil

Software Engineer | Blogger | Reader | Writer| Political and Social Activist