I am using sourceGenerators in Compile
to generate some Scala source files to target\scala-2.10\src_managed. When I run sbt compile
the sources are generated and compiled along with the regular code under src\main\scala.
But what if I want to generate the sources separately without compiling? What I am looking for is this flow:
How can this be accomplished?
Update
If I got you correct now, you want to be able to call the source generators seperately. For that you can simply define a custom task like this somewhere in your /build.sbt
or /project/Project.scala
file:
val generateSources = taskKey[List[File]]("generate sources")
generateSources <<=
(sourceGenerators in Compile) { _.join.map(_.flatten.toList) }
you can then call the generator manually from the sbt console like this:
> generateSources
[success] Total time: 0 s, completed 07.04.2014 13:42:41
Side Note: I would however reccomend to setup your IDE to generate the sources if the only thing you need them for is to get IDE support.
Old answer for future reference
You don't need to do anything special to use a generated class or object from a non-generated class or object.
In your /build.sbt
or /project/Project.scala
file you define the source generator:
sourceGenerators in Compile <+= sourceManaged in Compile map { dir =>
val file = dir / "A.scala"
IO.write(file, "class A(val name: String)")
Seq(file)
}
Then you write some code which creates an instance of class A in/src/main/scala/B.scala
:
object B extends App {
val a = new A("It works")
println(a.name)
}
If you compile this code from sbt, it will consider both generated and non-generated code at compilation:
> run
[info] Compiling 2 scala sources to <...>
[info] Running B
It works
[success] Total time: 0 s, completed 07.04.2014 13:15:47
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With