タスクを作成することはあまりありません。私はあなたがそれについてwikiを見て、何かを理解していないと仮定します。私はあなたがこの例のように何かをしたいと思う:
val stringTask = TaskKey[String]("string-task")
stringTask <<= (sampleTask, intTask) map { (sample: Int, intValue: Int) =>
"Sample: " + sample + ", int: " + intValue
}
<<=
方法は、単にstringTask
の定義は他のタスクに依存していることを述べています。
一方、タスクではなくランナーが必要なことがあります。
ランナーを書くことは、残念ながらそれほど単純ではありません。私はthis projectにいる。ここではその定義です:
class MyRunner(subproject: String, config: ForkScalaRun) extends sbt.ScalaRun {
def run(mainClass: String, classpath: Seq[File], options: Seq[String], log: Logger): Option[String] = {
log.info("Running " + subproject + " " + mainClass + " " + options.mkString(" "))
val javaOptions = classpathOption(classpath) ::: mainClass :: options.toList
val strategy = config.outputStrategy getOrElse LoggedOutput(log)
val process = Fork.java.fork(config.javaHome,
config.runJVMOptions ++ javaOptions,
config.workingDirectory,
Map.empty,
config.connectInput,
strategy)
def cancel() = {
log.warn("Run canceled.")
process.destroy()
1
}
val exitCode = try process.exitValue() catch { case e: InterruptedException => cancel() }
processExitCode(exitCode, "runner")
}
private def classpathOption(classpath: Seq[File]) = "-classpath" :: Path.makeString(classpath) :: Nil
private def processExitCode(exitCode: Int, label: String) = {
if(exitCode == 0) None
else Some("Nonzero exit code returned from " + label + ": " + exitCode)
}
}
そして、それは次のように使用されます:こんなに早く
runner in Compile in run <<= (thisProject, taskTemporaryDirectory, scalaInstance, baseDirectory, javaOptions, outputStrategy, javaHome, connectInput) map {
(tp, tmp, si, base, options, strategy, javaHomeDir, connectIn) =>
new MyRunner(tp.id, ForkOptions(scalaJars = si.jars, javaHome = javaHomeDir, connectInput = connectIn, outputStrategy = strategy,
runJVMOptions = options, workingDirectory = Some(base)))
}
答えに感謝していますが、ランナーのタスク間のすべてのそれらの違いなどが何であるかを教えてでしょうか?今私はsbtでやっていると嫌い、私はちょうどコピーペーストと文字列の解析を行う、私ははるかに速く、このsbtの駄目を書くよりやっている。 – Arne
@Arne「Runner」には、SBTが実行中のプロセスを中断するなどの処理を行うために使用できる特定のメソッドがあります。その特定のランナーは 'java'プロセスをフォークすることによってコードを実行し、それ以外の場合はデフォルトのランナーとほぼ同じように動作します(つまり、デフォルトランナーに影響を与えるものの影響を受けます)。しかし、あなたの質問を読んで、しかし、これはあなたが望むものではないかもしれません...あなたのプロセスを実行する_script_を作成したいですか?それほどずっと前にメーリングリストで見たと思います。 –