How can I make a task depend on another task?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 03:55:53

问题


I'm new to sbt and I try to create a script for either deploy my application or to deploy and run the application.

What already works for me is

sbt deploy

which will successfully deploy the final .jar file to the remove location.

However, I don't know how to make deployAndRunTask dependent on deployTask. I've tried several things but none of them worked so far.

My last hope was

deployAndRunTask := {
  val d = deployTask.value
}

However, this does not seem to work.

This is the script that I'm currently at but sbt deploy-run will only execute the deployAndRunTask task but not the deyployTask.

// DEPLOYMENT

val deployTask = TaskKey[Unit]("deploy", "Copies assembly jar to remote location")

deployTask <<= assembly map { (asm) =>
  val account = "user@example.com" 
  val local = asm.getPath
  val remote = account + ":" + "/home/user/" + asm.getName
  println(s"Copying: $local -> $account:$remote")
  Seq("scp", local, remote) !!
}

val deployAndRunTask = TaskKey[Unit]("deploy-run", "Deploy and run application.")

deployAndRunTask := {
  val d = deployTask.value
}

deployAndRunTask <<= assembly map { (asm) =>
  println(s"Running the script ..")
}

What is the problem here?


回答1:


The problem is that you define your task and then redefine it. So only the latter definition is taken into account. You cannot separate task definition and its dependency on another task. Also you're using a couple of outdated things in sbt:

  • use taskKey macro and you don't need to think about task name, because it's the same as the key name:

    val deploy = taskKey[Unit]("Copies assembly jar to remote location")
    val deployAndRun = taskKey[Unit]("Deploy and run application.")
    

    Then you can refer to them as deploy and deployAndRun both in build.sbt and in the sbt shell

  • replace <<= with := and keyname map { (keyvalue) => ... } with just keyname.value. Things are more concise and easier to write.

You can read more about Migrating from sbt 0.13.x.

So here's your deployAndRun task definition with these changes:

deployAndRun := {
  val d = deploy.value
  val asm = assembly.value
  println(s"Running the script ..")
}

It's dependent both on deploy and assembly tasks and will run them both before doing anything else. You can also use dependsOn, but I think it's unnecessary here.

You may also be interested in looking into Defining a sequential task with Def.sequential and Defining a dynamic task with Def.taskDyn.



来源:https://stackoverflow.com/questions/47872758/how-can-i-make-a-task-depend-on-another-task

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