Sunday, August 15, 2010

Java Ant - Build Jar with Auto-Incrementing Build Number

To auto-increment a build number while building a Jar file with Ant, we can use the "manifest" Ant task with the "buildnumber" task. The buildnumber task creates a build.version file and increments the build number contained within during each build. The build number along with other information can be integrated right into the MANIFEST.MF file (or anywhere else for that matter). Here is the Ant code I use to auto-increment the build number during a Jar file build:


    <target name="manifest">
        <echo>Creating manifest</echo>
     <mkdir dir="${build.dir}/META-INF"/>
        <buildnumber file="build.version"/>
        <tstamp>
           <format property="timestamp" pattern="yyyy-MM-dd HH:mm:ss" />
        </tstamp>
        <manifest file="${build.dir}/META-INF/MANIFEST.MF">
           <attribute name="Built-By" value="${user.name}"/>
           <attribute name="Build-Version" value="0.4-b${build.number}"/> 
           <attribute name="Build-Date" value="${timestamp}"/>                 
       </manifest>
    </target>



Here is the contents of the MANIFEST.MF file:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 1.5.0_24-149 (Apple Inc.)
Built-By: tim
Build-Version: 0.4-b2
Build-Date: 2010-08-15 16:13:49

See also:
Hello World Java Web Application in Eclipse (Part 3)


No comments: