How to Add Version Information to a Jar File with Netbeans

There are 2 common ways vendors use to version a jar file:

  • Add a version information to the jar file manifest
  • Append a version number to the jar file name

The preferred mechanism for storing version information is to use the Manifest file. There are two keywords commonly used to store version information in the manifest: "Implementation-Version" and "Bundle-Version".

Unfortunately, Netbeans 6 and 7 doesn't make it easy to set these properties through the user interface. Instead, you must create/edit the manifest file on your own. Fortunately, there's a way to automate process of creating/editing the manifest file via an ant task. In this tutorial, I will outline the steps I took to create a manifest on-the-fly and set properties dynamically.

Step 1: Edit your "project.properties"

First, you must update your Netbeans "project.properties" file found in the "nbproject" directory. Add the following line to the file:
manifest.file=manifest.mf

Step 2: Edit your "build.xml"

Next, create an ant task to create/update the manifest using the "build.xml" file. In this example, we will set the version number and date of the jar file.
<target name="-pre-init">
   <property name="project.name" value="My Library" />
   <property name="version.num" value="1.4.1" />
   <tstamp>
      <format property="NOW" pattern="yyyy-MM-dd HH:mm:ss z" />
   </tstamp>

   <!--
   <exec outputproperty="svna.version" executable="svnversion">
       <arg value="-c" />
       <redirector>
           <outputfilterchain>
               <tokenfilter>
                   <replaceregex pattern="^[0-9]*:?" replace="" flags="g"/>
                   <replaceregex pattern="M" replace="" flags="g"/>
               </tokenfilter>
           </outputfilterchain>
       </redirector>
   </exec>
   -->


   <manifest file="MANIFEST.MF">
      <attribute name="Bundle-Name" value="${project.name}" />           
      <attribute name="Bundle-Version" value="${version.num}" />
      <attribute name="Bundle-Date" value="${NOW}" />
      <!--<attribute name="Bundle-Revision" value="${svna.version}" />-->
      <attribute name="Implementation-Title" value="${project.name}" />
      <attribute name="Implementation-Version" value="${version.num}" />
      <attribute name="Implementation-URL" value="http://www.example.com" />
   </manifest>

</target>
This will create a manifest file in your netbeans project directory and stuff it into your jar file. If you want to delete the autogenerated manifest file from your netbeans project directory, simply create another ant task (post jar of course):
<target name="-post-jar">
  <delete file="MANIFEST.MF"/>
</target>

Step 3: Build and Test

After you have edited your "build.xml" file, simply rebuild your netbeans project to generate a new jar file. The jar file manifest should contain the properties we specified in the ant script. You can inspect the manifest by unzipping it from the META-INF directory in the jar file or you can list the contents programatically like this.

References