Skip to content

Instantly share code, notes, and snippets.

@mitchellh
Created March 25, 2011 02:52
Show Gist options
  • Save mitchellh/886285 to your computer and use it in GitHub Desktop.
Save mitchellh/886285 to your computer and use it in GitHub Desktop.
/**
* Creates a build properties file resource and packages it into the JAR,
* which can be read for various information such as version and build time.
* */
trait BuildPropertiesResource extends BasicScalaProject {
/**
* Specifies the name of the resource file. This is put [by default]
* in the top level directory of the jar.
* */
def buildPropertiesResourceFile = "build.properties"
/**
* Specifies the actual path to the resource in the JAR.
* */
def buildPropertiesResourcePath = (outputPath ##) / buildPropertiesResourceFile
/**
* Specifies the properties which will actually go into the build
* properties resource file.
* */
def buildPropertiesResourceData = Map("build.time" -> System.currentTimeMillis().toString,
"build.version" -> version.toString)
// Make the compile task depend on writing the resource
override def compileAction = super.compileAction dependsOn(writeBuildPropertiesResource)
// Add the build resource to the main resources list so that
// it is properly packaged to the jar.
abstract override def mainResources = super.mainResources +++ buildPropertiesResourcePath
// The actual task to write out the build resource
lazy val writeBuildPropertiesResource = writeBuildPropertiesResourceTask
protected def writeBuildPropertiesResourceTask: Task = task {
import java.io.FileOutputStream
import java.util.Properties
val props = new Properties()
buildPropertiesResourceData.foreach(pair => props.setProperty(pair._1, pair._2))
val outStream = new FileOutputStream(buildPropertiesResourcePath.asFile)
props.store(outStream, "Build properties from compile-time")
outStream.close()
None
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment