Cookbook: How To Add Build Time To A JAR Manifest?

Summary

This recipe describes how to add build time to a JAR manifest by calling Apache Ant tasks.

Prerequisite Plugins

Here is the list of the plugins used:

Plugin Version
antrun 1.1
jar 2.2

Sample Generated Manifest

  1. Manifest-Version: 1.0
  2. Archiver-Version: Plexus Archiver
  3. Created-By: Apache Maven
  4. Built-By: vsiveton
  5. Build-Jdk: 1.5.0_12
  6. Build-Time: 2008-01-18 06:53:13

Recipe

Configuring MANIFEST.MF

To generate and add build time into a Jar Manifest, we are using MANIFEST.MF, located in the src/main/resources/META-INF directory, which contains a built time value to be interpolated, i.e.

  1. Build-Time: ${build.time}

Configuring The POM

The value ${build.time} from the MANIFEST.MF will be filtering by Maven due to the <filtering> element. The value is taken from the file ${basedir}/target/filter.properties, listed by the <filter> element, i.e.

  1. <resources>
  2. <resource>
  3. <directory>src/main/resources</directory>
  4. <filtering>true</filtering>
  5. </resource>
  6. </resources>
  7.  
  8. <filters>
  9. <filter>${basedir}/target/filter.properties</filter>
  10. </filters>

Configuring Maven Antrun Plugin

The filter.properties file will be generated by the Antrun plugin. We use two core Ant Tasks, <tstamp> and <echo>.

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-antrun-plugin</artifactId>
  4. <executions>
  5. <execution>
  6. <phase>generate-resources</phase>
  7. <goals>
  8. <goal>run</goal>
  9. </goals>
  10. <configuration>
  11. <tasks>
  12. <!-- Safety -->
  13. <mkdir dir="${project.build.directory}"/>
  14.  
  15. <tstamp>
  16. <format property="last.updated" pattern="yyyy-MM-dd hh:mm:ss"/>
  17. </tstamp>
  18. <echo file="${basedir}/target/filter.properties" message="build.time=${last.updated}"/>
  19. </tasks>
  20. </configuration>
  21. </execution>
  22. </executions>
  23. </plugin>

Configuring Maven Jar Plugin

The last configuration is to set the defaultManifestFile to true to enable it.

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-jar-plugin</artifactId>
  4. <configuration>
  5. <useDefaultManifestFile>true</useDefaultManifestFile>
  6. </configuration>
  7. </plugin>

Running Maven

Just call Maven to generate the package:

  1. mvn package

Other Tips

You could tweak the Jar Plugin configuration into the War Plugin.