全域設定 onLoad
的類型為 State => State
,它會在所有專案建置和載入完成後執行一次。有一個類似的鉤子 onUnload
,用於在專案卸載時執行。
專案卸載通常發生在 reload
命令或 set
命令之後。由於 onLoad
和 onUnload
鉤子是全域的,修改此設定通常涉及將新函式與先前的值組合。以下範例顯示定義 onLoad
的基本結構。
假設您想在啟動時執行名為 dependencyUpdates
的任務。您可以這樣做
lazy val dependencyUpdates = taskKey[Unit]("foo")
// This prepends the String you would type into the shell
lazy val startupTransition: State => State = { s: State =>
"dependencyUpdates" :: s
}
lazy val root = (project in file("."))
.settings(
ThisBuild / scalaVersion := "2.12.6",
ThisBuild / organization := "com.example",
name := "helloworld",
dependencyUpdates := { println("hi") },
// onLoad is scoped to Global because there's only one.
Global / onLoad := {
val old = (Global / onLoad).value
// compose the new transition on top of the existing one
// in case your plugins are using this hook.
startupTransition compose old
}
)
您也可以使用此技術來切換啟動子專案。