Scala sbt 独自のTaskを定義してみる
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 ....
Antを思い出すと、mkdirやcopyなどのTaskをtargetという単位で記述して、いろいろ細かい処理を実行できた。
Mavenは、プラグインがあればいいけど、なければだめで、プラグインには、Antを実行するプラグインもあった。そこが便利さとトレードオフ。
sbtのTaskは、どちらかといったら、AntのTaskまたはtargetに近いものものなんだろうか。
ここでは、試しに、sbtの独自のTaskを定義してみる。
Tasks · harrah/xsbt Wiki · GitHub
This page has moved
再利用性を考えるとプラグインをつくったほうがよいらしいのだけど、お手軽にお試しということで。
プロジェクトの作成とBuild.scalaの準備
F:\Data\Scala\sbt-projects>mkdir MyTaskExample F:\Data\Scala\sbt-projects>mkdir MyTaskExample\project
ここで、build.sbtではなくて、Build.scalaファイルを用意する。Build.scalaファイルにカスタムTaskを記述する。
MyTaskExample\project\Build.scala
import sbt._ import Keys._ import java.io.File object HelloWorldBuild extends Build { // http://www.scala-sbt.org/release/docs/Detailed-Topics/Tasks.html val buildSettings = Defaults.defaultSettings ++ Seq( name := "helloworld", version := "1.0", scalaVersion := "2.9.2" ) val settingsScalacOptions = scalacOptions ++= Seq("-encoding", "UTF-8") //今回これは特に意味なし。 val settingsLib = libraryDependencies ++= Seq( "org.apache.commons" % "commons-lang3" % "3.1", "commons-io" % "commons-io" % "2.4" ) object Tasks { //make-maven-like-dirsが //sbtのコンソールから実行できるタスク名になる val makeMavenLikeDirs = TaskKey[Unit]("make-maven-like-dirs", "make directories like maven") val makeMavenLikeDirsTask = makeMavenLikeDirs := { //ここのIOというのは、sbtのAPIに含まれれる //ファイル操作のAPI IO.createDirectories(Seq( new File("src/main/java"), new File("src/main/scala"), new File("src/main/resources"), new File("src/test/java"), new File("src/test/scala"), new File("src/test/resources") )) } val settingsTasks = Seq(makeMavenLikeDirsTask) } lazy val project = Project( "project", file("."), settings = buildSettings ++ Seq(settingsScalacOptions, settingsLib) ++ Tasks.settingsTasks ) }
独自タスクの確認
Build.scalaを読み込みなおすと、自分がBuild.scalaに記述したtaskが追加されているのがわかる。
reload .... tasks -v .... make-maven-like-dirs make directories like maven ....
独自タスクの実行
準備ができたら、プロジェクトのディレクトリに移動して、sbt起動する。上で記述したカスタムTaskは、make-maven-like-dirsで実行できる。
F:\Data\Scala\sbt-projects>cd MyTaskExample F:\Data\Scala\sbt-projects\MyTaskExample>sbt .... > make-maven-like-dirs [success] Total time: 0 s, completed 2012/10/29 0:29:35 > exit F:\Data\Scala\sbt-projects\MyTaskExample>tree フォルダ パスの一覧 ボリューム シリアル番号は 781F-C3ED です F:. .... ├─src │ ├─main │ │ ├─java │ │ ├─resources │ │ └─scala │ └─test │ ├─java │ ├─resources │ └─scala ....
srcディレクトリ配下に、MavenでJavaのプロジェクトを作成したときのようなディレクトリにができているのがわかる。