【问题标题】:How do you add a generator task to resourceGenerators in a SBT autoplugin?如何在 SBT 自动插件中向 resourceGenerators 添加生成器任务?
【发布时间】:2015-10-03 05:36:18
【问题描述】:

我正在 SBT 中写Autoplugin。该插件应该生成一些文件到resourceManaged。使用下面的代码(正在进行中),我在运行compile 时看不到FOOO 输出,但是当我直接使用yamlGen 调用任务时看到它,这让我认为出于某种原因我的任务未正确添加为资源生成器。我检查了一堆其他的生成器插件,他们几乎都是这样做的。这里有什么问题?

import sbt._
import Keys._

object SamplePlugin extends AutoPlugin {
  override def trigger = allRequirements

  val yamlSourceFolder = SettingKey[File](
    "yaml-source-folder",
    "description"
  )

  val yamlSources = SettingKey[Seq[File]](
    "yaml-sources",
    "description"
  )

  val outputFolder = SettingKey[File](
    "output-folder",
    "description"
  )

  val yamlGen = TaskKey[Seq[File]](
    "yaml-gen",
    "description"
  )

  def yamlSettings(conf: Configuration): Seq[Setting[_]] = inConfig(conf)(Seq(
    yamlSourceFolder <<= (sourceDirectory in Compile) { _ / "yamin" },
    yamlSources <<= yamlSourceFolder { srcDir => (srcDir ** "*.yaml").get },
    outputFolder <<= (resourceManaged in Compile) { _ / "yamout" },
    yamlGen <<= (streams, yamlSources, outputFolder).map {
      (out, sources, outputDir) =>
        println("FOOO")
        // implement me
        (outputDir ** "*.abc").get
    },
    resourceGenerators <+= yamlGen
  ))

  override def projectSettings = yamlSettings(Compile)
}

【问题讨论】:

    标签: sbt


    【解决方案1】:

    在其他论坛上找到 Mark Harrah 的答案

    compile 不会触发资源生成,因为编译不需要它。它将为 packagerun 或其他需要资源的任务触发。

    但是,让我困惑的是,你需要在项目中直接添加yamlGen:

       val root = (project in file("."))
           .enablePlugins(SamplePlugin)
           .settings(
              resourceGenerators in Compile <+= yamlGen in Compile
           )
    

    【讨论】:

      【解决方案2】:

      通过 Joshua Suereth 的 some help,我得到了它的工作。缺少对 JvmPlugin 的依赖,这显然是必需的,这样我在 resourceGenerators 中的生成器就不会被覆盖。添加这个修复它:

      override def requires: Plugins = plugins.JvmPlugin
      

      【讨论】:

        猜你喜欢
        • 2021-06-09
        • 2013-01-20
        • 2023-03-26
        • 2011-11-29
        • 2012-07-13
        • 2016-07-27
        • 2014-09-03
        • 2011-11-12
        • 2019-11-17
        相关资源
        最近更新 更多