2012-04-23 144 views
3

我有兴趣编写一个任务,创建我的项目的启动程序,该程序完全不再涉及sbt。谢天谢地,我知道所有的信息,所以我可以创建自己的首发。在交互模式这四个命令告诉我,我需要创建我首发的所有信息,但他们只是把它们打印出来,我无法进一步处理他们在任务中运行任务并评估其结果sbt

show java-options 
show unmanaged-jars 
show managed-classpath 

我想处理这三个结果任务进一步,但是我知道我不知道该怎么做。维基中的任务定义非常混乱,并且运算符甚至更多。

回答

2

创建任务并不多。我假设你看了看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))) 
} 
+0

感谢您的回答,但你可能会告诉我什么任务运动员之间等等所有这些差异是什么?就在此刻,我已经完成了并且讨厌它,我只是复制了粘贴和字符串解析,但我做得比编写这个sbt废话快得多。 – Arne 2012-04-23 21:18:45

+0

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