如何从AutoPlugin修改Compile中的sourceGenerators?

mun*_*cho 1 code-generation scala sbt 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


}
Run Code Online (Sandbox Code Playgroud)

mun*_*cho 5

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

override def requires = JvmPlugin
Run Code Online (Sandbox Code Playgroud)

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

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