Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make task depend on another in sbt 0.12?

Tags:

sbt

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?

like image 437
Tg. Avatar asked Aug 15 '12 05:08

Tg.


2 Answers

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
like image 30
Tg. Avatar answered Oct 14 '22 07:10

Tg.


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))
like image 172
themel Avatar answered Oct 14 '22 07:10

themel