到目前為止,我們主要探討的是任務。還有一種稱為輸入任務的任務,它可以接受來自 shell 的使用者輸入。一個典型的例子是 Compile / run
任務。 scalastyle
任務實際上也是一個輸入任務。請參閱 輸入任務 以了解輸入任務的詳細資訊。
現在假設我們想要呼叫 Compile / run
任務,然後開啟瀏覽器進行測試。
object Greeting {
def main(args: Array[String]): Unit = {
println("hello " + args.toList)
}
}
lazy val runopen = inputKey[Unit]("run and then open the browser")
lazy val root = (project in file("."))
.settings(
runopen := {
(Compile / run).evaluated
println("open browser!")
}
)
在這裡,我使用 println
作為副作用來偽造瀏覽器的開啟。我們現在可以從 shell 呼叫此任務
> runopen foo
[info] Compiling 1 Scala source to /x/proj/...
[info] Running Greeting foo
hello List(foo)
open browser!
我們實際上可以移除 runopen
鍵,方法是將新的輸入任務重寫為 Compile / run
lazy val root = (project in file("."))
.settings(
Compile / run := {
(Compile / run).evaluated
println("open browser!")
}
)