如何从AutoPlugin修改Compile中的sourceGenerators?

时间:2014-08-06 10:42:06

标签: scala sbt code-generation sbt-plugin

我正在使用新的AutoPlugin机制为sbt编写代码生成插件。我需要修改sourceGenerators in Compile设置,但是当我从插件中执行此操作时它不起作用。调用编译后,屏幕上不会打印任何内容。

但是,如果我采用行sourceGenerators in Compile <+= (mySourceGenerator in Compile)并将其移动到项目的build.sbt突然修改sourceGenerators in Compile设置,当我运行编译任务时,将写入消息屏幕。

我有什么东西在那里失踪吗?插件的代码在这里:

package net.lopezbobeda.plugin

import sbt._
import Keys._
import java.io.{ File, Writer }

object MyPlugin extends AutoPlugin {
  // by defining autoImport, the settings are automatically imported into user's `*.sbt`
  object autoImport {
    // configuration points, like the built-in `version`, `libraryDependencies`, or `compile`
    lazy val mySourceGenerator = taskKey[Seq[File]]("Generate")

    // default values for the tasks and settings
    lazy val baseXtendPluginSettings: Seq[Def.Setting[_]] = Seq(
      mySourceGenerator in Compile := {
        val s: TaskStreams = streams.value
        s.log.info("Generating! "  + sourceManaged.value)
        Nil
      },
      sourceGenerators in Compile <+= (mySourceGenerator in Compile) // if I put this line in build.sbt everything works as expected.
      )

  }

  override def trigger = allRequirements

  import autoImport._

  override val projectSettings = baseXtendPluginSettings


}

1 个答案:

答案 0 :(得分:5)

问题是JVM插件重置了sourceGenerators设置。解决方案只是添加:

override def requires = JvmPlugin

我在另一个问题中找到了解决方案:

How to generate sources in an sbt plugin?