seratch's weblog in Japanese

About Scala, Java and Ruby programming in Japaense. If you need English information, go to http://blog.seratch.net/

sbt 0.7.xの使い方

手順

http://code.google.com/p/simple-build-tool/downloads/list
sbt-launch-X.X.X.jarをダウンロードして「sbt-launch.jar」にリネームしておきます。
(2011/6追記)この記事の投稿時点では0.7.4が最新でしたが、2011/6現在で0.7.x系の最新版は0.7.7なのでそちらをおすすめします。

jarファイルと同じディレクトリにUnix/Mac OSの場合は「sbt」として

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

Windowsの場合は「sbt.cmd」として

@echo off
java -Xmx512M -jar "%~dp0/sbt-launch.jar" %*

のようなファイルを作り、実行権限の付与とPATH設定を行います。

プロジェクトのルートディレクトリでsbtを起動します。初回起動時はプロジェクト設定をきかれるので指定します。

$ sbt

Project does not exist, create new project? (y/N/s) y
Name: scalahatenadiary
Organization: seratch2
Version [1.0]:
Scala version [2.7.7]: 2.8.1
sbt version [0.7.4]:
Getting Scala 2.7.7 ...
:: retrieving :: org.scala-tools.sbt#boot-scala
        confs: [default]
        2 artifacts copied, 0 already retrieved (9911kB/375ms)
Getting org.scala-tools.sbt sbt_2.7.7 0.7.4 ...
:: retrieving :: org.scala-tools.sbt#boot-app
        confs: [default]
        15 artifacts copied, 0 already retrieved (4096kB/172ms)
[success] Successfully initialized directory structure.
Getting Scala 2.8.1 ...
:: retrieving :: org.scala-tools.sbt#boot-scala
        confs: [default]
        2 artifacts copied, 0 already retrieved (15118kB/563ms)
[info] Building project scalahatenadiary 1.0 against Scala 2.8.1
[info]    using sbt.DefaultProject with sbt 0.7.4 and Scala 2.7.7
> compile
[info]
[info] == compile ==
[info]   Source analysis: 0 new/modified, 0 indirectly invalidated, 0 removed.
[info] Compiling main sources...
[info] Nothing to compile.
[info]   Post-analysis: 0 classes.
[info] == compile ==
[success] Successful.
[info]
[info] Total time: 0 s, completed 2011/02/16 11:24:34
> quit
[info]
[info] Total session time: 16 s, completed 2011/02/16 11:24:38
[success] Build completed successfully.

$

上記の設定は「./project/build.properties」に保存されます。

$ more ./project/build.properties
#Project properties
#Wed Feb 16 12:32:35 JST 2011
project.organization=seratch2
project.name=scalahatenadiary
sbt.version=0.7.4
project.version=1.0
build.scala.versions=2.8.1
project.initialize=false

sbtチートシート

独断と偏見でよく使いそうなものをピックアップしました。

  • 対話シェルを起動する

「!:」でヒストリ表示します。「!!」で最後に実行したコマンド、「!(数字)」で指定されたコマンドを再実行します(例:!2でsbt update)。

$ sbt
[info] Building project scalahatenadiary 1.0 against Scala 2.8.1
[info]    using sbt.DefaultProject with sbt 0.7.4 and Scala 2.7.7
> !:
   0  test
   1  compile
   2  update

これより下にあるコマンドは対話シェルを起動した状態ではsbtなしで実行できます。

  • targetディレクトリをクリアする
sbt clean
sbt clean-cache
  • 依存関係を最新のpom.xmlで更新する
sbt update
sbt compile
  • プロジェクトのテストコードをコンパイルする
sbt test-compile
  • 実行する

mvn scala:runと同じです。プロジェクトのソースコードをコンパイルしてmain関数を実行します。

sbt run

複数ある場合は選択になります。

Multiple main classes detected, select one to run:

 [1] scalahatenadiary.App
 [2] Hi

Enter number: 1

[info] Running scalahatenadiary.App
Hello World!
[info] == run ==
[success] Successful.
[info]
[info] Total time: 21 s, completed 2011/02/16 15:02:34
[info]
[info] Total session time: 21 s, completed 2011/02/16 15:02:34
[success] Build completed successfully.
  • テストを全実行する

テストを全実行します。JUnitベースのテストはskipされます。JUnitテストはmvn testで実行することができます。

sbt test
  • ファイルを変更したら自動的にコンパイルする

常駐プロセスとして起動し、ソースコードのファイルに変更があれば適宜コンパイルし直します。

sbt ~compile
  • ファイルを変更したら自動的にコンパイルしテストも全部流す

常駐プロセスとして起動し、ソースコードのファイルに変更があれば適宜コンパイルし直してテストを全実行します。

sbt ~test
  • scaladocを生成する
sbt doc
  • コンパイルしてjarファイルに固める

src/main/scala配下の*.scalaをコンパイルしたclassファイルとsrc/main/resources配下のファイルのコピーをまとめてjarファイルにします。
jarファイルのファイル名はproject/build.propertiesを元に「(project.name)_(build.scala.versions)-(project.version).jar」となります。(例:scalahatenadiary_2.8.1-1.0.jar)
mvn packageの場合は*.scalaだけでなく*.javaもコンパイルされます*1

sbt package
  • 依存ライブラリのclasspathが解決された状態でREPLを起動する

sbtの対話シェルを起動している状態からでも起動できます。

sbt console

見えているclass、objectなどであればtab補完がききます。

[info] == console ==
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Client VM, Java 1.6.0_23).
Type in expressions to have them evaluated.
Type :help for more information.

scala> scalahatenadiary.

App            AppTest        MySpec         MySpecRunner   MySpecTest

scala> scalahatenadiary.MySpec.main(null)
Specification "MySpec"
  This wonderful system should
  + save the world

Total for specification "MySpec":
Finished in 0 second, 234 ms
1 example, 1 expectation, 0 failure, 0 error

[info] == console ==
[success] Successful.
[info]
[info] Total time: 36 s, completed 2011/02/16 15:33:50
>

その他の詳細については、以下のドキュメントを参照して下さい。
http://code.google.com/p/simple-build-tool/wiki/DocumentationHome

*1:こちらのファイル名は当然mvnのルールです