Thursday, April 5, 2012

Ant script to replace tokens (VERSION) in a file (build.properties)

Version of a product ever changes for main releases, SP releases, hotfix releases, etc. We can't hard-code version value in source files. One of the effective solution to tackle this is to use tokens in the place of version values and replace it before the start of build. The most common tool used to make string replacements are scripts written in perl/shell. Though scripts works well, but why to depend on scripts which are closely tied with Unix systems, to perform token replacement operations on platform independent Java build systems. 


 Java builds are usually managed through Ant/Maven. If perl/shell scripts are not stored along with Java build system, then if anyone checkouts only java code from source control, there build may fail due to improper version characters.


Here is an illustration of performing token replacement using Ant script.


Input file: build.properties file with tokens for various version formats like VERSION_UNDERSCORE, VERSION_NODOT, etc

$ cat build.properties
# KMJC Version properties
version=VERSION_NODOT
dot.version=VERSION_DOUBLE
dot.version.double=VERSION_DOUBLE
dot.version.string=VERSION_DOUBLE
underscore.version=VERSION_UNDERSCORE


After string replacement it should become like


$ cat build.properties
# KMJC Version properties
version=40
dot.version=4.0
dot.version.double=4.0
dot.version.string=4.0
underscore.version=4_0


Ant script

$ cat build.xml
<project name="begin" default="must" basedir="." >
        <condition property="noVersion" value="true">
                <not>
                        <isset property="VERSION"/>
                </not>
        </condition>
        <target name="checkVersion" if="noVersion">
                <echo message="ERROR: VERSION property not defined. Supply it through -DVERSION argument" />
                <echo message="ant -DVERSION=version-number" />
                <echo message="EX: ant -DVERSION=4.0" />
                <fail message="version not supplied"/>
        </target>
        <target name="must" depends="checkVersion" >
                <echo> VERSION=${VERSION} </echo>
                <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
                <propertyregex property="VERSION_UNDERSCORE" input="${VERSION}" regexp="\." replace="_" global="true" />
                <propertyregex property="VERSION_NODOT" input="${VERSION}" regexp="\." replace="" global="true" />
                <echo> VERSION_UNDERSCORE=${VERSION_UNDERSCORE} </echo>
                <echo> VERSION_NODOT=${VERSION_NODOT} </echo>
                <replace file="build.properties" token="VERSION_DOUBLE" value="${VERSION}"/>
                <replace file="build.properties" token="VERSION_UNDERSCORE" value="${VERSION_UNDERSCORE}"/>
                <replace file="build.properties" token="VERSION_NODOT" value="${VERSION_NODOT}"/>
        </target>
</project>

Usage:
  1) If VERSION Variable is not defined. It will fail
      $ ant
Buildfile: C:\cygwin\home\guruss1\ant\build.xml


checkVersion:
     [echo] ERROR: VERSION property not defined. Supply it through -DVERSION argument
     [echo] ant -DVERSION=version-number
     [echo] EX: ant -DVERSION=4.0


BUILD FAILED
C:\cygwin\home\guruss1\ant\build.xml:11: version not supplied

2) Proper usage
   $ ant -DVERSION=4.1
Buildfile: C:\cygwin\home\guruss1\ant\build.xml

checkVersion:

must:
     [echo]  VERSION=4.1
     [echo]  VERSION_UNDERSCORE=4_1
     [echo]  VERSION_NODOT=41

BUILD SUCCESSFUL


Requirement: propertyregex task needs ant-contrib.jar. Download it and place it under lib directory of Ant installation and add the line <taskdef resource="net/sf/antcontrib/antcontrib.properties"/> to build.xml file
 Alternatively ant-contrib.jar can also be placed in directory of you choice and add line <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="${ant-contrib.jar}" /> to build.xml

No comments: