How to use ant Script to Reset BuildNumber?

To use this code, you need to have the file build.number containging:

major.number=1

minor.number=0
hotfix.number=0

revision.number=0
continuous.number=0

Then the following 3 targets:

  <taskdef resource=”net/sf/antcontrib/antlib.xml”/>
<taskdef name=”unset” classname=”ise.antelope.tasks.Unset”/>

    <target name=”initBuildNum” description=”Get current build number properties”>
<property file=”build.number”/>
<var name=”next.major” value=”${major.number}”/>
<var name=”next.minor” value=”${minor.number}”/>
<var name=”next.hotfix” value=”${hotfix.number}”/>
<var name=”next.revision” value=”${revision.number}”/>
<var name=”next.continuous” value=”${continuous.number}”/>
</target>

    <target name=”getBuildNum”>
<switch value=”${increment}”>
<case value=”rebuild”/>
<case value=”major”>
<!–Increment major, minor to 1, hotfix to 0, revision to 1–>
<math result=”next.major” operand1=”${next.major}” operation=”+” operand2=”1″ datatype=”int”/>
<var name=”next.minor” value=”1″ />
<var name=”next.hotfix” value=”0″ />
<var name=”next.revision” value=”1″ />
</case>
<case value=”minor”>
<!–Major stays the same, minor increments, hotfix goes to 0, revision to 1–>
<math result=”next.minor” operand1=”${next.minor}” operation=”+” operand2=”1″ datatype=”int”/>
<var name=”next.hotfix” value=”0″ />
<var name=”next.revision” value=”1″ />
</case>
<case value=”hotfix”>
<!–Major stays the same, minor stays the same, hotfix increments, revision goes to 1–>
<math result=”next.hotfix” operand1=”${next.hotfix}” operation=”+” operand2=”1″ datatype=”int”/>
<var name=”next.revision” value=”1″ />
</case>
<case value=”continuous”>
<!–For continuous integration don’t change anything but 5th build digit–>
<math result=”next.continuous” operand1=”${next.continuous}” operation=”+” operand2=”1″ datatype=”int”/>
</case>
<case value=”contReset”>
<!–Continuous build, but they want the 5th digit reset (i.e. new week)–>
<var name=”next.continuous” value=”1″/>
</case>
<default>
<!–Update revision number only, they didn’t ask for anything special–>
<math result=”next.revision” operand1=”${next.revision}” operation=”+” operand2=”1″ datatype=”int”/>
</default>
</switch>
</target>

    <target name=”setBuildNumber” depends=”initBuildNum,getBuildNum”>
<!–First save the build number properties–>
<propertyfile file=”build.number”>
<entry key=”major.number” value=”${next.major}”/>
<entry key=”minor.number” value=”${next.minor}”/>
<entry key=”hotfix.number” value=”${next.hotfix}”/>
<entry key=”revision.number” value=”${next.revision}”/>
<entry key=”continuous.number” value=”${next.continuous}”/>
</propertyfile>
<!–Unset the properties so that we can change their values–>
<unset name=”major.number”/>
<unset name=”minor.number”/>
<unset name=”hotfix.number”/>
<unset name=”revision.number”/>
<unset name=”continuous.number”/>
<property file=”build.number”/>
<!–set the full.buildnumber property to be used by the build–>
<if>
<or>
<equals arg1=”${increment}” arg2=”continuous”/>
<equals arg1=”${increment}” arg2=”contReset”/>
</or>
<then>
<property name=”full.buildnumber” value=”${major.number}.${minor.number}.${hotfix.number}.${revision.number}.${continuous.number}”/>
</then>
<else>
<property name=”full.buildnumber” value=”${major.number}.${minor.number}.${hotfix.number}.${revision.number}”/>
</else>
</if>
<echo>${full.buildnumber}</echo>
</target>

Add these three targets to your ant script.  Put a “depends” to “setBuildNumber” at invocation of your ant script.  Depending on what you want to do, you’ll set different vars on the way in as follows:

Usage:
To build and increment revision number only:
>Ant
To move to the next major build number:
>Ant -Dincrement=major
To move to the next minor build number:
>Ant -Dincrement=minor
To move to the next hotfix build number:
>Ant -Dincrement=hotfix

  To do a continuous integration build incrementing 5th digit:
>Ant -Dincrement=continuous
To do a continuous integration build resetting 5th digit to 1:
>Ant -Dincrement=contReset

Note that if you don’t set the value of increment, the build number will only have 4 digits.  The var that holds the build number after this is called is ${full.buildnumber}.

Tagged : / / / / / / / / / / / / / / / /