我有以下最小的自定义sbt插件,我想在其中实现(覆盖)标准clean
命令以执行与myTask
相同的操作。
package nigeleke.sbt
import sbt.*
import Keys.*
import scala.sys.process.*
object MyPlugin extends AutoPlugin {
object autoImport {
val myTask = taskKey[Unit]("Do something.")
}
import autoImport._
override def requires = empty
override def trigger = noTrigger
override lazy val projectSettings = Seq(
myTask := {
println(s"project: ${thisProject.value.id} plugins: ${thisProject.value.plugins}")
},
clean := clean.dependsOn(myTask).value
)
}
当我在客户端项目中启用这个插件时,myTask
命令按预期工作。我还希望clean
调用相同的任务,但没有输出:
> sbt
[info] welcome to sbt 1.8.2 (Eclipse Adoptium Java 17.0.6)
[info] loading global plugins from ... .sbt1.0plugins
[info] loading settings for project sbt-example-client-build from plugins.sbt ...
[info] loading project definition from ... sbt-example-clientproject
[info] loading settings for project root from build.sbt,version.sbt ...
[info] set current project to sbt-example-client (in build file:/ ... /sbt-example-client/)
[info] sbt server started at local:sbt-server-c377c6b81cca5b432adb
[info] started sbt server
sbt:sbt-example-client> myTask
project: client plugins: nigeleke.sbt.MyPlugin
[success] Total time: 0 s, completed 13 Apr 2023, 5:05:18 pm
sbt:sbt-example-client> clean
[success] Total time: 0 s, completed 13 Apr 2023, 5:05:21 pm
sbt:sbt-example-client>
我没有找到正确的语法来调用它,所以任何指针都是赞赏的。
问题是clean
是在默认的内置sbt插件JvmPlugin
中定义的
https://github.com/sbt/sbt/blob/v1.8.2/main/src/main/scala/sbt/plugins/JvmPlugin.scala L44-L46
object JvmPlugin extends AutoPlugin {
...
override lazy val projectSettings: Seq[Setting[_]] =
... ++
Defaults.baseTasks ++ // (1)
Defaults.compileBase ++ // (2)
Defaults.defaultConfigs // (3)
}
https://github.com/sbt/sbt/blob/v1.8.2/main/src/main/scala/sbt/Defaults.scala
object Defaults extends BuildCommon {
lazy val baseTasks: Seq[Setting[_]] = projectTasks ++ ...
lazy val projectTasks: Seq[Setting[_]] = Seq(
...
clean := // (1)
Def.taskDyn(Clean.task(resolvedScoped.value.scope, full = true)).value,
...
)
def compileBase = ... ++ Seq(
...
clean := clean.dependsOn(cleanIvy).value, // (2)
...
)
lazy val defaultConfigs: Seq[Setting[_]] =
inConfig(Compile)(compileSettings) ++
inConfig(Test)(testSettings) ++ ...
lazy val compileSettings: Seq[Setting[_]] = configSettings + ...
lazy val testSettings: Seq[Setting[_]] = configSettings ++ ...
lazy val configSettings: Seq[Setting[_]] = ... ++ configTasks ++ ...
lazy val configTasks: Seq[Setting[_]] = ... ++ Seq(
...
clean := (compileOutputs / clean).value, // (3)
...
)
}
您不能禁用JvmPlugin
,因为在这种情况下,sbt无法构建项目。
所以为了覆盖clean
,你应该让你的插件依赖于JvmPlugin
(否则clean
的JvmPlugin
定义覆盖你的)
object MyPlugin extends AutoPlugin {
...
override def requires = JvmPlugin
那么以下任意选项都可以工作:
clean := clean.dependsOn(myTask).value
clean := {
myTask.value
clean.value
}
clean := Def.sequential(myTask, clean).value
如何从AutoPlugin重写默认任务?
https://www.scala-sbt.org/1.x/docs/Custom-Settings.html实现+ +任务
SBT task dependsOn
SBT依赖于使用-从0.12迁移到0.13
SBT中每个项目的任务
多个项目的Sbt运行命令
SBT中的排序和重写任务
如何排除自定义清理任务中的文件?
如何从任务执行从属项目中的清洁任务?