
Brief introduction to Scala and SBT
Let us look at Scala and SBT briefly. This book assumes that the reader is familiar with Scala and therefore will not go into the depth of it.
What is Scala?
Scala is a general purpose programming language. Scala is an acronym for Scalable Language. This reflects the vision of its creators of making Scala a language that grows with the programmer's experience of it. The fact that Scala and Java objects can be freely mixed makes transition from Java to Scala quite easy.
Scala is also a full-blown functional language. Unlike in other languages, such as Haskell, which is a pure functional language, Scala allows interoperability with Java and support for object-oriented programming. Scala also allows the use of both pure and impure functions. Impure functions have side effects such as mutation, I/O, and exceptions. The purist approach to Scala programming encourages the use of pure functions only.
Scala is a type-safe JVM language that incorporates both object-oriented and functional programming in an extremely concise, logical, and extraordinarily powerful language.
Why Scala?
Here are some advantages of using Scala:
- A functional solution to a problem is always better: This is my personal view and is open to contention. The elimination of mutation from application code allows the application to be run in parallel across hosts and cores without any deadlocks.
- Better concurrency model: Scala has an Actor model that is better than Java's model of locks on a thread.
- Concise code: Scala code is more concise than its more verbose cousin, Java.
- Type safety/static typing: Scala does type checking at compile time.
- Pattern matching: The case statements in Scala are super powerful.
- Inheritance: The mixin traits are great, and they definitely reduce code repetition.
- Domain-specific language (DSL): Scala syntax allows for a programmer to write a natural looking DSL. This ability was carefully built into the original language design. This is a very powerful feature of Scala. Scala test/specs build on top of this feature.
Scala Build Tool
Scala Build Tool (SBT) is a build tool that allows the compiling, running, testing, packaging, and deployment of your code. SBT is mostly used with Scala projects, but it can as easily be used for projects in other languages. In this book, we will be using SBT as a build tool for managing our project and running our tests.
SBT is written in Scala and can use many of the features of the Scala language. Build definitions for SBT are also written in Scala. These definitions are both flexible and powerful. SBT also allows the use of plugins and dependency management. If you have used a build tool such as Maven or Gradle in any of your previous incarnations, you will find SBT a breeze.
Why SBT?
The following are the reasons we choose SBT:
- Better dependency management:
- Ivy-based dependency management
- Only-update-on-request model
- Can launch REPL in project context
- Continuous command execution
- Scala language support for creating tasks
Resources for learning Scala
Here are a few of the resources for learning Scala:
- http://www.scala-lang.org/
- https://www.coursera.org/course/progfun
- https://www.manning.com/books/functional-programming-in-scala
- http://www.tutorialspoint.com/scala/index.htm
Resources for SBT
Here are a few of the resources for learning SBT:
Setting up a build environment
We have done enough jibber-jabber to lay the groundwork for getting our hands on the driving plates now. Let's start with setting up the environment so we can write, compile, and run a test-driven application using Scala and SBT.
Steps for downloading and installing Scala
- Scala can be downloaded from http://www.scala-lang.org/download. Download the latest version available.
Note
Scala can also be installed using Homebrew for Mac by typing
brew install scala
, provided Homebrew is installed. Visit http://brew.sh/ for more information. - After downloading the binaries, unpack the archive. Unpack the archive in a convenient location, such as
/usr/local/share/scala
on Unix orC:\Program Files\Scala
on Windows. You are, however, free to choose a location. - Add the following environment variables:
- Test the Scala interpreter (aka the REPL) by typing
scala
in the Terminal window. - Test the Scala compiler by typing
scalac
in the Terminal window.
Tip
You can test the version of Scala you are using by typing:
scala -version
This should give an output similar to this:
Scala code runner version 2.11.7 -- Copyright 2002-2013, LAMP/EPFL
Steps for downloading and installing SBT
- On Mac, SBT can be installed using:
- Homebrew:
brew install sbt
- MacPort:
port install sbt
- Homebrew:
- On all other platforms, downloading and unpacking the archive can install SBT. After unpacking, add SBT to the
PATH
environment variable. The latest versions are available at http://www.scala-sbt.org/download.html. - SBT can also be installed to run from a JAR. This can be done by downloading the JAR from https://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/sbt-launch/0.13.11/sbt-launch.jar and then creating a script file to start it. Add the script file to the
PATH
environment variable.
Tip
Unix: Download the JAR file. Create an executable script file with this content:
#!/bin/bash
SBT_OPTS="-Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M"
java $SBT_OPTS -jar `
dirname $0`/sbt-launch.jar "$@"
Windows: Create a batch file with this content:
set SCRIPT_DIR=%~dp0
java -Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M -jar "%SCRIPT_DIR%sbt-launch.jar" %*
This is the barebone setup you need to write in order to create a very trivial project that builds using SBT.
Creating a project directory structure
There is a default project directory structure for SBT projects that defines where SBT will look for application code and tests. By default, SBT uses a similar directory structure to Maven:

Build definition files
SBT uses build.sbt
as a default build definition file. There are other variations of build definition files, such as .scala
and multi-project files. For the scope of this book, we will only look at the build.sbt
definition file.