One of my biggest gripes with Java (and all the languages that run on the JVM) is getting my project setup and building it. Maven is not my favorite, and ant..well..I don't like it either. Fortunately, if you want to start a new project in Scala, there is a great build tool available that takes a lot of the pain out of project management and building - SBT, simple-build-tool.
sbt is a simple build tool for Scala projects that aims to do the basics well. It requires Java 1.5 or later.
Installing SBT
I'm using Mac OS X, but the following instructions should be pretty much the same on any Unix based OS.
You can find the latest version of SBT here.
cd ~
wget http://simple-build-tool.googlecode.com/files/sbt-launcher-0.5.6.jar
sudo mv sbt-launcher-0.5.6.jar /usr/local/bin/sbt-launcher.jar
echo "java -Xmx512M -jar /usr/local/bin/sbt-launcher.jar \"\$@\"" | sudo tee /usr/local/bin/sbt
sudo chmod +x /usr/local/bin/sbt
This will install the SBT jar and create a script called sbt that will allow you to run the sbt jar.
Just type sbt and press enter, and you now have access to sbt.
$ sbt
Project does not exist, create new project? (y/N/s) : n
Creating a new Scala project
Now we will create a Hello World Scala project with SBT.
mkdir hello_scala
cd hello_scala
Running the sbt command in a directory where there is no project will prompt you to create one.
sbt
Project does not exist, create new project? (y/N/s) : y
Name: Hello, Scala!
Organization []:
Version [1.0]:
Scala version [2.7.7]:
sbt version [0.5.6]:
:: retrieving :: sbt#boot
confs: [default]
2 artifacts copied, 0 already retrieved (9911kB/72ms)
:: retrieving :: sbt#boot
confs: [default]
3 artifacts copied, 0 already retrieved (3409kB/15ms)
[success] Successfully initialized directory structure.
[info] Building project Hello, Scala! 1.0 using sbt.DefaultProject
[info] with sbt 0.5.6 and Scala 2.7.7
[success] Build completed successfully.
[info]
[info] Total build time: 0 s
Awesome. It handles all the Scala dependencies for us! Now let's create a file that contains our Hello, Scala example.
Below is the directory structure of an SBT project.
$ ls
lib project src
Creating our HelloScala sources and running
Now we are going to create our main file, HelloScala.scala
src/main/scala/HelloScala.scala
object HelloScala {
def main(args: Array[String]) {
println("Hello, Scala!")
}
}
And now we can build and run it by just issuing the following:
sbt run
And the output:
[info] Building project Hello, Scala! 1.0 using sbt.DefaultProject
[info] with sbt 0.5.6 and Scala 2.7.7
[info]
[info] == compile ==
[info] Source analysis: 0 new/modified, 0 indirectly invalidated, 0 removed.
[info] Compiling main sources...
[info] Nothing to compile.
[info] Post-analysis: 2 classes.
[info] == compile ==
[info]
[info] == copy-resources ==
[info] == copy-resources ==
[info]
[info] == run ==
[info] Running HelloScala ...
Hello, Scala!
[info] == run ==
[success] Successful.
[info]
[info] Total time: 0 s
[success] Build completed successfully.
[info]
[info] Total build time: 1 s
And that's it. Setting up a new Scala project with SBT is painless. In the next part I will talk about managing dependencies and how SBT makes this also very easy.
You can read a lot more about SBT by checking out their wiki.
Update 09/18/2011 - If you are on a Mac, its probably easier to install sbt this way:
brew install sbt
The above command requires homebrew, a package manager for OS X. Get it here: https://github.com/mxcl/homebrew