如何将任务依赖项从另一个插件添加到我的 SBT 插件?

时间:2021-01-03 15:53:49

标签: scala sbt sbt-plugin

我正在为 SBT 编写一个插件,它将生成从 Scala 程序创建 Azure 函数所需的配置文件。我的工作是在 github (https://github.com/code-star/sbt-azure-functions-plugin/tree/develop) 上,并且可以在自述文件中阅读,我想在用户发出 assembly 时自动触发 sbt azfunCreateZipFile 任务。

我插件的用户可以通过在他们的 build.sbt 中添加以下行来实现:

azfunCreateZipFile := (azfunCreateZipFile dependsOn assembly).value

我希望该任务连接已包含在我的插件定义中。我该怎么做?

我知道通过在任务定义中使用 otherTask.value 可以使任务依赖于其他任务,所以我尝试在 azfunCreateZipFile 任务定义中添加这样一行(参见 ./plugin/src/main/scala/sbtazurefunctions/AzureFunctions.scala):

azfunGenerateFunctionJsons := {
  // depend on assembly step
  val _ = assembly.value
  ... <actual task definition code>
  azfunTargetFolder.value
}

但后来我收到一个错误,提示找不到程序集:

[IJ]sbt:root> compile
[info] Compiling 1 Scala source to /home/jml/work/scala/azure/sbt-azure-functions- plugin/plugin/target/scala-2.12/sbt-1.0/classes ...
[error] /home/jml/work/scala/azure/sbt-azure-functions-plugin/plugin/src/main/scala/sbtazurefunctions/AzureFunctions.scala:113:15: not found: value assembly
[error]       val _ = assembly.value
[error]               ^
[error] one error found
[error] (plugin / Compile / compileIncremental) Compilation failed
[error] Total time: 1 s, completed Jan 3, 2021, 4:41:56 PM
[IJ]sbt:root> 

我尝试了很多导入,但都没有成功:

import sbtassembly.AssemblyPlugin._ (and variations using AssemblyKeys and the AutoImport object)
import sbt.Keys._

我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:1)

另一个 SO 答案 (How can I use an sbt plugin as a dependency in a multi-project build?) 终于让我走上了正轨,因为它显示了 libraryDependencies += Defaults.sbtPluginExtra 的使用,所以感谢 @michael-zajac。

我最终添加了这样的程序集依赖项:

lazy val plugin = (project in file("plugin"))
  .enablePlugins(SbtPlugin)
  .settings(
    ...<other settings>...,
    libraryDependencies ++= Seq(
      "org.scala-sbt" %% "scripted-plugin" % sbtVersion.value,
      Defaults.sbtPluginExtra("com.eed3si9n" % "sbt-assembly" % "0.14.10", "1.0", "2.12")
    ),
    ...

更好:

    libraryDependencies ++= Seq(
      "org.scala-sbt" %% "scripted-plugin" % sbtVersion.value
    ),
    addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.10"),