如何添加运行时先运行编译的任务

时间:2015-04-20 19:43:35

标签: sbt

我想先运行一个依赖于编译任务的任务(例如像运行但与另一个主类一起运行)

我看到的示例似乎使用依赖于任务的值。但是我不知道编译的价值是什么,我不明白为什么我在乎。

我认为所需的值是(compile in Compile).value我该怎么做。

例如,在build.sb中有一个hello任务

lazy val hello = taskKey[Unit]("Prints 'Hello World'")

hello := println("hello world!")

我想我已经完成了最后一行

hello := {
  val dummy = (compile in Compile).value
  println("hello world!")
}

这是正确的吗? 如果是这样,编译的价值是什么,我应该用它做什么

1 个答案:

答案 0 :(得分:1)

这绝对是定义依赖关系的最佳方式。

请参阅How to declare task dependency on tasks in 0.13?


另一种选择,也许只是出于学术原因可能是:

mainClass in Compile := Some("org.example.Main1")

val mainClass2 = taskKey[Option[String]]("Defines the alternative main class.")
val run2 = inputKey[Unit]("Runs the alternative main class, passing along arguments provided on the command line.")
run2 <<= Defaults.runTask(fullClasspath in Runtime, mainClass2 in Compile, runner in run)

mainClass2 in Compile := Some("org.example.Main2")

来自sbt shell

> run
[info] Running org.example.Main1
main1
[success] Total time: 0 s, completed 20-Apr-2015 22:59:49
> run2
[info] Running org.example.Main2
main2
[success] Total time: 0 s, completed 20-Apr-2015 22:59:48