mdesjardins.github.io

Recently, I had to put a build timestamp onto a login page for a web application I’m developing at work. The web application is written using Tapestry 4.1, but some of the techniques are equally applicable to other frameworks. I thought I’d share.

First, you need to setup your ant task to grab a timestamp, and put it into your manifest file. You do so using the tstamp task, like this:
The tstamp task is taking the current date and time, formatting it as specified by pattern (just as you’d specify it in a SimpleDateFormat) and placing it in the buildtstamp variable. The manifest task builds a MANIFEST.MF file which ends up in your deployed web application’s META-INF directory. You’ll notice that I’m also putting the name of the user who built the application into the manifest.

Next, we need to read the Manifest from our application. The first screen presented by my Tapestry app is LogOn.java. First, use HiveMind to inject the ServletContext into my page:

@InjectObject(service:tapestry.globals.ServletContext)
public abstract ServletContext getServletContext();

Also, we need to create an abstract method into which we’ll store and retrieve the build date:

public abstract String getBuiltOn();
public abstract void setBuiltOn(String builtOn);

Finally, we need to read the Manifest file in our pageBeginRender method, and set the “Built On” date accordingly. This is how I did this:

public void pageBeginRender(PageEvent event) {
  ServletContext sc = this.getServletContext();
  String filename = sc.getRealPath("/META-INF/MANIFEST.MF");
  try {  
    BufferedInputStream i = new BufferedInputStream(new FileInputStream(filename));
    Manifest m = new Manifest(i);
    Attributes attrib = m.getMainAttributes();
    this.setBuiltOn(attrib.getValue("Build-Date"));
  } catch (Exception e) {  
    log.warn("Unable to read MANIFEST.MF");
  }
}

Finally, we need to actually render this on the LogOn page. I did this with a simple Insert component directly on the html page:

Built: <span jwcid="@Insert" value="ognl:builtOn"></span>

And voila! You have a build date on your log page, which can come in handy, e.g., when your QA team doesn’t know which version they’re testing!