Methods & Tools Software Development Magazine

Software Development Magazine - Programming, Software Testing, Project Management, Jobs

Click here to view the complete list of tools reviews

This article was originally published in the Spring 2012 issue of Methods & Tools


SBT Scala Build Tool

Evgeny Goldin, http://evgeny-goldin.com/

"sbt" is a Scala-oriented build tool, developed by Mark Harrah. Unlike most other general build tools, "sbt" is targeted to support only Scala and Java projects. In addition to traditional batch operation where a number of build tasks are executed in sequence, "sbt" provides a special interactive mode, making Scala builds significantly faster by using the same JVM, watching project sources for modifications and keeping the compiled classes "warm". As a build tool, it is capable of compiling Scala code, packaging archive artifacts, running Scala code, executing tests and many other build operations.

Web Site: https://github.com/harrah/xsbt/wiki

Version Tested: 0.11.0 on Mac OS X 10.7.3, Java 1.6.0_29 and Scala 2.9.1, March 2012

License & Pricing: https://github.com/harrah/xsbt/blob/0.11/LICENSE, Free

Documentation:

Getting started guide: https://github.com/harrah/xsbt/wiki/Getting-Started-Welcome

Detailed topics: https://github.com/harrah/xsbt/wiki/Detailed-Topics

Default settings and tasks: http://harrah.github.com/xsbt/latest/sxr/Keys.scala.html

Plugins list: https://github.com/harrah/xsbt/wiki/sbt-0.10-plugins-list

Support:

Mailing list: https://groups.google.com/forum/?fromgroups#!forum/simple-build-tool

Issue tracker: https://github.com/harrah/xsbt/issues

Videos:

"SBT Introduction & Cookbook for Scala": http://goo.gl/sR552

"sbt: Design and Implementation": http://days2010.scala-lang.org/node/138/165

"sbt 0.9: Why, what, how?": https://vimeo.com/20263617

Installation

One simple way to install "sbt" would be downloading a self-executable sbt-launch.jar file and running it with an "sbt" script:

java -Xmx512M -jar "sbt-launch.jar" %*

If you're a Mac user then "sbt" can be installed with MacPorts:

sudo port install sbt

When "sbt" is run for the first time all required dependencies are downloaded automatically. Make sure you have everything set up correctly by running "sbt" and then typing "about":

$ sbt
[info] Loading global plugins from /Users/evgenyg/.sbt/plugins
[info] Loading project definition from /Users/evgenyg/Projects/scala-examples/project
[info] Set current project to scala-examples (in build file:/Users/evgenyg/Projects/scala-examples/)
> about
[info] This is sbt 0.11.0
[info] The current project is {file:/Users/evgenyg/Projects/scala-examples/} hello
[info] The current project is built against Scala 2.9.1
[info] Available Plugins: org.sbtidea.SbtIdeaPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.9.1
> 

If you're using IntelliJ IDEA then you can also install two additional plugins:

  • sbt-idea plugin for auto-generating IDEA project files using your "sbt" Scala configs.
  • SBT plugin for running SBT interactive console from inside IntelliJ IDEA.

"sbt" example project

"sbt" example is available at https://github.com/evgeny-goldin/scala-examples. This is a simple Scala project making use of Twitter's API to make a search request for user query. You can clone it and run as follows (commands typed are marked in bold):

$ git clone git://github.com/evgeny-goldin/scala-examples.git
Cloning into 'scala-examples'...
remote: Counting objects: 24, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 24 (delta 3), reused 24 (delta 3)
Receiving objects: 100% (24/24), 2.06 KiB, done.
Resolving deltas: 100% (3/3), done.
$ cd scala-examples
$ sbt
[info] Loading global plugins from /Users/evgenyg/.sbt/plugins
[info] Set current project to scala-examples (in build file:/Users/evgenyg/Temp/scala-examples/)
> run something[info] Running com.goldin.scala.Twitter4j something
...
[iam_imperfect]: [RT @EhIs4Adam: Beginning to feel the need to work out or something... 
you know because of the whole 2012 thing. #EndOfDays #PumpItUp]
[bnsbbyx3]: [If you're truly in love, the other person should be your bestfriend. If they're not.. 
there's something wrong with your relationship.]
...
[success] Total time: 1 s, completed Feb 25, 2012 6:44:43 PM
> 

With just a few keystrokes we've run a Twitter query for "something". Try it out for any other query!

Interactive Mode

Java-based build tools operate in batch mode: you run the tool and provide it with goals or tasks to execute, such as "ant" "clean zip", "mvn clean install" or "gradle test". The build tool boots the JVM, processes the build file, reads the goals and executes them. Gradle is capable of running a background daemon process, saving on JVM boot up time, but the overall picture stays mostly the same.

"sbt" took a new approach and provided an interactive mode in which you only invoke "sbt" once, as we've just seen, and then "sbt" waits for your commands. "run" something in the previous example was a "run" command followed by the "something argument, invoking the Scala code and passing it the argument. The reference to the commands provided by the tool is available at http://goo.gl/vZ73u and it includes the following commands, among many others: update, compile, package, test, publish, and doc. http://goo.gl/2y7bU provides a list of all "sbt" tasks available by default.

Two special commands "show" and "inspect" allow to examine the build environment of the project in more details:

> show sources
[info] ArrayBuffer(/Users/evgenyg/Temp/scala-examples/src/main/scala/com/goldin/scala/QueryRunner.scala, 
/Users/evgenyg/Temp/scala-examples/src/main/scala/com/goldin/scala/Twitter4j.scala)
> show dependency-classpath
[info] ArrayBuffer(Attributed(/Users/evgenyg/.sbt/boot/scala-2.9.1/lib/scala-library.jar),
Attributed(/Users/evgenyg/.ivy2/cache/org.twitter4j/twitter4j-core/jars/twitter4j-core-2.2.5.jar),
Attributed(/Users/evgenyg/.ivy2/cache/org.slf4j/slf4j-api/jars/slf4j-api-1.6.4.jar),
Attributed(/Users/evgenyg/.ivy2/cache/ch.qos.logback/logback-classic/jars/logback-classic-1.0.0.jar),
Attributed(/Users/evgenyg/.ivy2/cache/ch.qos.logback/logback-core/jars/logback-core-1.0.0.jar))
> inspect scala-version
[info] Setting: java.lang.String = 2.9.1
[info] Description:
[info] The version of Scala used for building.
...
> inspect clean
[info] Task: Unit
[info] Description:
[info] Deletes files produced by the build, such as generated sources, compiled classes, and task caches.
...
> 

"build.sbt"

"sbt" is guided by the "build.sbt" file stored in project's root:

organization := "com.goldin"
name         := "scala-examples"
version      := "1.0"
scalaVersion := "2.9.1"
showSuccess  := true
showTiming   := true
libraryDependencies += "org.twitter4j"  % "twitter4j-core"  % "2.2.5"
libraryDependencies += "org.slf4j"      % "slf4j-api"       % "1.6.4"
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.0.0"

As you see, it contains the standard settings of a build model, probably familiar to you from Maven or Gradle. Dependency delegation is performed using Ivy and the language used to specify the model is Scala. See http://goo.gl/aW4cI for more details about settings available and http://goo.gl/2y7bU for a complete list of all default settings. Note that "sbt" follows the standard Maven conventions: sources are expected to be in "src/main/java" and "src/main/scala", tests are expected to be in "src/test/java" and "src/test/scala". It means very little needs to be done in order to build your Scala project: put your sources in "src/main/scala", add the corresponding "build.sbt" file and type "sbt" package or just "sbt" and then "package".

Why SBT?

What makes "sbt" different from other build tools? After all, one can compile Scala sources with Maven and Gradle, so what's the point of using a new build tool?

As it turns out, there are plenty of reasons for using "sbt".

First of all, "sbt" was developed as a Scala build tool from the very beginning. As I mentioned earlier, most Java-based build tools are general in their nature, i.e., they can work with a variety of JVM languages: Java, Groovy, Scala, Jython, JRuby, etc. "sbt" specializes in Scala which makes it more powerful in various Scala-related scenarios. For example, it is possible to switch the Scala version from "2.9.1" to "2.9.0" with the "++2.9.0" command in the interactive mode, which makes it especially convenient for running tests in various Scala versions. Another example of a Scala-specific command is "console" starting a familiar Scala REPL with project files compiled and available in the classpath.

The tool itself is written in Scala and all build files are written in Scala as well. It means that Scala knowledge is expected but it also makes the job easier for Scala developers. It also means Scala rigorous type safety is built into your build files.

One of "sbt" cornerstone principles is "same JVM execution" using powerful interactive mode. It eliminates JVM startup costs and keeps compiled Scala classes "warm", thus making project recompilation an immediate event the only sources modified are actually compiled. A special "~ command"syntax watches sources for modifications and re-runs the command automatically whenever modifications are recognized, freeing you from a need to re-run it manually. This way, the "~ test" will re-run project's tests every time a new test is added. The interactive mode is also accompanied with tab completion when commands are typed. As you see, it provides a tremendous speedup and advantage over the traditional "type and wait" way of running build tools, with Gradle being one of the few tools that work hard to minimize the wait time with its background daemon. It's good to see "sbt" takes this issue into a whole new level.

"sbt" allows for parallel aggregation and execution of multi-project builds, providing yet another attractive speedup for your builds.

As you see, "sbt" is all about Scala, flexibility and performance, therefore it is a compelling alternative in the build tools arena. If your project is developed in Scala, I believe this should be the first tool to check out.


More Continuous Integration Knowledge


Click here to view the complete list of tools reviews