How to make task depend on another in sbt 0.12?

百般思念 提交于 2019-12-22 04:08:32

问题


I'm using SBT 0.12.0.

I've two tasks in my project/Build.scala - helloTask and u2 defined as follows:

val hello = TaskKey[Unit]("hello", "Prints 'Hello World'")

val helloTask = hello := {
  println("Hello World")
}

val u2Task = TaskKey[Unit]("u2") := { println("u2") }

How to make u2 task depend on hellotask? I used <<= following the sample as described in Tasks (in the original version of the question it was https://github.com/harrah/xsbt/wiki/Tasks, but the doc has since moved and changed).

u2Task <<= u2Task dependsOn helloTask

But I got reassignment to val error. Apparently, I cannot get anything with <<= to work. What am I doing wrong?


回答1:


I don't see you following the sample very closely - this works for me:

  val helloTask = TaskKey[String]("hello")
  val u2Task = TaskKey[Unit]("u2") 

  helloTask := {
    println("Hello World")
    "Hello World"
  }

  u2Task := {println("u2")} 

  u2Task <<= u2Task.dependsOn (helloTask)

The precise reason is that your definition of u2Task has a different type, you can see in the REPL:

scala> val u2Task = TaskKey[Unit]("u2")
u2Task: sbt.TaskKey[Unit] = sbt.TaskKey$$anon$3@101ecc2

scala> val u2Task = TaskKey[Unit]("u2") := {println("u2")}
u2Task: sbt.Project.Setting[sbt.Task[Unit]] = setting(ScopedKey(Scope(This,This,This,This),u2))



回答2:


I got it to work. I misunderstood the <<= and := operators as assignment operators.

  val hello = TaskKey[Unit]("hello", "Prints 'Hello World'")

  val helloTask = hello := {
     println("Hello World")
  }

  val u2 = TaskKey[Unit]("u2", "print u2")
  val u2Task = u2 <<= hello map {_ => println("u2")} 

and the result

> u2
Hello World
u2


来源:https://stackoverflow.com/questions/11964583/how-to-make-task-depend-on-another-in-sbt-0-12

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!