My favorites | Sign in
Project Logo
                
Search
for
Updated Nov 03, 2009 by dmharrah
Labels: Featured
Setup  

Introduction

This page describes how to set up your project for use with sbt.

The basic steps are:

  1. Create a script to launch sbt.
  2. Create your project with sbt.
  3. Put libraries in lib and sources in src.
  4. Read RunningSbt for basic usage instructions.
  5. Read BuildConfiguration for further configuration.

Launching Sbt

The easiest way to launch sbt is to create a one-line script. Download sbt-launcher.jar if you have not already. (Note: do not put sbt-launcher.jar in your $SCALA_HOME\lib directory.)

Unix

Put the jar in your ~/bin directory, put the line

java -Xmx512M -jar `dirname $0`/sbt-launcher.jar "$@"

in a file called sbt in your ~/bin directory and do

$ chmod u+x ~/bin/sbt

This allows you to launch sbt in any directory by typing sbt at the command prompt.

sbt will pick up any HTTP proxy settings from the http.proxy environment variable. If you are behind a proxy requiring authentication, you must in addition pass flags to set the http.proxyUser and http.proxyPassword properties:

java -Dhttp.proxyUser=username -Dhttp.proxyPassword=mypassword -Xmx512M -jar `dirname $0`/sbt-launcher.jar "$@"

Windows

Create a batch file sbt.bat:

set SCRIPT_DIR=%~dp0
java -Xmx512M -jar "%SCRIPT_DIR%sbt-launcher.jar" %*

and put the jar in the same directory as the batch file. Put sbt.bat on your path so that you can launch sbt in any directory by typing sbt at the command prompt.

If you are behind a proxy on Windows, add flags to this second line for proxy host, port, and if applicable, username and password:

java -Dhttp.proxyHost=myproxy -Dhttp.proxyPort=8080 -Dhttp.proxyUser=username -Dhttp.proxyPassword=mypassword -Xmx512M -jar "%SCRIPT_DIR%sbt-launcher.jar" %*

Create Project

Full Setup

When you run sbt and it does not find a project in the current directory, it asks if you want to create a new project. If you say yes, sbt will prompt for some basic information:

See Loader for more information on setting the versions of Scala and sbt to use to build your project. With the above information, sbt creates the project properties file and directory structure. These are described in the following section.

Quick Setup

A new option in sbt 0.5 is to choose s (for scratch) when prompted to create a new project. This can be used to quickly get sbt compiling and running code without needing to do a full project setup. This option skips all of the prompts in a full setup and configures sbt to look in the current directory for sources and jars in addition to the usual places in a full project. Using sbt for a hello world might look like:

  $ echo 'object Hi { def main(args: Array[String]) { println("Hi!") } }' > hw.scala
  $ sbt
  Project does not exist, create new project? (y/N/s) : s
  ...
  > run
  ...
  Hi!

Directory Layout

Sources

Sbt uses the same directory structure as Maven for source files by default (all paths are relative to the project directory):

  src/
    main/
      resources/
         <files to include in main jar here>
      scala/
         <main Scala sources>
      java/
         <main Java sources>
    test/
      resources
         <files to include in test jar here>
      scala/
         <test Scala sources>
      java/
         <test Java sources>

Other directories in src/ will be ignored. Additionally, all hidden directories will be ignored.

Dependencies

All dependencies (jars) go in the

  lib/

directory or any subdirectory of lib. Again, hidden directories will be ignored. If you want to use ScalaCheck, specs, or ScalaTest for testing, those jars should go in here as well. Alternatively, you can configure sbt to automatically manage your dependencies (see LibraryManagement).

Build Configuration

Build configuration is done in the project directory:

  project/
    build.properties
    build/
    boot/

The build.properties file contains the project name, version, and organization, the versions of Scala and sbt used to build the project, and any other user-defined properties (see Properties). You can directly edit this file to change these properties or you can use the set command at the interactive prompt (see Basic Usage). The build directory is where further configuration is done and is described in BuildConfiguration. The boot directory is where the versions of Scala and sbt used to build the project are downloaded to.

Products

Generated files (classes, jars, analysis, and documentation) will be written to the

  target/

directory. Automatically managed dependencies are downloaded to:

  lib_managed/

Version Control

sbt creates and uses several directories that you will normally want to exclude from version control:

  target/
  lib_managed/
  project/boot/
  project/build/target/
  project/plugins/target/
  project/plugins/lib_managed/
  project/plugins/src_managed/

A .gitignore for an sbt project should contain these entries:

target/
lib_managed/
src_managed/
project/boot/

Next Step

Read Basic Usage for basic sbt usage information.

Summary

  1. Create a launcher script.
  2. Setup your project information and directory structure by running sbt in your project directory.
  3. Put libraries in lib or configure automatic dependency management.
  4. Read RunningSbt for basic usage instructions.
  5. Start with BuildConfiguration for further project configuration.

Comment by lypanov, Apr 15, 2009

copying the launcher into the bin dir and creating the script

#!/bin/sh
java -Xmx256M -jar `dirname $0`/sbt-launcher.jar "$@"

works for me

Comment by jzaugg, Jun 09, 2009

Or on Windows:

set SCRIPT_DIR=%~dp0
java %SBT_OPTS% -Xmx256M -jar "%SCRIPT_DIR%sbt-launcher-0.4.6.jar" %*

SBT_OPTS allows one to configure a HTTP proxy which is needed for Ivy. For example: set SBT_OPTS=-Dhttp.proxyHost=172.22.100.15 -Dhttp.proxyPort=8080

I suggest this should be added to FAQ.

Comment by dmharrah, Jun 09, 2009

Thanks to both of you.

jzaugg: on unix, you can configure the proxy with the http.proxy environment variable and sbt will pick it up. Is http.proxy used in windows?

-Mark

Comment by zhu.tan, Jun 29, 2009

dmharrah, thanks for this nice simple, yet elegant tool. It is a proof that Scala can be used in a wide range situations, from a build script to a large application. It saves me lots of time in building Scala apps and learning Scala!

Comment by dmharrah, Aug 24, 2009

sbt does work with 1.5. It might be something with your setup, since people use Mac+1.5. You might try asking on the mailing list. It might have come up before.

-Mark


Sign in to add a comment
Hosted by Google Code