创建任务并不多。我假设你看了看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废话快得多。 – Arne 2012-04-23 21:18:45
@Arne一个'Runner'具有特定的方法,SBT可以用来做一些事情,比如中断正在运行的进程。该特定运行程序通过分叉java进程来执行代码,否则就像缺省运行程序一样(即会受到影响缺省运行程序的内容的影响)。但是,重新阅读你的问题,也许这不是你想要的......你想创建一个能运行你的进程的_script_吗?我想我不久前在邮件列表上看到了这一点。 – 2012-04-23 21:27:38