5. Build Tools for Scala

In this section, we provide a brief overview of build tools for Scala. In general, build tools support the build process in several ways:

  • structured representation of the project dependency graph
  • management of the build lifecycle (compile, test, run)
  • management of external dependencies

5.1. Why Use a Build Tool?

When using the Java or Scala command-line tools, the developer is responsible for setting the dreaded classpath. This can quickly become unwieldy when dependencies even as simple as JUnit are involved, so this is not something you would usually want to do manually.

5.2. Brief History of Build Tools

Unix make, Apache ant
These tools manage the build lifecycle but not external dependencies.
Apache maven
This tool also manages external dependencies but requires a lot of XML-based configuraration.
1
2
3
4
5
      <dependency>
              <groupId>org.restlet</groupId>
              <artifactId>org.restlet.ext.spring</artifactId>
              <version>${restlet.version}</version>
      </dependency>
Apache ivy, Gradle, Scala’s Simple Build Tool (sbt), etc.
These tools emphasize convention over configuration in support of agile development processes. sbt is compatible with ivy and designed primarily for Scala development. For example, ivy uses a structured but lighter-weight format:
1
      <dependency org="junit" name="junit" rev="4.11"/>

5.3. Sbt

in the simplest case, sbt does not require any configuration and will use reasonable defaults.

SBT supports two configuration styles, one based on a simple subset of Scala, and one based on the full Scala language for configuring all aspects of a project.

5.3.1. build.sbt format

A minimal SBT build.sbt file would look like this. The empty lines are required, and the file must be placed in the project root folder.

1
2
3
4
5
  name := "integration-scala"

  version := "0.0.2"

  scalaVersion := "2.10.1-RC1"

Additional dependencies can be specified either one at a time

1
  libraryDependencies += "junit" % "junit" % "4.11"

or as a group

1
2
3
4
  libraryDependencies ++= Seq(
    "junit" % "junit" % "4.11",
    "com.novocode" % "junit-interface" % "0.10-M2" % "test"
  )

5.3.2. Build.scala format

Examples of more complex Scala-based project configurations can be found in these examples:

5.4. Plugin Ecosystem

sbt includes a growing plugin ecosystem. Key examples include

sbteclipse
automatically generates an Eclipse project configuration from an sbt one.
sbt-start-script
generates a start script for running a Scala application outside of sbt.

5.5. Starting from Scratch

A remaining question is how to start new projects from scratch. One can start with a skeleton and modify it, or one can use maven archetypes, which are somewhat configuration-heavy and a bit hard to use.

Alternatively, Giter8 is a command-line tool that instantiates templates stored in Git repositories. Giter8 itself is based on Scala but handles templates in any language(s). For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
  $ g8 fxthomas/android-app

  Template for Android apps in Scala

  package [my.android.project]:
  name [My Android Project]: my-android-project
  main_activity [MainActivity]:
  min_api_level [8]:
  scala_version [2.10.0]:
  api_level [16]: 17
  useProguard [true]:
  scalatest_version [1.9.1]:

  Applied fxthomas/android-app.g8 in my-android-project

Now we have a hello world app that is ready to run.

1
2
  $ sbt android:package-debug
  $ sbt android:start-emulator