8. インストール
$ brew install sbt
==> Downloading https://homebrew.bintray.com/bottles/
sbt-0.13.8.yosemite.bottle.tar.gz
Already downloaded: /Library/Caches/Homebrew/
sbt-0.13.8.yosemite.bottle.tar.gz
==> Pouring sbt-0.13.8.yosemite.bottle.tar.gz
==> Caveats
You can use $SBT_OPTS to pass additional JVM options to SBT:
SBT_OPTS="-XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M"
This formula is now using the standard typesafe sbt launcher script.
Project specific options should be placed in .sbtopts in the root of your
project.
Global settings should be placed in /usr/local/etc/sbtopts
==> Summary
🍺 /usr/local/Cellar/sbt/0.13.8: 5 files, 1.2M
9. プロジェクトの最小構成
$ brew install typesafe-activator
==> Downloading https://downloads.typesafe.com/typesafe-activator/1.3.2/typesafe-
activator-1.3.2.zip
Already downloaded: /Library/Caches/Homebrew/typesafe-activator-1.3.2.zip
🍺 /usr/local/Cellar/typesafe-activator/1.3.2: 4413 files, 470M, built in 9 seconds
$ activator new sbt-simple
Fetching the latest list of templates...
Browse the list of templates: http://typesafe.com/activator/templates
Choose from these featured templates or enter a template name:
1) minimal-akka-java-seed
2) minimal-akka-scala-seed
3) minimal-java
4) minimal-scala
5) play-java
6) play-scala
(hit tab to see a list of all templates)
> 4
OK, application "sbt-simple" is being created using the "minimal-scala" template.
To run "sbt-simple" from the command line, "cd sbt-simple" then:
/Users/cw-junichi/temp/sbt-simple/activator run
To run the test for "sbt-simple" from the command line, "cd sbt-simple" then:
/Users/cw-junichi/temp/sbt-simple/activator test
To run the Activator UI for "sbt-simple" from the command line, "cd sbt-simple" then:
/Users/cw-junichi/temp/sbt-simple/activator ui
10. 生成されたファイル群
.
"## LICENSE
"## activator
"## activator-launch-1.3.2.jar
"## build.sbt
"## project
$ &## build.properties
&## src
"## main
$ &## scala
$ &## com
$ &## example
$ &## Hello.scala
&## test
&## scala
&## HelloSpec.scala
11. build.sbt
name := """sbt-simple"""
version := "1.0"
scalaVersion := "2.11.7"
// Change this to another test framework if you prefer
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.4" % "test"
// Uncomment to use Akka
//libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.3.11"
project/build.properties
Activator-generated Properties
#Thu Jul 30 14:55:12 JST 2015
template.uuid=e17acfbb-1ff5-41f5-b8cf-2c40be6a8340
sbt.version=0.13.8 # sbtのバージョンを固定できる
13. sbt compile
$ sbt
[info] Loading project definition from /Users/cw-junichi/temp/sbt-simple/project
[info] Updating {file:/Users/cw-junichi/temp/sbt-simple/project/}sbt-simple-build...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Set current project to sbt-simple (in build file:/Users/cw-junichi/temp/sbt-
simple/)
> compile
[info] Updating {file:/Users/cw-junichi/temp/sbt-simple/}sbt-simple...
[info] Resolving jline#jline;2.12.1 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/cw-junichi/temp/sbt-simple/target/
scala-2.11/classes...
[success] Total time: 3 s, completed 2015/07/30 15:37:49
$ sbt compile
[info] Loading project definition from /Users/cw-junichi/temp/sbt-simple/project
[info] Set current project to sbt-simple (in build file:/Users/cw-junichi/temp/
sbt-simple/)
[info] Updating {file:/Users/cw-junichi/temp/sbt-simple/}sbt-simple...
[info] Resolving jline#jline;2.12.1 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/cw-junichi/temp/sbt-simple/target/
scala-2.11/classes...
[success] Total time: 3 s, completed 2015/07/30 15:38:19
14. sbt run
package com.example
object Hello {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
$ sbt run
[info] Loading project definition from /Users/cw-junichi/temp/sbt-simple/
project
[info] Set current project to sbt-simple (in build file:/Users/cw-junichi/
temp/sbt-simple/)
[info] Running com.example.Hello
Hello, world!
[success] Total time: 0 s, completed 2015/07/30 15:40:02
15. sbt test
import org.scalatest._
class HelloSpec extends FlatSpec with Matchers {
"Hello" should "have tests" in {
true should === (true)
}
}
$ sbt test
[info] Loading project definition from /Users/cw-junichi/temp/sbt-simple/project
[info] Set current project to sbt-simple (in build file:/Users/cw-junichi/temp/sbt-simple/)
[info] Compiling 1 Scala source to /Users/cw-junichi/temp/sbt-simple/target/scala-2.11/
test-classes...
[info] HelloSpec:
[info] Hello
[info] - should have tests
[info] Run completed in 294 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 4 s, completed 2015/07/30 15:42:53
22. セッティング
lazy val message = settingKey[String]("message")
message := “hello”
セッティングキーを定義して、セッティングに対して値を割り当てる
$ sbt message
[info] Loading project definition from /Users/cw-junichi/temp/sbt-simple/
project
[info] Set current project to sbt-simple (in build file:/Users/cw-junichi/
temp/sbt-simple/)
[info] hello
セッティングキー名で呼び出す
23. タスク
lazy val hello = taskKey[Unit]("An example task")
hello := { println(“Hello!") }
タスクキーを定義して、タスクに対して関数を割り当てる
$ sbt hello
[info] Loading project definition from /Users/cw-junichi/temp/sbt-simple/
project
[info] Set current project to sbt-simple (in build file:/Users/cw-junichi/
temp/sbt-simple/)
Hello!
[success] Total time: 0 s, completed 2015/07/31 10:51:11
タスクキー名で呼び出す
24. タスクからセッティングを参照する
lazy val message = settingKey[String]("message")
lazy val say = taskKey[Unit]("say task")
message := "hello"
say := { println(message.value) }
セッティングキー名.valueで割当られている値を参照する
$ sbt say
[info] Loading project definition from /Users/cw-junichi/temp/sbt-simple/
project
[info] Set current project to sbt-simple (in build file:/Users/cw-junichi/
temp/sbt-simple/)
hello
[success] Total time: 0 s, completed 2015/07/31 12:04:46
25. タスクの結果を得る
lazy val message = settingKey[String]("message")
lazy val modifier = taskKey[String]("modifier task")
lazy val display = taskKey[Unit]("display task")
message := "hello"
modifier := { "{{{" + message.value + "}}}" }
display := { println(modifier.value) }
タスクキー.valueでタスクの結果を得ることができる
$ sbt display
[info] Loading project definition from /Users/cw-junichi/temp/sbt-simple/
project
[info] Set current project to sbt-simple (in build file:/Users/cw-junichi/
temp/sbt-simple/)
{{{hello}}}
[success] Total time: 0 s, completed 2015/08/01 7:43:23
26. タスクの実行意味論
val startServer = taskKey[Unit]("start server")
val stopServer = taskKey[Unit]("stop server")
val sampleIntTask = taskKey[Int]("A sample int task.")
val sampleStringTask = taskKey[String]("A sample string task.")
startServer := {
println("starting...")
Thread.sleep(500)
}
stopServer := {
println("stopping...")
Thread.sleep(500)
}
sampleIntTask := {
startServer.value
val sum = 1 + 2
println("sum: " + sum)
stopServer.value // THIS WON'T WORK
sum
}
sampleStringTask := {
startServer.value
val s = sampleIntTask
.value.toString
println("s: " + s)
s
}
$ sbt sampleStringTask
[info] Loading project definition from /
Users/cw-junichi/temp/sbt-simple/project
[info] Set current project to sbt-simple (in
build file:/Users/cw-junichi/temp/sbt-
simple/)
stopping...
starting...
sum: 3
s: 3
[success] Total time: 1 s, completed
2015/07/31 12:40:28
sampleIntTask := {
ServerUtil.startServer
try {
val sum = 1 + 2
println("sum: " + sum)
sum
} finally {
ServerUtil.stopServer
}
}
28. タスクのスコープ
lazy val say = taskKey[Unit](“say task”)
lazy val message = settingKey[String]("message")
lazy val modifier = taskKey[String]("modifier task")
lazy val display = taskKey[Unit]("display task")
message in say := "hello"
modifier in say := { "{{{" + (message in say).value + "}}}" }
display in say := { println((modifier in say).value) }
$ sbt say::display
[info] Loading project definition from /Users/cw-junichi/temp/sbt-simple/
project
[info] Set current project to sbt-simple (in build file:/Users/cw-junichi/
temp/sbt-simple/)
{{{hello}}}
[success] Total time: 0 s, completed 2015/08/01 7:45:33