如何在 sbt 插件中生成源代码? [英] How to generate sources in an sbt plugin?

查看:29
本文介绍了如何在 sbt 插件中生成源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照 Generating 中所述生成一些源文件.

当我将以下内容放入我的 build.sbt 时,一切正常:

When I put the following in my build.sbt, everything works:

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

但是当我尝试在插件中做同样的事情时,任务永远不会运行:

But when I attempt to do the same thing in a plugin, the task never runs:

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
  )
}

我在插件中放入的所有其他内容似乎都可以正常工作,但从未生成源文件.

Everything else I put in my plugin seems to work fine, but the source file is never generated.

我是否遗漏了一些重要的东西?

Am I missing something important?

推荐答案

您必须在 JvmPlugin 之后加载您的插件,这会重置 projectSettings<中的 sourceGenerators/code>(见 sbt.Defaults.sourceConfigPaths).

You have to load your plugin after the JvmPlugin, which resets sourceGenerators in projectSettings (see sbt.Defaults.sourceConfigPaths).

您可以通过将其作为要求添加到您的插件中来实现,例如

You can do that by adding it as a requirements to your plugin, e.g.

override def requires = JvmPlugin

您的完整示例应如下所示:

Your complete example should look as follows:

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
  )
}

这篇关于如何在 sbt 插件中生成源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆