Start programming and experimenting with a functional programming language like Scala might not seem simple at first sight. So, to get your feet wet it’s useful to have the very essential in a single place. I started writing this post in the form of a gist, but then I realized that maybe it was better to share it with the community.

sbt (aka Simple Build Tool) is a general purpose build tool written in Scala for JVM developers. It took advantage of previous ideas developed by other similar tools like Maven, Gradle and Ant.

  1. Default project layouts
  2. Built-in tasks
  3. Plugin architecture
  4. Declarative Dependency management
  5. Code over Configuration: A DSL for build tool
  6. Interactive
  7. Scala REPL integration

In order to have a better understanding of the main tool you’ll use programming with Scala you must read:

Project scaffolding (doing it in the right way)

The best way to organize your codebase, using sbt, is to follow the same folder structure used by Maven:

       <files to include in main jar here>
       <main Scala sources>
       <main Java sources>
       <files to include in test jar here>
       <test Scala sources>
       <test Java sources>

Create a new project using sbt

At first, you’ve to create the new project folder (e.g. myproject). Then, put inside the new folder a file named build.sbt, which contains the build settings that sbt will use. to house the build script (how build.sbt defines settings). This file, at the very beginning, will look like the following:

name := "myproject"
version := "0.1.0"
scalaVersion := "2.12.2"

where := is a function defined in the sbt library. It is used to define a setting that overwrites any previous value without referring to other settings. In order to define the version of Scala that sbt will pick up it’s possible to define a specific scalaVersion, but before doing so it’s better to verify your environment configuration and the current version of your Scala compiler (scala -version).

If the Scala version is not specified, the version sbt was built against is used. It is recommended to explicitly specify the version of Scala.
Please note that because compile is a dependency of run, you don’t have to run compile before each run; just type sbt run.

@see sbt documentation

sbt.version specifies the target sbt version that our build will use. If the required version is not available locally, the sbt launcher will download it for you. If this file is not present, the sbt launcher will choose an arbitrary version, which is discouraged because it makes your build non-portable.


sbt.version = 0.13.16

To retrieve the essential information about sbt (also see this SO answer) you’re running on you can use the following command:

sbt about

[warn] No sbt.version set in project/, base directory: /tmp
[info] Set current project to tmp (in build file:/tmp/)
[info] This is sbt 1.0.0
[info] The current project is {file:/tmp/}tmp 0.1-SNAPSHOT
[info] The current project is built against Scala 2.12.3
[info] Available Plugins: sbt.plugins.IvyPlugin, sbt.plugins.JvmPlugin, sbt.plugins.CorePlugin, sbt.plugins.JUnitXmlReportPlugin, sbt.plugins.Giter8TemplatePlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.12.3

What’s more, in the interactive mode sbt has a list of useful commands that guide you in the day to day work like help and tasks. One of the most useful things of sbt is the automatic and continous execution of a task fired by the changes saved to a source file you’re editing. To trigger this kind of behaviour a task must be prepended with ~. So, to execute all the tests that either failed during the previous execution or whose transitive dependencies changed the task is ~testQuick, to stop its execution just hit Enter.

Add ScalaTest

Starting from sbt 0.10 it’s possible to add the ScalaTest framework to a sbt project adding the following line to the build.sbt:

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

Start simple using the hello-world template

To start in the fastest way possible using sbt there is a hello-world template (the full tutorial) that can be imported just using sbt itself:

$ sbt new scala/hello-world.g8

It’s worth to point out that the default build.sbt template file contained inside the hello-world template is a very good source of information if you want to know how to configure your new Scala project in a matter of minutes.

@see Getting started with Scala and sbt in the CLI


IDE and its configuration