Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何在sbt插件中生成源代码?_Scala_Sbt - Fatal编程技术网

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

Scala 如何在sbt插件中生成源代码?,scala,sbt,Scala,Sbt,我正在尝试生成一些源,如中所述 当我在我的build.sbt中放入以下内容时,一切都正常工作: sourceGenerators in Compile += Def.task { val file = (sourceManaged in Compile).value / "demo" / "Test.scala" IO.write(file, """object Test extends App { println("Hi") }""") Seq(file) }.taskValue

我正在尝试生成一些源,如中所述

当我在我的
build.sbt
中放入以下内容时,一切都正常工作:

sourceGenerators in Compile += Def.task {
  val file = (sourceManaged in Compile).value / "demo" / "Test.scala"
  IO.write(file, """object Test extends App { println("Hi") }""")
  Seq(file)
}.taskValue
但当我尝试在插件中执行相同的操作时,任务永远不会运行:

object MyPlugin extends AutoPlugin {
  override lazy val projectSettings = Seq(
    sourceGenerators in Compile += Def.task {
      val file = (sourceManaged in Compile).value / "demo" / "Test.scala"
      IO.write(file, """object Test extends App { println("Hi") }""")
      Seq(file)
    }.taskValue
  )
}
我在插件中添加的其他内容似乎都很好,但从未生成源文件


我遗漏了什么重要的东西吗?

您必须在
JvmPlugin
之后加载插件,该插件将在
projectSettings
中重置
sourceGenerators
(请参见
sbt.Defaults.sourceConfigPaths

你可以把它作为一个需求添加到你的插件中,例如

override def requires = JvmPlugin
您的完整示例应如下所示:

import sbt._
import Keys._
import plugins._

object MyPlugin extends AutoPlugin {
  override def requires = JvmPlugin
  override lazy val projectSettings = Seq(
    sourceGenerators in Compile += Def.task {
      val file = (sourceManaged in Compile).value / "demo" / "Test.scala"
      IO.write(file, """object Test extends App { println("Hi") }""")
      Seq(file)
    }.taskValue
  )
}