scala – 如何在sbt插件中生成源代码?
发布时间:2020-12-16 09:16:32 所属栏目:安全 来源:网络整理
导读:我正在尝试生成一些来源,如 Generating files所述. 当我将以下内容放在我的build.sbt中时,一切正常: sourceGenerators in Compile += Def.task { val file = (sourceManaged in Compile).value / "demo" / "Test.scala" IO.write(file,"""object Test exten
我正在尝试生成一些来源,如
Generating files所述.
当我将以下内容放在我的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之后加载插件,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 ) } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |