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
- Before installing sbt make sure you have the Java version 8 (also known as 1.8) or higher in your system.
javac -versionin 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
- Install SBT
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.
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 ?
Running below command in your project directory with no command line arguments starts sbt shell in interactive mode.
For example, we could type
compile at the sbt shell:
compile again, press up arrow and then enter.
Now to run your program, type
To leave the shell, type
exit or use Ctrl+D (Unix) or Ctrl+Z (Windows).
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 (
compile and then run).
Frequently used Commands
Below are some of the most commonly used commands with sbt:
deletes all the generated files in the target/ directory.
compiles the main sources in /main/scala/ and /main/java/ directories.
runs the main class for the project.
compiles and runs all the tests for the project.
reloads the project’s build definition
generates the jar file for the project
starts an interactive SBT shell.
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.
libraryDependencies += groupID % artifactID % revision
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:
libraryDependencies ++= Seq( groupID % artifactID % revision, groupID % otherID % otherRevision )
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" )
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