Skip to content

Instantly share code, notes, and snippets.

@arjanblokzijl
Created August 12, 2011 06:24
Show Gist options
  • Save arjanblokzijl/1141567 to your computer and use it in GitHub Desktop.
Save arjanblokzijl/1141567 to your computer and use it in GitHub Desktop.
sbt10 - generated sources
import sbt._
import sbt.Keys._
object build extends Build {
lazy val generate = Project(
id = "source-gen",
base = file("."),
settings = Defaults.defaultSettings ++ generateSourceSettings
)
lazy val GenSourcesConfig = config("gen-sources-config").hide
lazy val genSources = TaskKey[Seq[File]]("gen-src")
lazy val outputDir = SettingKey[File]("output-dir")
lazy val generateSourceSettings: Seq[Project.Setting[_]] = inConfig(GenSourcesConfig)(Seq(
genSources <<= genSourcesTask,
outputDir <<= (sourceDirectory in Compile) {_ / "src_managed"},
(managedSourceDirectories in Compile) <+= (outputDir in GenSourcesConfig).identity
))++ Seq(sourceGenerators in Compile <+= (genSources in GenSourcesConfig).identity)
def genSourcesTask = (streams, outputDir in GenSourcesConfig) map ((s, dir) => {
s.log.info("generating java sources")
val source = "package javapackage;\n\n" +
"public class GeneratedJavaClass {\n\n public String theString;\n}\n"
val outputFile = (dir / "GeneratedJavaClass.java").asFile
IO.write(outputFile, source)
//Seq(outputFile) in case you generate directly
(dir ** "*.java").get //in case your files are generated by an external process
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment