如何在SBT自动插件中向resourceGenerators添加生成器任务?

时间:2015-07-14 14:07:54

标签: sbt

我在SBT写了Autoplugin。该插件应生成一些文件到resourceManaged。使用下面的代码(正在进行中),当我运行FOOO时,我没有看到compile输出,但是当我直接使用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)
}

2 个答案:

答案 0 :(得分:0)

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

  

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

然而,令我困惑的是你需要直接在项目中添加yamlGen:

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

答案 1 :(得分:0)

来自Joshua Suereth的some help我得到了它的工作。错过了JvmPlugin的依赖性,这显然是必需的,因此resourceGenerators中的生成器不会被覆盖。添加此修复它:

override def requires: Plugins = plugins.JvmPlugin