sbt+ScalaTestと戯れる(その1:sbtではろーわーるど)

次回の天領倉敷Scalaのネタとして、sbt+ScalaTestのネタをやろうかなー(個人的に手を出してみたかった)と思いまして、その辺の触り部分の手順など。

sbtって何よ?

Scala製のビルドツール(simple-build-tool)です。

じゃあScalaTestは?

その名前の通り、Scala製のテスティングフレームワークです。
何か前はテストツールといえばSpecsのイメージがあったんですが、どうも最近はScalaTestがいい的な話をどこかで見かけたので今回はこっちで。

sbtのセットアップ

sbtのダウンロード

http://code.google.com/p/simple-build-tool/downloads/list よりダウンロードします。
今回は、現時点でFeaturedの0.7.4にしました。
好きなところに放りこんでください。

起動用スクリプトの作成

下記スクリプトをsbtのjarと同一階層につくります。実行権限は適宜つけてください。
sbt(Unixの場合)

#! /bin/sh
java -Xmx512M -jar `dirname $0`/sbt-launch-0.7.4.jar "$@"
# proxyを使う場合は下記記述が必要
java -Dhttp.proxyUser=username -Dhttp.proxyPassword=mypassword -Xmx512M -jar `dirname $0`/sbt-launch.jar "$@"

sbt.bat(Windowsの場合)

set SCRIPT_DIR=%~dp0
java -Xmx512M -jar "%SCRIPT_DIR%sbt-launch.jar" %*
rem proxyを使う場合は下記記述が必要
java -Dhttp.proxyHost=myproxy -Dhttp.proxyPort=8080 -Dhttp.proxyUser=username -Dhttp.proxyPassword=mypassword -Xmx512M -jar "%SCRIPT_DIR%sbt-launch.jar" %*

あとはパスを通せば出来上がりです。

sbtの起動

コマンドラインでsbtを実行します。

> sbt
Project does not exist, create new project? (y/N/s) 
プロジェクトの作成

"y"を選択すると、対話形式でプロジェクトを作成します。

Project does not exist, create new project? (y/N/s) y
Name: sbttest  
Organization: razon
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/517ms)
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/344ms)
[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/526ms)
[info] Building project sbttest 1.0 against Scala 2.8.1
[info]    using sbt.DefaultProject with sbt 0.7.4 and Scala 2.7.7
> 

"s"を選択すると、ブランクプロジェクトを生成します。

Project does not exist, create new project? (y/N/s) s
Getting Scala 2.7.7 ...
:: retrieving :: org.scala-tools.sbt#boot-scala
	confs: [default]
	2 artifacts copied, 0 already retrieved (9911kB/42ms)
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/33ms)
[success] Successfully initialized directory structure.
[info] Building project test 1.0 against Scala 2.7.7
[info]    using sbt.DefaultProject with sbt 0.7.4 and Scala 2.7.7
> 

sbtコンソールが立ち上がりました。


"s"でプロジェクトを作成した場合、Scalaのバージョンは2.7.7になります。
別バージョンを使いたい場合は、sbtコンソールで下記コマンドを実行します。

> set build.scala.versions 2.8.1
> reload

まずはHelloWorld

ベタベタなはろーわーるどをゴリゴリ書きます。
src/main/scala/Hello.scala

object Hello { 
  def main(args: Array[String]) = { 
    println("Hello, World!")
  }
}

コンパイル

> compile
[info] 
[info] == compile ==
[info]   Source analysis: 1 new/modified, 0 indirectly invalidated, 0 removed.
[info] Compiling main sources...
[info] Compilation successful.
[info]   Post-analysis: 2 classes.
[info] == compile ==
[success] Successful.
[info] 
[info] Total time: 6 s, completed 2011/01/19 1:05:54
> 

実行

> run
[info] 
[info] == copy-resources ==
[info] == copy-resources ==
[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: 2 classes.
[info] == compile ==
[info] 
[info] == run ==
[info] Running Hello 
Hello, World!
[info] == run ==
[success] Successful.
[info] 
[info] Total time: 0 s, completed 2011/01/19 1:06:45
> 

はろーしました。

ちょっと手直し

はろーわーるどのソースをちょっと直します。

object Hello { 
  def main(args: Array[String]) = { 
    println("Hello, Scala!")
  }
}

再度コンパイル - 実行

> compile
[info] 
[info] == compile ==
[info]   Source analysis: 1 new/modified, 0 indirectly invalidated, 0 removed.
[info] Compiling main sources...
[info] Compilation successful.
[info]   Post-analysis: 2 classes.
[info] == compile ==
[success] Successful.
[info] 
[info] Total time: 2 s, completed 2011/01/19 1:09:06
> run 
[info] 
[info] == copy-resources ==
[info] == copy-resources ==
[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: 2 classes.
[info] == compile ==
[info] 
[info] == run ==
[info] Running Hello 
Hello, Scala!
[info] == run ==
[success] Successful.
[info] 
[info] Total time: 0 s, completed 2011/01/19 1:09:12
> 

無事変わりました。