2012-04-23 18 views
3

私はもうsbtに関係しないプロジェクトのスターターを作成するタスクを書くことに興味があります。ありがたいことにsbtはすべての情報を知っているので、自分のスターターを作ることができます。対話モードでは、私はこれらの3つの結果を処理したいと思い、それらの4つのコマンドは、私が私のスターターを作成するために必要なすべての情報を表示するが、彼らはちょうどそれらをプリントアウトし、私はさらにタスクでタスクを実行し、その結果をsbtで評価します

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

をそれらを処理することができませんタスクはさらに進んでいますが、私はそれを行う方法がわかりません。 wikiのタスクのdefenitionは非常に混乱しており、< < =オペレータはさらに多くなります。

答えて

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でやっていると嫌い、私はちょうどコピーペーストと文字列の解析を行う、私ははるかに速く、このsbtの駄目を書くよりやっている。 – Arne

+0

@Arne「Runner」には、SBTが実行中のプロセスを中断するなどの処理を行うために使用できる特定のメソッドがあります。その特定のランナーは 'java'プロセスをフォークすることによってコードを実行し、それ以外の場合はデフォルトのランナーとほぼ同じように動作します(つまり、デフォルトランナーに影響を与えるものの影響を受けます)。しかし、あなたの質問を読んで、しかし、これはあなたが望むものではないかもしれません...あなたのプロセスを実行する_script_を作成したいですか?それほどずっと前にメーリングリストで見たと思います。 –

関連する問題