我有兴趣编写一个任务来创建我的项目的启动器,该项目不再涉及 sbt。幸好 sbt 知道所有信息,所以我可以创建自己的启动器。在交互模式下,这四个命令向我显示了创建启动器所需的所有信息,但它们只是将它们打印出来,我无法进一步处理它们
show java-options
show unmanaged-jars
show managed-classpath
我想进一步处理这三个任务的结果,但我知道我不知道该怎么做。wiki 中的任务定义非常混乱,<<= 运算符更是如此。
创建任务并不多。我假设您查看了有关它的wiki,但没有理解某些内容。我想你会做这样的例子:
val stringTask = TaskKey[String]("string-task")
stringTask <<= (sampleTask, intTask) map { (sample: Int, intValue: Int) =>
"Sample: " + sample + ", int: " + intValue
}
该<<=
方法简单地说,定义stringTask
取决于其他任务。
另一方面,也许你需要一个 Runner,而不是一个 Task。
不幸的是,编写一个跑步者并不是那么简单。我在这个项目上。这是它的定义:
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)) )
}