如何从我的 SBT 任务中调用另一个任务? [英] How can I call another task from my SBT task?

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

问题描述

我正在尝试在我的任务中调用 runTask 并认为这会起作用:

I'm trying to call the runTask inside of my task and considered this would work:

name := "hello"

version := "1.0"

scalaVersion := "2.10.2"

lazy val hello = taskKey[Unit]("executes hey")

lazy val helloTask = hello <<= runTask(fullClasspath, "sample.Hey" in run, runner in run)

但是,好吧,它没有.关于我如何做到这一点的任何想法?

But, well, it doesn't. Any ideas on how I could do this?

推荐答案

一般答案:

要回答您的一般性问题,解决方案是让您的任务依赖于其他任务.直接调用任务会导致依赖系统、并行执行系统等的结束.你依赖并调用这样的任务(使用 0.13 风格的语法):

To answer your general question, the solution is to make your task depend on the other task. Invoking the task directly would do an end run around the dependency system, the parallel execution system, etc. You depend on, and invoke, the task like this (in 0.13-style syntax):

myTask := {
  ...
  val result = otherTask.value
  ...
}

注意otherTask 会在myTask 开始之前被调用,而不是在myTask 主体中出现依赖的地方;因为这就是依赖项的工作方式.

Note that otherTask will be invoked before myTask begins, rather than at the point in the body of myTask where the dependency appears; because that's how dependencies work.

如果无论出于何种原因,您发现以正常"方式执行此操作不合适或不可接受,请考虑 sbt 中的良好风格是将任务的声明与其实现分开.典型的任务实现只是简单地编组参数,然后调用实际完成工作的方法.如果您要调用的任务以这种方式实现,则回答我如何调用任务 T?"是不要;调用 T 调用的相同代码."

If for whatever reason you find doing it the "normal" way inappropriate or unacceptable, consider that good style in sbt is to separate the declaration of a task from its implementation. A typical task implementation simply marshals arguments and then calls a method that actually does the work. If the task you want to call is implemented that way, then an answer to "How do I call task T?" is "Don't; call the same code T calls."

具体答案:

但是从您的示例来看,在我看来,您实际上要解决的问题是除了运行之外,我还如何创建自定义运行任务?"这个问题在 sbt FAQ 中得到了回答;见 http://www.scala-sbt.org/0.13.0/docs/faq.html.答案是使用方便的方法 fullRunTaskfullRunInputTask.

But from your example, it looks to me like the problem you're actually trying to solve is "How can I create a custom run task, in addition to run?" This question is answered in the sbt FAQ; see http://www.scala-sbt.org/0.13.0/docs/faq.html. The answer is to use the convenience methods fullRunTask and fullRunInputTask.

顺便说一句,如果您查看这些方法的源代码,您会发现它们不会创建一个调用另一个任务的任务;相反,他们采用调用相同代码"的方法.

Incidentally, if you look at the source code for those methods, you'll see that they don't make a task that invokes another task; rather, they take the "call the same code" approach.

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

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