如何在我的自定义sbt插件中使用sbt命令,例如clean和compile。

huangapple go评论92阅读模式
英文:

How can I use sbt commands like clean and compile in my custom sbtplugin

问题

以下是翻译好的部分:

  1. 我有以下的最小自定义sbt插件我想要在其中实现覆盖标准的`clean`命令以执行与`myTask`相同的操作
  2. 当我在客户项目中启用此插件时`myTask`命令按预期工作我还希望`clean`调用相同的任务但没有输出
  3. 我无法找到正确的语法来调用它所以感谢任何指点
英文:

I have the following, minimal, custom sbt plugin, where I'd like to implement (override) the standard clean command to do the same action as myTask.

  1. package nigeleke.sbt
  2. import sbt.*
  3. import Keys.*
  4. import scala.sys.process.*
  5. object MyPlugin extends AutoPlugin {
  6. object autoImport {
  7. val myTask = taskKey[Unit]("Do something.")
  8. }
  9. import autoImport._
  10. override def requires = empty
  11. override def trigger = noTrigger
  12. override lazy val projectSettings = Seq(
  13. myTask := {
  14. println(s"project: ${thisProject.value.id} plugins: ${thisProject.value.plugins}")
  15. },
  16. clean := clean.dependsOn(myTask).value
  17. )
  18. }

When I enable this plugin a client project, the myTask command works as expected. I would also like clean to invoke the same task, but no output is forthcoming:

  1. > sbt
  2. [info] welcome to sbt 1.8.2 (Eclipse Adoptium Java 17.0.6)
  3. [info] loading global plugins from ... \.sbt.0\plugins
  4. [info] loading settings for project sbt-example-client-build from plugins.sbt ...
  5. [info] loading project definition from ... \sbt-example-client\project
  6. [info] loading settings for project root from build.sbt,version.sbt ...
  7. [info] set current project to sbt-example-client (in build file:/ ... /sbt-example-client/)
  8. [info] sbt server started at local:sbt-server-c377c6b81cca5b432adb
  9. [info] started sbt server
  10. sbt:sbt-example-client> myTask
  11. project: client plugins: nigeleke.sbt.MyPlugin
  12. [success] Total time: 0 s, completed 13 Apr 2023, 5:05:18 pm
  13. sbt:sbt-example-client> clean
  14. [success] Total time: 0 s, completed 13 Apr 2023, 5:05:21 pm
  15. sbt:sbt-example-client>

I'm failing to find the correct syntax to invoke it, so any pointers are appreciated.

答案1

得分: 1

The thing is that clean is defined in a default built-in sbt plugin JvmPlugin.

你要翻译的代码部分已经翻译完成,如有需要,请提供更多内容。

英文:

The thing is that clean is defined in a default built-in sbt plugin JvmPlugin

https://github.com/sbt/sbt/blob/v1.8.2/main/src/main/scala/sbt/plugins/JvmPlugin.scala#L44-L46

  1. object JvmPlugin extends AutoPlugin {
  2. ...
  3. override lazy val projectSettings: Seq[Setting[_]] =
  4. ... ++
  5. Defaults.baseTasks ++ // (1)
  6. Defaults.compileBase ++ // (2)
  7. Defaults.defaultConfigs // (3)
  8. }

https://github.com/sbt/sbt/blob/v1.8.2/main/src/main/scala/sbt/Defaults.scala

  1. object Defaults extends BuildCommon {
  2. lazy val baseTasks: Seq[Setting[_]] = projectTasks ++ ...
  3. lazy val projectTasks: Seq[Setting[_]] = Seq(
  4. ...
  5. clean := // (1)
  6. Def.taskDyn(Clean.task(resolvedScoped.value.scope, full = true)).value,
  7. ...
  8. )
  9. def compileBase = ... ++ Seq(
  10. ...
  11. clean := clean.dependsOn(cleanIvy).value, // (2)
  12. ...
  13. )
  14. lazy val defaultConfigs: Seq[Setting[_]] =
  15. inConfig(Compile)(compileSettings) ++
  16. inConfig(Test)(testSettings) ++ ...
  17. lazy val compileSettings: Seq[Setting[_]] = configSettings + ...
  18. lazy val testSettings: Seq[Setting[_]] = configSettings ++ ...
  19. lazy val configSettings: Seq[Setting[_]] = ... ++ configTasks ++ ...
  20. lazy val configTasks: Seq[Setting[_]] = ... ++ Seq(
  21. ...
  22. clean := (compileOutputs / clean).value, // (3)
  23. ...
  24. )
  25. }

You can't disable JvmPlugin because in such case sbt can't build a project.

So in order to override clean you should make your plugin depend on JvmPlugin (otherwise JvmPlugin definition of clean overrides yours)

  1. object MyPlugin extends AutoPlugin {
  2. ...
  3. override def requires = JvmPlugin

Then any of the following options works:

  1. clean := clean.dependsOn(myTask).value
  1. clean := {
  2. myTask.value
  3. clean.value
  4. }
  1. clean := Def.sequential(myTask, clean).value

https://stackoverflow.com/questions/25247662/how-can-a-default-task-be-overridden-from-an-autoplugin

https://www.scala-sbt.org/1.x/docs/Custom-Settings.html#Implementing+a+task

https://stackoverflow.com/questions/41183255/sbt-task-dependson

https://stackoverflow.com/questions/44657163/sbt-dependson-usage-migration-from-0-12-to-0-13

https://stackoverflow.com/questions/54393160/per-project-tasks-in-sbt

https://stackoverflow.com/questions/57806115/sbt-run-command-for-multiple-projects

https://stackoverflow.com/questions/34841410/sequencing-and-overriding-tasks-in-sbt

https://stackoverflow.com/questions/24020437/how-to-exclude-files-in-a-custom-clean-task

https://stackoverflow.com/questions/26218457/how-to-execute-clean-task-in-dependent-projects-from-a-task

huangapple
  • 本文由 发表于 2023年4月13日 15:13:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定