Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 2.0: How to add SBT build number to page footer

I would like to add build information to the footer of my Play! application (Play! 2.0.4 with Scala), similar as done here on stackoverflow. I'm new to SBT and just happy that most of the time everything works like a charm. :-)

Basically, I would like to extract the application version from project/Build.scala, add the current date, and finally add a build number (that is incremented automatically, but that may be the topic of another question to post). This information should be added to a file conf/build-info.conf that is included into the main conf/application.conf. I know how to extract the build info from the application configuration and add it to the pages.

Thank you for your time!

like image 433
Erich Schreiner Avatar asked Jan 25 '13 05:01

Erich Schreiner


2 Answers

I came up with an extension to the bash script that does the actual staging and starting of my application, thus providing the build information before even starting SBT / play. This extension grabs the revision of the working directory off the Mercurial repo and writes this with the current date into the file conf/build-info.conf. I refrained from using an automatically incremented build counter as the information provided by the repo should be sufficient.

#!/bin/bash
CHANGESET=`hg tip | grep changeset`
while IFS=":" read -ra PARTS; do
    REV=`echo "${PARTS[1]}" | tr -d ' '`@`hg branch`
    echo application.build=\"`date +"%Y-%m-%d"` "[$REV]"\" > conf/build-info.conf
done <<< "$CHANGESET"

resulting in a file containg a line similar to

application.build="2013-01-25 [98@bug-001]"

This script could easily be extended to grep the file project/Build.scala for the application version and include this in the build info created.

Feel free to copy/paste and adapt / improve. No attribution required but feedback here is always welcome :-)

like image 171
Erich Schreiner Avatar answered Sep 22 '22 13:09

Erich Schreiner


A simple way to add build information to your project at compile time, is to use an SBT plugin called sbt-buildinfo.

First choose the proper version based on your SBT version, and follow the instructions. by default, only your application name, version, scalaVersion and sbtVersion are documented at compile time.

To add the build time, dependencies or any other custom value, you can add this code to your build.sbt and customize it:

    buildInfoKeys ++= Seq[BuildInfoKey](
      resolvers,
      libraryDependencies in Test,
      BuildInfoKey.map(name) { case (k, v) => "project" + k.capitalize -> v.capitalize },
      "custom" -> 12345, // computed at project load time
      BuildInfoKey.action("buildTime") {
        System.currentTimeMillis
      } // re-computed each time at compile
    )
like image 37
behzad Avatar answered Sep 22 '22 13:09

behzad