1. installing sbt
-
installing sbt on mac
$ brew install sbt
-
installing sbt on ubuntu
$ sudo echo "deb http://dl.bintray.com/sbt/debian /" | \ sudo tee -a /etc/apt/sources.list.d/sbt.list $ sudo apt-get update $ sudo apt-get install sbt
2. hello, world
-
hw.scala
$ pico hw.scala object Hi { def main(args: Array[String]) = println("Hi!") }
-
build
$ sbt ... > run ... Hi!
-
build definition
$ pico build.dbt lazy val root = (project in file(".")). settings( name := "hello", version = "1.0", scalaVersion := "2.11.4" ) # if you plan package your project in a jar # you will want to set at least the name and version in a build.sbt
directory structure
-
hello
is base directory -
source code
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>
-
sbt build definition files
build.sbt project/ Build.scala # you mac see `.sbt` files inside `project/` # but they are not equivalent to `.sbt` in `base directory`
-
build products
# compiled classes, package jars, managed files, caches, doc # will be written to the target directory by default
4. running
-
interactive mode
$ sbt > compile > run > exit > Ctrl+D (Unix) > Ctrl+Z (Windows)
-
batch mode
$ sbt clean compile "testOnly TestA TestB"
-
continuous build and test
> ~ compile
-
common commands
> clean > compile > test > console > run <argument> > package > help > reload
-
tab completion
-
history commands
> ! > !! > !: > !:n > !n > !-n > !string > !?string
5. .sbt build definition
-
three flavors of build definition
-
multi-project
.sbt
-
bare
.sbt
-
.scala
-
-
what is a build definition
# in `build.sbt` create project definition lazy val root = (project in file("."))
-
how
build.sbt
defines settingslazy val commonSettings = Seq( organization := "com.example", version := "0.1.0", scalaVersion := "2.11.4" ) lazy val root = (project in file(".")). settings(commonSettings: _*). settings( name := "hello" )
-
keys
-
types
SettingKey[T] TaskKey[T] InputKey[T]
-
build-in keys
# a `build.sbt` implicitly has an import sbt.Keys._ # so `sbt.Keys.name can be referred to as `name`
-
custom keys
# define a key for a new task called `hello` lazy val hello = taskKey[Unit]("An example task")
-
task vs setting keys
# a TaskKey[T] is said to define a `task` # tasks are operations such as `compile` or `package` # they may return `Unit` (Unit is scala for void) # `package` is a `TaskKey[File]` # and its value is the `jar` file it creates
-
-
defining tasks and settings
-
use
:=
you can assign a value to a setting and a computation to a task -
types for tasks and settings
-
-
keys in sbt interactive mode
-
imports in build.sbt
import sbt._ import Process._ import Kyes._
-
adding library dependencies
val derby = "org.apache.derby" % "derbt" % "10.4.1.3" lazy val commonSettings = Seq( organization := "com.example", version := "0.1.0", scalaVersion := "2.11.4" ) lazy val root = (project in file(".")). settings(commonSettings: _*). settings( name := "hello", libraryDependencies += derby )