Skip to content

Instantly share code, notes, and snippets.

@freekh
Created December 19, 2012 12:46
Show Gist options
  • Save freekh/4336428 to your computer and use it in GitHub Desktop.
Save freekh/4336428 to your computer and use it in GitHub Desktop.
import sbt._
import Keys._
import play.Project._
object ApplicationBuild extends Build {
val appName = "javatest"
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
// Add your project dependencies here,
javaCore,
javaJdbc,
javaEbean
)
import VersionInfoPlugin._
//add this: ^ import the plugin
val main = play.Project(appName, appVersion, appDependencies).settings(
// Add your own project settings here
).settings(versionInfoSettings: _*)
//add this: ^ enable the plugin
.settings(
versionInfoPackage := "com.mycompany.utils"
//add this: ^ the package for the generated class
)
}
import sbt._
import Keys._
object VersionInfoPlugin extends Plugin {
val versionInfoPackage = SettingKey[String]("version-info-package")
val versionInfoClassTask = TaskKey[Seq[java.io.File]]("version-info-class", "writes a java class VersionInfo with information about the versionInfo to sourceManaged")
val versionInfo = TaskKey[String]("version-info", "the current versionInfo")
val versionInfoSettings = Seq(
versionInfo <<= version map { version =>
Option(System.getProperty("version")).getOrElse{ //use property version if found
version
}
},
versionInfoClassTask <<= (sourceManaged in Compile, versionInfoPackage, versionInfo) map { (srcDir, versionInfoPackage, versionInfo) =>
// depends on: ^ source directory, ^ the package ^ the version
val classString = """
|package %s;
|
|public class VersionInfo {
| public static String CURRENT_VERSION = "%s";
|}""" format (versionInfoPackage, versionInfo) stripMargin
val path = srcDir.getAbsolutePath + versionInfoPackage.replace(".", java.io.File.separator) + java.io.File.separator + "VersionInfo.java"
IO.write(file(path), classString)
Seq(file(path))
},
sourceGenerators in Compile <+= versionInfoClassTask
//add our versionInfoClass to generators so it is picked up by sbt
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment