Let’s Dive into the World of SBT (Simple Build Tool)

Reading Time: 4 minutes

In this blog, we are going to discuss about SBT which is basically an open source built tool for Scala and Java projects. Also, we will discuss about its directory structure, installation and setup, how to run it, frequently used sbt commands, how to add library dependencies in your project & finally the conclusion.

Now before diving into sbt, first we need to understand what actually is a build tool ?

What is a build tool ?

A build tool in software is a way of automating compilation, dependency management, packaging and running various automation test cases. There are many build tool available in the market. Some of the popular Java build tools are Maven and Gradle.

Why do we need build tools ?

When we work on a software we need to compile, package and run test cases every now and then. We cannot do this manually everyday. So build tools are a way of automating all such processes.
Any developer can set up a build tool for the project and thus saving time and effort for everyone else. So these tools are portable and can reproduce your builds for everyone.

What is SBT ?

SBT is a build tool written in Scala that helps one to manage their Scala project, which includes building, compiling, testing, as well as managing libraries and dependencies. It is also equipped with its own plugins which allow for integration of other features. If you are new to Scala, I would suggest starting with it from an IDE such as IntelliJ IDEA, since the command line workflow, and project structure might seem overwhelming to beginners.

It is similar to other build tools like Ant and Maven but differs mainly in it’s flexibility with project compilations. Using it, you can access the same dependencies that you can with Maven with the added advantage of incremental compilation, interactive shells, etc.

While it 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.

Installation and Setup

  1. Before installing sbt make sure you have the Java version 8 (also known as 1.8) or higher in your system.
    • Run javac -version in the command line to check the current java version in your system.
    • If you don’t have version 1.8 or higher, install from here
  2. Install SBT
    • For Windows, click here
    • For Linux, click here
    • Fore Mac, click here

Now Let’s Understand the Directory Structure associated with every SBT Project

A typical sbt project has the following directory structure:

The source code for an SBT project resides 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 directory so they can be loaded as class path resources.

Likewise other traditional Java projects, all test files / resources reside in a separate test/ directory.

build.sbt file

At the root of your project there is a file named build.sbt and this encapsulates the project’s build definition and specifies things like project name, version, scala version, etc and also includes managed dependencies and other custom defined tasks and settings for the project.

How to run sbt ?

Interactive mode

Running below command in your project directory with no command line arguments starts sbt shell in interactive mode.

$ sbt

For example, we could type compile at the sbt shell:

> compile

To compile again, press up arrow and then enter.

Now to run your program, type run.

To leave the shell, type exit or use Ctrl+D (Unix) or Ctrl+Z (Windows).

Batch mode

We can also run sbt in batch mode by specifying a space separated list of SBT commands as arguments. For sbt commands that take arguments, pass the command and arguments as one argument to sbt by enclosing them in quotes. For example,

$ sbt clean compile run

In this example,  the commands will be run in sequence (cleancompile and then run).

Frequently used Commands

Below are some of the most commonly used commands with sbt:

clean

deletes all the generated files in the target/ directory.

compile

compiles the main sources in /main/scala/ and /main/java/ directories.

run

runs the main class for the project.

test

compiles and runs all the tests for the project.

reload

reloads the project’s build definition

package

generates the jar file for the project

sbt

starts an interactive SBT shell.

console

Starts the Scala interpreter with a classpath including the compiled sources and all dependencies. To return or exit, type  Ctrl+D .

Adding Library Dependencies

We  can specify library dependencies in our build.sbt file to fetch libraries from Maven Repository or other repositories.

Syntax:

libraryDependencies += groupID % artifactID % revision

For Example,

Here’s how we can add scalatest dependency to a project:

libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.2" % "test"

Similarly, we can also add a list of dependencies all at once:

Syntax:

libraryDependencies ++= Seq(
groupID % artifactID % revision,
groupID % otherID % otherRevision
)

For Example,

Here’s how we can add scalatest and mockito dependencies together.

libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.2.2" % "test",
"org.mockito" %% "mockito-scala" % "1.5.12" % "test"
)

Conclusion

SBT is a powerful tool to describe your build and manage your dependencies. You can use it in the same way you use Maven or Ant but with the added benefits of incremental compilation and custom configurations through code.

To learn Scala from scratch, click here

Written by 

Prateek Gupta is a Software Consultant at Knoldus Inc. He like to explore new technologies and believes in writing clean code. He has a good understanding of programming languages like C, Java, MySQL and Scala. He also has good time management skills. In his leisure time, he like to do singing and watch SciFi movies.