我正在使用新的 AutoPlugin 机制为 sbt 编写代码生成插件。我需要修改sourceGenerators in Compile
设置,但当我从插件中执行此操作时,它不起作用。调用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
}
最佳答案
问题是 JVM 插件重置了 sourceGenerators 设置。解决办法就是添加:
override def requires = JvmPlugin
我在另一个问题中找到了解决方案:
关于scala - 如何从 AutoPlugin 修改 Compile 中的 sourceGenerators?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25158287/