如何使任务依赖于另一个任务? [英] How can I make a task depend on another task?

查看:75
本文介绍了如何使任务依赖于另一个任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 sbt 的新手,我尝试为部署我的应用程序或部署运行应用程序创建脚本.

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

对我有用的是

sbt deploy

这将成功将最终的 .jar 文件部署到删除位置.

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

但是,我不知道如何使 deployAndRunTask 依赖于 deployTask.我尝试了几件事,但到目前为止都没有奏效.

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

我最后的希望是

deployAndRunTask := {
  val d = deployTask.value
}

然而,这似乎不起作用.

However, this does not seem to work.

这是我目前使用的脚本,但 sbt deploy-run 只会执行 deployAndRunTask 任务而不是 deyployTask.

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 ..")
}

这里有什么问题?

推荐答案

问题在于你定义了你的任务,然后重新定义它.所以只考虑后一个定义.您不能将任务定义与其对另一个任务的依赖分开.此外,您在 sbt 中使用了一些过时的东西:

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:

  • 使用taskKey宏,你不需要考虑任务名称,因为它和键名相同​​:

  • 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.")

然后你可以在 build.sbt 和 sbt shell

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

:=keyname map { (keyvalue) => 替换 <<<=... } 只需 keyname.value.事情更简洁,更容易写.

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

您可以阅读有关 从 sbt 0.13 迁移的更多信息.x.

这里是您的 deployAndRun 任务定义和这些更改:

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

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

它依赖于 deployassembly 任务,并且会在执行任何其他操作之前运行它们.您也可以使用 dependsOn,但我认为这里没有必要.

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.

您可能也有兴趣查看 定义顺序任务与 Def.sequential使用 Def.taskDyn 定义动态任务.

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

这篇关于如何使任务依赖于另一个任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆