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

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

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

问题

以下是翻译好的部分:

我有以下的最小自定义sbt插件我想要在其中实现覆盖标准的`clean`命令以执行与`myTask`相同的操作

当我在客户项目中启用此插件时`myTask`命令按预期工作我还希望`clean`调用相同的任务但没有输出

我无法找到正确的语法来调用它所以感谢任何指点
英文:

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.

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
  )

}

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:

> sbt
[info] welcome to sbt 1.8.2 (Eclipse Adoptium Java 17.0.6)
[info] loading global plugins from ... \.sbt.0\plugins
[info] loading settings for project sbt-example-client-build from plugins.sbt ...
[info] loading project definition from ... \sbt-example-client\project
[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>

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

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)
    ...
  )
}

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)

object MyPlugin extends AutoPlugin {
  ...

  override def requires = JvmPlugin

Then any of the following options works:

clean := clean.dependsOn(myTask).value
clean := {
  myTask.value
  clean.value
}
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:

确定