Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

scala - Sbt : multi task ordering

I want to make tasks dependent on each other in sbt. I want task task2 to execute before task3. Here's my code :

Task1.dependsOn(task3)
Task3.dependsOn(task2)

task2 executes before task3 and then task1 is executed. Now If I use this:

Task1.dependsOn(task2, task3)

Does sbt preserve order of tasks? So that execution order is 2 3 1 ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

TL;DR: The answer is no. It does not preserve order. Let's examine this.

I created a simple build.sbt containing:

val task1 = taskKey[Seq[String]]("taks1")
val task2 = taskKey[Seq[String]]("taks2")
val task3 = taskKey[Seq[String]]("taks3")

task1 := {
  println("task1")
  Seq("111")
}

task2 := {
  println("task2")
  Seq("222")
}

task3 := {
  println("task3")
  Seq("333")
}

Then adding:

task1 := task1.dependsOn(task2, task3).value

When running sbt task1 I get consistently the output:

task2
task3
task1

This might cause you to think that the later is the first one to execute. It is only partially true. Let's remember that those actions are asynchronous. Let's add some sleeps and prints. Let's modify task2(which supposed to be printed first):

task2 := {
  println("task2 started")
  Thread.sleep(1000)
  println("task2 finished")
  Seq("222")
}

Then running task1 provide:

task2 started
task3
task2 finished
task1

Which proves that they don't preserve order. If you do want to preserve order, there are many ways to achieve that. For example:

task1 := task1.dependsOn(task3).dependsOn(task2).value

which outputs:

task2 started
task2 finished
task3
task1

Or:

task1 := task1.dependsOn(task3).value
task1 := task1.dependsOn(task2).value

Which outputs the same. Or you can even declare task2 as a dependency of task3:

task1 := task1.dependsOn(task3).value
task3 := task3.dependsOn(task2).value

Which outputs the same. Before taking a decision, you might want to read Task graph and Scope delegation (.value lookup).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...