Scala sbtのプロジェクト
ビルドツール sbt 0.12のプロジェクトについてのメモ
sbtは、インストール済みとする。
Getting Started Hello · harrah/xsbt Wiki · GitHub
もちろんJavaもインストール済み。Windowsで、javaコマンドが実行できないときは、以下のような設定で。
mklink /d C:\jdk "C:\Program Files\Java\jdk1.7.0_07" set JAVA_HOME=C:\jdk set PATH=%JAVA_HOME%\bin;%PATH%
Scalaについては,sbtがインストールしてくれるのでだいじょうぶ。
自分は、こっちでsbtをインストールしてちょっと使ってた
Scalaのビルドツールsbtを使ってみる - kaishitaeiichiの日記
プロジェクトの作成と設定
まず、sbtのプロジェクトを置くディレクトリをつくっておこう
mkdir sbt-projects cd sbt-projects
プロジェクトのディレクトリ作成
mkdir helloworld mkdir helloworld\src\main\scala mkdir helloworld\src\main\java mkdir helloworld\src\main\resources mkdir helloworld\src\test\scala mkdir helloworld\src\test\java mkdir helloworld\src\test\resources
設定ファイルbuild.sbt
プロジェクトの設定を記述する。実際は、もっと細かく設定項目があったり、必要なら、Build.scalaなんてファイルも書くらしい。詳細調べられてないので、ここではデフォルトのまま。
アプリが依存するライブラリもここへ記述する。Mavenのリポジトリを利用するときの形式になる。
helloworld\build.sbt
helloworld\project\Build.scalaは、今回書いてない
name := "helloworld" version := "1.0" scalaVersion := "2.9.2" scalacOptions ++= Seq("-encoding", "UTF-8") libraryDependencies ++= Seq( "org.apache.commons" % "commons-lang3" % "3.1", "commons-io" % "commons-io" % "2.4" )
cd helloworld sbt
sbtのコンソールを起動したら、
> name [info] helloworld > version [info] 1.0 > scala-version [info] 2.9.2 > scalac-options [success] Total time: 0 s, completed 2012/10/24 22:17:05 > library-dependencies [info] List(org.scala-lang:scala-library:2.9.2, org.apache.commons:commons-lang3:3.1, commons-io:commons-io:2.4) >
build.sbtの設定が表示されるのがわかる。
ソースコードの作成とアプリの実行
ソースコードの作成
src配下におく。
今回は簡単に、作業ディレクトリであるプロジェクトの直下、においている。
以下のソースコードは、ScalaじゃなくてJavaのライブラリをimportというのがなんだけど、依存ライブラリも使ってみた。
helloworld\examples.scala
import org.apache.commons.lang3.StringUtils import org.apache.commons.io.FileUtils import java.io.File object Examples { def main(args: Array[String]) = { println(StringUtils.substring("abcd", 0, 2)) println(FileUtils.readFileToString(new File("examples.scala"), "UTF-8")) } }
アプリの実行
sbtのコンソールを起動したら
> run [info] Compiling 1 Scala source to F:\Data\Scala\sbt-projects\helloworld\target\scala-2.9.2\classes... [info] Running Examples ab import org.apache.commons.lang3.StringUtils import org.apache.commons.io.FileUtils import java.io.File object Examples { def main(args: Array[String]) = { println(StringUtils.substring("abcd", 0, 2)) println(FileUtils.readFileToString(new File("examples.scala"), "UTF-8")) } } [success] Total time: 2 s, completed 2012/10/20 1:55:21 >
jarの作成
jarを作成するには、package
> package [info] Updating {file:/F:/Data/Scala/sbt-projects/helloworld/}default-d50d4b... [info] Resolving org.scala-lang#scala-library;2.9.2 ... [info] Resolving org.apache.commons#commons-lang3;3.1 ... [info] Resolving commons-io#commons-io;2.4 ... [info] Done updating. [info] Compiling 1 Scala source to F:\Data\Scala\sbt-projects\helloworld\target\scala-2.9.2\classes... [info] Packaging F:\Data\Scala\sbt-projects\helloworld\target\scala-2.9.2\helloworld_2.9.2-1.0.jar ... [info] Done packaging. [success] Total time: 1 s, completed 2012/10/20 2:31:57 >
helloworld\target\scala-2.9.2\helloworld_2.9.2-1.0.jar
が作成された
sbtのタスク
sbtを起動中とする。tasksと入力すると一覧がでる。
> tasks This is a list of tasks defined for the current project. It does not list the scopes the tasks are defined in; use the 'inspect' command for that. Tasks produce values. Use the 'show' command to run the task and print the resulting value. clean Deletes files produced by the build, such as generated sources, compiled classes, and task caches. compile Compiles sources. console Starts the Scala interpreter with the project classes on the classpath. console-project Starts the Scala interpreter with the sbt and the build definition on the classpath and useful imports. console-quick Starts the Scala interpreter with the project dependencies on the classpath. copy-resources Copies resources to the output directory. doc Generates API documentation. package Produces the main artifact, such as a binary jar. This is typically an alias for the task that actually does the packaging. package-bin Produces a main artifact, such as a binary jar. package-doc Produces a documentation artifact, such as a jar containing API documentation. package-src Produces a source artifact, such as a jar containing sources and resources. publish Publishes artifacts to a repository. publish-local Publishes artifacts to the local repository. run Runs a main class, passing along arguments provided on the command line. run-main Runs the main class selected by the first argument, passing the remaining arguments to the main method. test Executes all tests. test-only Executes the tests provided as arguments or all tests if no arguments are provided. test-quick Executes the tests that either failed before, were not run or whose transitive dependencies changed, among those provided as arguments. update Resolves and optionally retrieves dependencies, producing a report. More tasks may be viewed by increasing verbosity. See 'help tasks'. >
この一覧以外にも
Keys.scala
に定義してあるものがいろいろ使えるようだ。
tasksに、オプションをつけると、より多くのtaskが一覧に表示される。
tasks -v .... tasks -V ....
独自タスクの作成は、こちら
Scala sbt 独自のTaskを定義してみる - kaishitaeiichiの日記
console
ScalaのREPLを起動できる。REPLから抜けるときは、:quit
> console [info] Starting scala interpreter... [info] Welcome to Scala version 2.9.2 (Java HotSpot(TM) Client VM, Java 1.7.0_02). Type in expressions to have them evaluated. Type :help for more information. scala> val a = List(1,2,3,4) a: List[Int] = List(1, 2, 3, 4) scala> :quit [success] Total time: 25 s, completed 2012/10/20 14:56:52 >