Saturday, December 3, 2011

Scala project skeleton using SBT

Let's create a minimal scala project skeleton, based on SBT build system, with some SBT plugins enabled (eclipse and assembly). The goal is to generate a single standalone executable jar (without any external dependencies), using scala language (and/or java) and eclipse as an optional IDE.
The scala project skeleton is available as a google code project. visit : scala-dummy-project or checkout the project :
# Quick start
$ git clone git://github.com/dacr/scala-dummy-project.git
$ cd scala-dummy-project
$ sbt assembly
$ java -jar target/dummy.jar
Hello userX
Requirements are :
- JVM >= 1.5 available in your PATH
- get and install SBT (Simple Build Tool)
- if you want to use eclipse, download and install Scala IDE for eclipse

To use eclipse, you must initialize the project for eclipse, run "sbt eclipse" or just "eclipse" from SBT console.
All steps :
$ cd scala-dummy-project
$ sbt 
> eclipse
> run
> test
> assembly
> exit
$ java -jar target/dummy.jar
#OR
$ sbt run
The project is configured as follow :
import AssemblyKeys._

seq(assemblySettings: _*)

name := "ScalaDummyProject"

version := "0.1"

scalaVersion := "2.9.1"

mainClass in assembly := Some("dummy.Dummy")

jarName in assembly := "dummy.jar"

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

// Junit is just required for eclipse to be able to start tests.
libraryDependencies += "junit" % "junit" % "4.10" % "test"

What's great with sbt is that it manages all dependencies for you (even sub-dependencies !), no need to download anything, everything is done automatically.
CONTEXT : Scala 2.9.1 / SBT 0.11 / ScalaTest 1.6.1 / sbt-assembly 0.7.2 / sbteclipse 1.5.0

No comments:

Post a Comment