2

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

但是,如果我突然将这条线sourceGenerators in Compile <+= (mySourceGenerator in Compile)移到项目的位置,则设置会被修改,并且当我运行编译任务时,消息会写入屏幕。build.sbtsourceGenerators 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


}
4

1 回答 1

5

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

override def requires = JvmPlugin

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

如何在 sbt 插件中生成源代码?

于 2014-08-06T10:48:48.857 回答