Quick start tutorial to Simple Build Tool (SBT)

This is a quick start tutorial to get going with SBT. It is meant for myself, but you are welcome to use it too 🙂

For the detailed documentation please checkout the documentation on the SBT website.

Introduction to SBT

Simple Build Tool (SBT) is a flexible build system written in Scala.SBT build definitions are written in Scala in the form of a Domain Specific Language (DSL), having the benefit of compile-time checking. In addition to its dependency management ability based on Ivy and supporting Maven-format repositories, SBT offers both incremental compilation and an interactive shell (that is, the REPL we were using earlier). It also supports continuous testing and deployment, and integrates with many Scala test frameworks, making it the de facto build tool for the Scala community.

Prerequisites

You should have SBT installed. If you do not please checkout the SBT website.

I will import the project into Eclipse using the Eclipse SBT plugin. There are also plugins available for IntelliJ IDEA and NetBeans, but they are out of scope for this tutorial.

Okay now the formalities are out of the way lets get started with the quick tutorial…

Quick start

Setup the project:

mkdir -p ~/projects/sbt/SampleProject
cd ~/projects/sbt/SampleProject

Okay lets create a sample project using SBT:

Note that lines prefixed with ‘>’ are commands that must be entered in the SBT editor in interactive mode.

sbt
> set name := "SampleProject"
> session save
> exit

This will create a build.sbt file under the project root. This file gathers information about the project, it is the equivalent of the Maven’s .pom file. The difference being that build.sbt compiles to Scala rather than being XML. In addition to this file two directories project and target are created.

You can open and view the build.sbt file. Note that the extra line between each statement is important. The .sbt files are not Scala programs; they are a list of Scala expressions, where a blank line is the delimiter between these expressions.

Okay, lets import the project into Eclipse. The sbteclipse plugin is available to adapt a pure SBT project to an Eclipse project. You just need to create a plugins.sbt file under the project directory and add the following line to import the sbteclipse plugin:

Note that you should double check the version number to make sure you are using the latest version of the sbteclipse plugin.

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.5.0")

Or for convenience from the root directory:

echo 'addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.5.0")' > project/plugins.sbt

The preceding given string is a way in SBT to express a dependency to a Maven library; it is the equivalent to what you would normally write into a pom file:

<groupId>com.typesafe.sbteclipse</groupId>
<artifactId>sbteclipse-plugin</artifactId>
<version>2.5.0</version>

Lets create the Eclipse project. From the root directory execute the following command:

sbt eclipse

Now just fire-up your Eclipse IDE if you haven’t already done so.

Import as a new project

  • Select File | Import….
  • Navigate to General | Existing Projects into Workspace.
  • Browse to the root directory of your project and click on OK.
  • Then, click on Finish to complete the import of the project, which will appear in the Project Explorer window.

Refresh existing project

  • Just select the project and hit F5.

That’s all 🙂

Leave a comment