Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call another task from my SBT task?

Tags:

scala

task

sbt

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?

like image 840
Maurício Linhares Avatar asked Nov 21 '13 01:11

Maurício Linhares


1 Answers

General answer:

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

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.

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

Specific answer:

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.

like image 162
Seth Tisue Avatar answered Sep 27 '22 21:09

Seth Tisue