Maven: Adding Custom Attributes and Build Timestamp to Manifest
By admin on Feb 20, 2008 in Java, Programming
Here is how I add custom attributes and build time stamp to my manifest file. Reference is made to the Maven CookBook.
First, in src/main/resources/META-INF/MANIFEST.MF, I added the following entries
Specification-Title: App Name
Specification-Version: ${pom.version} - ${build.time}
Specification-Vendor: Company Name
Implementation-Title: App Name
Implementation-Version: ${pom.version} - ${build.time}
Implementation-Vendor: Company Name
Built-By: ${user.name}
Build-Jdk: ${java.version}
Build-Time: ${build.time}
In my pom.xml file, I added the following
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>${basedir}/target/filter.properties</filter>
</filters>
I use Maven AntRun plugin to generate the build time,
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<mkdir dir="${project.build.directory}"/>
<tstamp>
<format property="last.updated"
pattern="yyyy-MM-dd hh:mm:ss"/>
</tstamp>
<echo file="${basedir}/target/
filter.properties" message="build.time=${last.updated}"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
Then I set the pom file to use the default manifest file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<useDefaultManifestFile>true</useDefaultManifestFile>
<!--
<archive>
<index>true</index>
<manifest>
<addClasspath>true</addClasspath>
<addDefaultImplementationEntries>true
</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true&
lt;/addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Built-By>${user.name}</Built-By>
<Build-Jdk>${java.version}</Build-Jdk>
</manifestEntries>
</archive>
-->
</configuration>
</plugin>
Then generated MANIFEST.MF in the jar file will look like this.
Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: Apache Maven Built-By: admin Build-Jdk: 1.5.0_14 Specification-Title: App Name Specification-Version: 0.1 - 2008-02-21 01:03:13 Specification-Vendor: Company Name Implementation-Title: App Name Implementation-Version: 0.1 - 2008-02-21 01:03:13 Implementation-Vendor: Company Name Build-Time: 2008-02-21 01:03:13
