CLOVER🍀

That was when it all began.

sbt 0.10で依存関係の管理

続いて、Mavenの時と同様、sbtで依存関係の管理をしてみましょう。今回も、スケープゴートになっていただくのは、Jakarta Commons Langでございます。

では、ちょっとクラスを修正。

$ cat src/main/scala/HelloSbt.scala 
import org.apache.commons.lang.StringUtils

object HelloSbt {
  def main(args: Array[String]): Unit = {
    val message = "Hello %replace%!"

    val target = args.headOption.getOrElse("Commons")

    println(StringUtils.replace(message, "%replace%", target))
  }
}

そして、sbtでコンパイル

> compile
[info] Compiling 1 Scala source to /xxxxx/hello/target/scala-2.9.0.final/classes...
[error] /xxxxx/hello/src/main/scala/HelloSbt.scala:1: object apache is not a member of package org
[error] import org.apache.commons.lang.StringUtils
[error]            ^
[error] one error found
[error] Incomplete tasks (run 'last <task>' for the full log):
[error]   {file:/xxxxx/hello/}default/compile: Compilation failed
[error] Total time: 1 s, completed Jul 10, 2011 7:13:19 PM

当然ですが、失敗します。

それでは、Setupに従ってbuild.sbtを修正。

$ cat build.sbt 
name := "Hello"

version := "1.0"

scalaVersion := "2.9.0"

libraryDependencies += "commons-lang" % "commons-lang" % "2.6"

build.sbtを修正したので、reloadしてコンパイル、実行。

> reload
[info] Set current project to default (in build file:/xxxxx/hello/)
> compile
[info] Updating...
[info] Done updating.
[info] Compiling 1 Scala source to /xxxxx/hello/target/scala-2.9.0.final/classes...
[success] Total time: 2 s, completed Jul 10, 2011 7:19:56 PM
> run
[info] Running HelloSbt 
Hello Commons!
[success] Total time: 0 s, completed Jul 10, 2011 7:19:57 PM

今度はうまくいきました。

なお、引数を与えたい場合はrunの後に付けるとよいですよ。

> run World
[info] Running HelloSbt World
Hello World!
[success] Total time: 0 s, completed Jul 10, 2011 7:21:07 PM