I’d like to compile a project which contains a java source generator and then compile the generated code within a single project.
I.e: compile Generator.scala, run Generator.generate(outputDir), compile outputDir, package into a jar.
I’m trying this:
sourceGenerators in Compile <+= sourceManaged in Compile map { out =>
Generator.generate(out / "generated")
}
but sbt complains
[error] Build.scala:1: object example is not a member of package org
[error] import org.example.Generator
Basically, sbt doesn’t see Generator defined in the project it compiles.
Is it possible to do it my way with sbt?
So, after digging on this a bit, I have come up with a solution. First, you need to break your project into two sub projects.
genhas all the source that includes your generator code.usedepends ongenand uses the generator.In this case, I was generating .java files so I passed in
javaSourceto the generator.It is important to not that when using sourceGenerators as we are here, the executed task must return a
Seq[File]of all the files that were generated so that sbt can manage them. In this implementation, our generator outputs the full path file names to standard out and we save them to a temporary file.As with all things Scala and surely SBT, you can do anything, just need to dig into it.