1

我尝试了我能想到的所有方法,但运行 taskB 仍然会出现错误消息,即如果我将它放在自定义配置中,*/*:taskB或者在其中未定义任务。*/cmd:taskB

Command.command("doStuff", Help.more("doStuff", "whatever")) {
      (state: State) =>
        val e = Project.extract(state)

        val taskA = taskKey[Seq[String]]("A")
        val taskB = taskKey[Seq[File]]("B")

        val cmdConfig = config("cmd")

        val newState = e.append(
          inConfig(cmdConfig)(Seq(
              taskA := {
                // do stuff
              },
              taskB := {
                // do stuff
              }
            )
          )
          , state
        )

        val result: (State, Seq[File]) = e.runTask(taskB in cmdConfig, newState)
)

调试它时,似乎该任务不存在于structure.data搜索它的位置。

4

1 回答 1

2

首先,我假设您只想从命令中调用任务,并且您并不真正关心是否通过修改该命令中的状态来添加它们。

如果是这样,我会以更标准的方式来做,就像 AutoPlugins 所做的那样。

import sbt._
import Keys._

object MyPlugin extends AutoPlugin {

  object autoImport {
    val taskA = taskKey[Seq[String]]("Task A")
    val taskB = taskKey[Seq[File]]("Task B")
  }

  import autoImport._

  val cmdConfig = config("cmd")

  override def projectConfigurations = Seq(cmdConfig)

  // this is optional of course, you can also enable plugin manually
  override def trigger = allRequirements

  override def projectSettings = 
    Seq(commands += doStuffCommand) ++
    inConfig(cmdConfig)(Seq(
      taskA := {
        println("TASK A")
        Seq("A", "B")
      },
      taskB := {
        println("TASK B")
        Seq(file("."))
      }
    ))


  lazy val doStuffCommand =
   Command.command("doStuff", Help.more("doStuff", "whatever")) {
     (state: State) =>
     val e = Project.extract(state)
     val (newState, bResult) = e.runTask(taskB in cmdConfig, state)
     newState
   }

}

此外,也许你根本不需要命令,只是让一些任务调用另一个任务会更简单。

于 2014-08-15T06:49:41.280 回答