Run a task in a task and evaluate its result in sbt

I am interested in writing a task that creates the starter of my project, which is no longer associated with sbt. Fortunately, sbt knows all the information so that I can create my own starter. In interactive mode, these four commands show me all the information I need to create a starter, but they just print them, and I can’t process them further

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

I would like to process the result of these three tasks further, but I know correctly that I do not know how to do this. Defragmenting a task on a wiki is very confusing, and the <= = operator is even bigger.

+3
source share
1 answer

. , 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)) )
}
+2

All Articles