Now let's set up a simple method of implementing versioning to our project (not based on svn). In the build.properties file add

    build.version.file = version.txt

Now add these four targets to the build file

    <!-- ==========================================================
       
             inc.build.number
             Increment the local build number
       
         ========================================================== -->
    <target name="inc.build.number">
        <echo level="verbose">
    ***********************************************************************
    *
    * Incrementing the build number
    *
    ***********************************************************************
        </echo>    
        <propertyfile file="${build.version.file}" comment="Version Information">
            <entry  key="build.number" type="int" default="0" operation="+"/>
            <entry  key="build.minor" type="int" default="0"/>
            <entry  key="build.major" 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}"/>
       
        <property name="version" value="${build.major}.${build.minor}.${build.number}"/>
       
    </target>
   
    <!-- ==========================================================
       
             inc.build.minor
             Increment the minor build number
       
         ========================================================== -->
    <target name="inc.build.minor">
        <echo level="info">
    ***********************************************************************
    *
    * Incrementing the MINOR build number
    *
    ***********************************************************************
        </echo>
        <propertyfile file="${build.version.file}" comment="Increment minor version number">
            <entry  key="build.number" type="int" value="0"/>
            <entry  key="build.minor" type="int" default="0" operation="+"/>
            <entry  key="build.major" type="int" default="0"/>
            <entry  key="build.date" type="date" value="now" pattern="yyyy-MM-dd HH:mm:ss"/>
        </propertyfile>
       
        <antcall target="version"/>
    </target>

    <!-- ==========================================================
       
             inc.build.major
             Increment the major build number
       
         ========================================================== -->
    <target name="inc.build.major">
        <echo level="info">
    ***********************************************************************
    *
    * Incrementing the MAJOR build number
    *
    ***********************************************************************
        </echo>    
        <propertyfile file="${build.version.file}" comment="Increment major version 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" operation="+"/>
            <entry  key="build.date" type="date" value="now" pattern="yyyy-MM-dd HH:mm:ss"/>
        </propertyfile>
       
        <antcall target="version"/>
    </target>
   
    <!-- ==========================================================
       
             version
             Print version information for project (major.minor.build)
       
         ========================================================== -->
    <target name="version" description="print version information">
        <propertyfile file="${build.version.file}" comment="Version Information">
            <entry  key="build.number"  type="int" default="0"/>
            <entry  key="build.minor"   type="int" default="0"/>
            <entry  key="build.major"   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}"/>
        <echo level="info">
    ***********************************************************************
    *
    * version ${build.major}.${build.minor}.${build.number} built on ${build.date}
    *
    ***********************************************************************
        </echo>
    </target>

Now test with

> ant version
Buildfile: build.xml

version:
[propertyfile] Creating new property file: C:\data\workspace\JavaPersistence\version.txt
     [echo]
     [echo]     ***********************************************************************
     [echo]     *
     [echo]     * version 0.0.0 built on 2009-05-06 16:28:56
     [echo]     *
     [echo]     ***********************************************************************
     [echo]

BUILD SUCCESSFUL

Check to see that JP/version.txt has been created and looks something like this:

#Version Information
#Wed May 06 16:28:56 EDT 2009
build.minor=0
build.number=0
build.date=2009-05-06 16\:28\:56
build.major=0

Now try incrementing the build with

>ant inc.build.number version
Buildfile: build.xml

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.0.1 built on 2009-05-06 16:31:11
     [echo]     *
     [echo]     ***********************************************************************
     [echo]

BUILD SUCCESSFUL

Try updating the minor version

>ant inc.build.minor
Buildfile: build.xml

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.1.0 built on 2009-05-06 16:32:06
     [echo]     *
     [echo]     ***********************************************************************
     [echo]

BUILD SUCCESSFUL

Try updating the major version

>ant inc.build.major
Buildfile: build.xml

inc.build.major:
     [echo]
     [echo]     ***********************************************************************
     [echo]     *
     [echo]     * Incrementing the MAJOR 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 1.0.0 built on 2009-05-06 16:32:42
     [echo]     *
     [echo]     ***********************************************************************
     [echo]

BUILD SUCCESSFUL

Finally, let's use the 'init' target to update the build number since it's called everytime we do anything.

    <target name="init" depends="inc.build.number,version">
        ...
    </target>

Now see what happens when you run any target

>ant compile

Buildfile: build.xml

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 1.0.1 built on 2009-05-06 16:34:15
     [echo]     *
     [echo]     ***********************************************************************
     [echo]

init:

compile:
     [echo]
     [echo]     ***********************************************************************
     [echo]     *
     [echo]     * Compiling src/main/java to build/classes
     [echo]     *
     [echo]     ***********************************************************************
     [echo]
    [mkdir] Created dir: C:\data\workspace\JavaPersistence\build\classes
    [javac] Compiling 2 source files to C:\data\workspace\JavaPersistence\build\classes
     [echo] copying files to build/classes

BUILD SUCCESSFUL

Perfect. Now let's delete the version file, run svn, and let ant create a version 0.0.0 file

>ant svn version
Buildfile: build.xml

svn:
     [echo]
     [echo]     ***********************************************************************
     [echo]     *
     [echo]     * Preparing for repository commitment
     [echo]     *
     [echo]     ***********************************************************************
     [echo]

clean:
     [echo]
     [echo]     ***********************************************************************
     [echo]     *
     [echo]     * Cleaning directories
     [echo]     *   build/classes
     [echo]     *
     [echo]     ***********************************************************************
     [echo]

version:
[propertyfile] Creating new property file: C:\data\workspace\JavaPersistence\version.txt
     [echo]
     [echo]     ***********************************************************************
     [echo]     *
     [echo]     * version 0.0.0 built on 2009-05-06 16:36:44
     [echo]     *
     [echo]     ***********************************************************************
     [echo]

BUILD SUCCESSFUL

commit