Now let's add a few more features to versioning. Specifically, the ability to reset the build number and adding a 'release' number so that 0.0.1 becomes 0.0.0.1 (release.major.minor.build). Finally, we'll add a simple delete deploy target and force the developer to call that directly.

    <target name="version" [...]>
        <propertyfile file="${build.version.file}" comment="Version Information">
            [...]
            <entry  key="build.release" type="int" default="0"/>
            <entry  key="build.date" type="date" value="now" pattern="yyyy-MM-dd HH:mm:ss"/>
        </propertyfile>
        [...]
        <!-- set version to current version -->
        <property name="version" value="${build.release}.${build.major}.${build.minor}.${build.number}"/>          
       
        <echo level="info">
    ***********************************************************************
    *
    * version ${version} built on ${build.date}
    *
    ***********************************************************************
        </echo>
    </target>
   
    <!-- ==========================================================
       
             reset.build.number
             reset the build number
       
         ========================================================== -->
    <target name="reset.build.number"
            description="reset the build number">
        <echo level="verbose">
    ***********************************************************************
    *
    * Resetting the build number
    *
    ***********************************************************************
        </echo>    
        <propertyfile file="${build.version.file}" comment="Version Information">
            <entry  key="build.number" type="int" value="0"/>
            <entry  key="build.minor" type="int" default="0"/>
            <entry  key="build.major" type="int" default="0"/>
            <entry  key="build.release" type="int" default="0"/>
            <entry  key="build.date" type="date" value="now" pattern="yyyy-MM-dd HH:mm:ss"/>
        </propertyfile>
       
        <property file="${build.version.file}"/>
        <antcall target="version"/>
        <property name="already.inc.build.number" value="true"/>
       
    </target>  

    <target name="inc.build.number" [...]>
        [...]      
        <propertyfile [...]>
            [...]
            <entry  key="build.release" type="int" default="0"/>
            [...]
        </propertyfile>
        [...]
        <!--
        <property name="version" value="${build.major}.${build.minor}.${build.number}"/>
        -->
        [...]
    </target>

    <target name="inc.build.minor" [...]>
        [...]      
        <propertyfile [...]>
            [...]
            <entry  key="build.release" type="int" default="0"/>
            [...]
        </propertyfile>
        [...]
    </target>

    <target name="inc.build.major" [...]>
        [...]      
        <propertyfile [...]>
            [...]
            <entry  key="build.release" type="int" default="0"/>
            [...]
        </propertyfile>
        [...]
    </target>
   
    <!-- ==========================================================
       
             inc.build.release
             Increment the release number
       
         ========================================================== -->
    <target name="inc.build.release" description="increment release number">
        <echo level="info">
    ***********************************************************************
    *
    * Incrementing the RELEASE number
    *
    ***********************************************************************
        </echo>    
        <propertyfile file="${build.version.file}" comment="Increment release number">
            <entry  key="build.number"  type="int" value="0"/>
            <entry  key="build.minor"   type="int" value="0"/>
            <entry  key="build.major"   type="int" default="0"/>
            <entry  key="build.release" type="int" default="0" operation="+"/>
            <entry  key="build.date" type="date" value="now" pattern="yyyy-MM-dd HH:mm:ss"/>
        </propertyfile>
       
        <antcall target="version"/>
    </target>

Now we can test everything out. First, check the version to verify that the format is now x.x.x.x

>ant version
Buildfile: build.xml
Trying to override old definition of datatype resources

version:
[propertyfile] Updating property file: C:\data\workspace\JavaPersistence\version.txt
     [echo]
     [echo]     ***********************************************************************
     [echo]     *
     [echo]     * version 0.0.6.0 built on 2009-05-11 12:21:59
     [echo]     *
     [echo]     ***********************************************************************
     [echo]

BUILD SUCCESSFUL

Next test incrementing with any target really ... I'll use compile.

>ant compile
Buildfile: build.xml
Trying to override old definition of datatype resources

inc.build.number:
[propertyfile] Updating property file: C:\data\workspace\JavaPersistence\version.txt

version:
[propertyfile] Updating property file: C:\data\workspace\JavaPersistence\version.txt
     [echo]
     [echo]     ***********************************************************************
     [echo]     *
     [echo]     * version 0.6.1 built on 2009-05-11 12:22:04
     [echo]     *
     [echo]     ***********************************************************************
     [echo]

init:

compile:
     [echo]
     [echo]     ***********************************************************************
     [echo]     *
     [echo]     * Compiling src/main/java to build/classes
     [echo]     *
     [echo]     ***********************************************************************
     [echo]
     [echo] copying files to build/classes

BUILD SUCCESSFUL

Now try resetting the build number (the last in the series).

>ant reset.build.number
Buildfile: build.xml
Trying to override old definition of datatype resources

reset.build.number:
[propertyfile] Updating property file: C:\data\workspace\JavaPersistence\version.txt

version:
[propertyfile] Updating property file: C:\data\workspace\JavaPersistence\version.txt
     [echo]
     [echo]     ***********************************************************************
     [echo]     *
     [echo]     * version 0.0.6.0 built on 2009-05-11 12:22:23
     [echo]     *
     [echo]     ***********************************************************************
     [echo]

BUILD SUCCESSFUL

Perfect. Next make sure we can increment the release number (and reset the rest to zero).

>ant inc.build.release
Buildfile: build.xml
Trying to override old definition of datatype resources

inc.build.release:
     [echo]
     [echo]     ***********************************************************************
     [echo]     *
     [echo]     * Incrementing the RELEASE number
     [echo]     *
     [echo]     ***********************************************************************
     [echo]
[propertyfile] Updating property file: C:\data\workspace\JavaPersistence\version.txt

version:
[propertyfile] Updating property file: C:\data\workspace\JavaPersistence\version.txt
     [echo]
     [echo]     ***********************************************************************
     [echo]     *
     [echo]     * version 1.0.0.0 built on 2009-05-11 12:22:42
     [echo]     *
     [echo]     ***********************************************************************
     [echo]

BUILD SUCCESSFUL

Alright. That takes care of the versioning stuff. Next, let's add a "deploy.delete" task and don't automatically delete the deploy directory on rebuild.

    <target name="zip" [...]">
        [...]  
        <!--
            call deploy.delete task manually
            <delete dir="${dir.deploy}"/>
        -->
       
        [...]
    </target>

Now run the 'deploy.delete', verify everything is gone and then run the 'zip' task a few times and take a look at the deploy directory.

>ant deploy.delete
>ant zip
>ant zip
>ant zip
>ant zip
>dir /B deploy\*
JavaPersistence-0.0.6.3.zip
JavaPersistence-0.0.6.3.zip.md5
JavaPersistence-0.0.6.4.zip
JavaPersistence-0.0.6.4.zip.md5
JavaPersistence-0.0.6.5.zip
JavaPersistence-0.0.6.5.zip.md5

Some think this is redundant, but with other projects I've found myself having to open a terminal and fire up ant just to get access to something like javadoc or a schema diagram. This way I can keep around a version (or a few). Deleting them is easy enough (through ant). If you don't like this behavior, it's easy to change, just make the zip task depend on deploy.delete.

Finally, just becaue I'm getting lazy, let's make a 'svni' task that saves some keystrokes when I want to run svn and inc.build.minor prior to a commit.

    <!-- ==========================================================
       
             svni
             increment the minor build number and run svn
       
         ========================================================== -->
    <target name="svni"
            depends="svn, inc.build.minor"
            description="increment the minor build number and run svn">

        <!-- no body necessary -->         
    </target>

Now test it

>ant svni
Buildfile: build.xml
Trying to override old definition of datatype resources

svn:
     [echo]
     [echo]     ***********************************************************************
     [echo]     *
     [echo]     * Preparing for repository commitment
     [echo]     *
     [echo]     ***********************************************************************
     [echo]
   [delete] Deleting directory C:\data\workspace\JavaPersistence\build

inc.build.minor:
     [echo]
     [echo]     ***********************************************************************
     [echo]     *
     [echo]     * Incrementing the MINOR build number
     [echo]     *
     [echo]     ***********************************************************************
     [echo]
[propertyfile] Updating property file: C:\data\workspace\JavaPersistence\version.txt

version:
[propertyfile] Updating property file: C:\data\workspace\JavaPersistence\version.txt
     [echo]
     [echo]     ***********************************************************************
     [echo]     *
     [echo]     * version 0.0.7.0 built on 2009-05-11 12:54:05
     [echo]     *
     [echo]     ***********************************************************************
     [echo]

svni:

BUILD SUCCESSFUL

Now commit