1. 如何在启动时执行操作

如何在启动时执行操作 

全局设置 onLoad 的类型为 State => State 且在所有项目构建并加载后仅执行一次。有一个类似的钩子 onUnload 用于在项目卸载时使用。

项目卸载通常是 reload 命令或 set 命令的结果。由于 onLoadonUnload 钩子是全局的,因此修改此设置通常需要使用先前值来组合新的函数。以下示例展示了定义 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
   
}
 
)

您可以使用此技术来切换启动子项目。