Using SBT

Be aware that version 0.10.x of SBT is not backward compatible with earlier versions.
For more information see Migrating from SBT 0.7.x to 0.10.x

Using SBT 0.10 and newer

sbt is a build tool for Scala and Java projects that aims to do the basics well.

Three ways to get started:

See Scala-starter Github project for a minimal SBT setup.

Lift build config template

See also Tutorial: Building your first Lift app with sbt.

Create a build.sbt file in the root of your project

name := "projname"
 
scalaVersion := "2.9.1"
 
seq(webSettings: _*)

// If using JRebel with 0.1.0 of the sbt web plugin
//jettyScanDirs := Nil
// using 0.2.4+ of the sbt web plugin
scanDirectories in Compile := Nil


// you can also add multiple repositories at the same time
resolvers ++= Seq("snapshots"     at "http://oss.sonatype.org/content/repositories/snapshots",
                  "staging"       at "http://oss.sonatype.org/content/repositories/staging",
                  "releases"      at "http://oss.sonatype.org/content/repositories/releases"
                 )

// if you have issues pulling dependencies from the scala-tools repositories (checksums don't match), you can disable checksums
//checksums := Nil

libraryDependencies ++= {
  val liftVersion = "2.4" // Put the current/latest lift version here
  Seq(
    "net.liftweb" %% "lift-webkit" % liftVersion % "compile->default",
    "net.liftweb" %% "lift-mapper" % liftVersion % "compile->default",
    "net.liftweb" %% "lift-wizard" % liftVersion % "compile->default")
}

// when using the sbt web app plugin 0.2.4+, use "container" instead of "jetty" for the context
// Customize any further dependencies as desired
libraryDependencies ++= Seq(
  "org.eclipse.jetty" % "jetty-webapp" % "8.0.4.v20111024" % "container", // For Jetty 8
  //"org.eclipse.jetty" % "jetty-webapp" % "7.3.0.v20110203" % "container", // For Jetty 7
  //"org.mortbay.jetty" % "jetty" % "6.1.22" % "jetty,test", // For Jetty 6, add scope test to make jetty avl. for tests
  "org.scala-tools.testing" % "specs_2.9.0" % "1.6.8" % "test", // For specs.org tests
  "junit" % "junit" % "4.8" % "test->default", // For JUnit 4 testing
  "javax.servlet" % "servlet-api" % "2.5" % "provided->default",
  "com.h2database" % "h2" % "1.2.138", // In-process database, useful for development systems
  "ch.qos.logback" % "logback-classic" % "0.9.26" % "compile->default" // Logging
)

// by default, it listens on port 8080; use the following to override
port in container.Configuration := 8081

Create a new file called project/plugins/build.sbt or project/plugins.sbt with the following content:

libraryDependencies <+= sbtVersion(v => "com.github.siasia" %% "xsbt-web-plugin" % (v+"-0.2.10")) // moved to repo1
// for the older version of the plugin, use the following instead:
// you will need to change jetty's scope from 'container' to 'jetty' above
//libraryDependencies <+= sbtVersion(v => "com.github.siasia" %% "xsbt-web-plugin" % ("0.1.0-"+v))

The xsbt-web-plugin 0.2.4+ uses container to prefix all of its tasks, so the equivalent of jetty-run and ~prepare-webapp is now just ~container:start.

Learn more about the SBT Webapp plugin here: (https://github.com/siasia/xsbt-web-plugin)

Getting source jars SBT 10+

> update-classifiers

Eclipse users see below

Using Eclipse with SBT 0.10+

sbteclipse is a plugin for SBT 0.10+ providing a command to create Eclipse project files from a SBT project.

To make this SBT plugin a general project independent global SBT plugin put the following definition in your build.sbt file in the ~/.sbt/plugins directory, or if everyone working on the project is using eclipse, you can put the definition in a build.sbt file in the project/plugins folder of your project. Pay strong attention to the blank lines!

For SBT 10.x :

resolvers += {
  val typesafeRepoUrl = new java.net.URL(http://repo.typesafe.com/typesafe/releases)
  val pattern = Patterns(false, [organisation]/[module]/[sbtversion]/[revision]/[type]s/[module](-[classifier])-[revision].[ext])
  Resolver.url(Typesafe Repository, typesafeRepoUrl)(pattern)
}

libraryDependencies <<= (libraryDependencies, sbtVersion) { (deps, version) => deps :+ (com.typesafe.sbteclipse %% sbteclipse % 1.2 extra(sbtversion -> version)) }

For SBT 0.11.x:

addSbtPlugin(com.typesafe.sbteclipse % sbteclipse-plugin % 2.0.0-M3)

When you have set up the plugin you can use the command eclipse with-sources (0.10.x) or eclipse with-sources=true (0.11.x) in an SBT session to create Eclipse project files and then import the project into Eclipse using the Import Wizard i.e. Eclipse menu File-->Import... to import Existing Projects into Workspace. Use the same command to update your project files if you change your build.sbt to have new dependencies.


Using the old SBT (0.7 and earlier)

SBT can be used to to manage a Lift project. The SBT documentation is the best place to get familiar with all of the options available with sbt.

To get started however you can just go to the getting started page which contains links to both .tar and .zip files that contain several examples are empty sbt based lift projects. The lift_basic project is a good place to start, your first time using lift.

Simply unzip/tar the downloaded file. cd into the directory and type sbt to get started.

Get dependencies

Get the dependencies listed in your project file by running the update command from the sbt console.

Note: It can take several minutes for SBT to download all the dependencies. Future updates won't take as long.

Test with Jetty (SBT 0.7)

You can run your webapp with the included Jetty server. Start it with the jetty-run command from the sbt console. Your webapp will be running at http://localhost:8080. This port can be configured with e.g. override val jettyPort = 8081 The webapp can be stopped with jetty-stop.

If you want a console with access to classpath dependencies, console (this can be used while jetty is running). To run any SBT command continuously while scanning for changes, prefix it with ~: ~compile, ~test, ~test-only some.test.class, ~jetty, etc. For ~jetty, you will still have to hit return at the SBT shell to restart jetty in order to see changes. To avoid this:

override def jettyWebappPath  = webappPath

If you're running with JRebel, avoid unnecessary redeploys with

override def scanDirectories = Nil 

The package action will create a WAR file for when you are ready to deploy your webapp on a production server.

To avoid frequent OutOfMemory errors, try modifying your sbt shell script to the following:

java -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256m -Xmx512M -Xss2M -jar `dirname $0`/sbt-launch.jar "$@"

The JVM options above should keep things running smoothly.

Maven to SBT converter

See some simple scripts on Stack Overflow: [[url:http://stackoverflow.com/questions/2972195/migrating-from-maven-to-sbt]]

Getting source jars SBT 0.7

> olegychRepo at https://bitbucket.org/olegych/mvn/raw/default
> sources is com.olegych sbt-sources-processor 0.2.0
> sources