2

I'm writing a code generation plugin for sbt using the new AutoPlugin mechanism. I need to modify the sourceGenerators in Compile setting but somehow it does not work when I do it from the plugin. After calling compile, nothing is printed on the screen.

However, if I take the line sourceGenerators in Compile <+= (mySourceGenerator in Compile) and move it to the build.sbt of the project suddenly the sourceGenerators in Compilesetting is modified and when I run the compile task the message is written to the screen.

Is there something I'm missing there? The code of the plugin is here:

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


}
mundacho
  • 229
  • 1
  • 8

1 Answers1

5

The problem was that the JVM plugin resets the sourceGenerators setting. The solution is just to add:

override def requires = JvmPlugin

I found the solution in this other question:

How to generate sources in an sbt plugin?

Community
  • 1
  • 1
mundacho
  • 229
  • 1
  • 8