It is quite convenient to be able to show Application Version and build time in any given java program. Below is my way of doing this.
Ant script for War Files
<target name="
-post-dist">
<!-- Empty placeholder for easier customization. -->
<!--<property file="build.properties"/>-->
<buildnumber file="build.version"/>
<tstamp>
<format property="timestamp" pattern="yyyy-MM-dd HH:mm:ss"/>
</tstamp>
<jar destfile="${dist.war}" update="true">
<manifest>
<attribute name="Manifest-Version" value="1.0" />
<attribute name="Created-By" value="Me" />
<attribute name="Bundle" value="${build.number}"/>
<attribute name="Bundle-Date" value="${timestamp}" />
</manifest>
</jar>
<!-- You can override this target in the ../build.xml file. -->
</target>
Java Code for Web Application
public static String getBuildNumberAndDateFromWar(WebServiceContext context){
String buildNumber="";
try {
javax.xml.ws.handler.MessageContext mc = context.getMessageContext();
HttpServletRequest req = (HttpServletRequest) mc.get(javax.xml.ws.handler.MessageContext.SERVLET_REQUEST);
InputStream stream = req.getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF");
Manifest manifest = new Manifest(stream);
Attributes attributes = manifest.getMainAttributes();
String bundle = attributes.getValue("Bundle");
String bundle_date = attributes.getValue("Bundle-Date");
stream.close();
if (bundle!=null && !"null".equals(bundle))
buildNumber = " [bundle:"+ bundle + ", date:" + bundle_date+"] ";
} catch (Exception e) {
e.printStackTrace();
}
return buildNumber;
}
Ant Script for Jar file
<target name="
-post-jar">
<!-- Empty placeholder for easier customization. -->
<!-- You can override this target in the ../build.xml file. -->
<buildnumber file="build.version"/>
<tstamp>
<format property="timestamp" pattern="yyyy-MM-dd HH:mm:ss"/>
</tstamp>
<jar destfile="${dist.jar}" update="true">
<manifest>
<attribute name="Manifest-Version" value="1.0" />
<attribute name="Version" value="1.0.1" />
<attribute name="Created-By" value="Me" />
<attribute name="Bundle" value="${build.number}"/>
<attribute name="Bundle-Date" value="${timestamp}" />
</manifest>
</jar>
</target>
Java code for Java Application
public static String getVersionBuildNumberAndDateFromJar(){
String buildNumber="";
try {
InputStream stream = (new EscrowPaymentsAppClient()).getClass().getResourceAsStream("/META-INF/MANIFEST.MF");
Manifest manifest = new Manifest(stream);
Attributes attributes = manifest.getMainAttributes();
String version = attributes.getValue("Version");
String bundle = attributes.getValue("Bundle");
String bundle_date = attributes.getValue("Bundle-Date");
stream.close();
if (bundle!=null && !"null".equals(bundle))
buildNumber = " [version:"+ version + ", bundle:"+ bundle + ", date:" + bundle_date+"] ";
} catch (Exception e) {
e.printStackTrace();
}
return buildNumber;
}