scala - 如何从 AutoPlugin 修改 Compile 中的 sourceGenerators?

标签 scala sbt code-generation sbt-plugin

我正在使用新的 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

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

How to generate sources in an sbt plugin?

关于scala - 如何从 AutoPlugin 修改 Compile 中的 sourceGenerators?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25158287/

相关文章:

scala - scala 中的类型安全响应是否可能?

java - 如何管理来自在单独线程中运行的多个外部命令的多个标准输出?

firefox - 如何在Firefox中查看 “generated HTML code”?

java - JHipster:错误:Image和ImageType的关系中,ImageType没有声明

java - 使用 Asm 字节码生成器 (ClassWriter) 生成具有泛型类型的方法

scala - 有没有办法引用被内联类字段遮蔽的函数参数?

scala - 在 Scala 中将函数文字与 quasiquotes 匹配

scala - SBT 测试任务如何管理类路径以及如何从 SBT 测试正确启动 Java 进程

java - 未找到 Spark RDD 类

java - sbt编译错误: no augmentString method in StringOps