Siddesh BG's Build Release Config mgmt Blog

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Thursday, 5 April 2012

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

Posted on 04:05 by Unknown
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

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in ANT | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Installing and configuring Fortify on Linux and Windows machines
    Installing Fortify on Linux (RHEL 5 32 bit) Download Fortify archive Fortify-360-2.6.5-Analyzers_and_Apps-Linux-x86.tar.gz and extract it to...
  • Solution to Project Euler Problem 10 - Find the sum of all the primes below two million
    http://projecteuler.net/problem=10 Problem The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two mi...
  • How to know architecture of a AIX machine?
    We can easily know the architecture type in Linux or Solaris systems with the help of "uname" command. But uname command in AIX do...
  • fortifyclient uploadFPR An internal error has occurred
    When you try to upload a .fpr file to Fortify 360 server and you get the below mentioned error. Then, this blog provides one of the route ca...
  • FeatureBranch v/s FeatureToggle v/s BranchByAbstraction patterns
    This blog is excerpts from Martin Fowler blog and Continuous Delivery book (Jez Humble, David Farley). It explains about the concepts of 1) ...
  • Posting a JIRA bug using Perl Mechanize
    Perl provides modules which can be used as command line browser to automate tasks dependent on web pages. Among them LWP and mechanize are i...
  • Tweak single instance of tinderbox to work on two different perforce servers
    Tinderbox is a tool developed by Mozilla, which collects build logs and presents the logs and the result it in a nice, clear and concise way...
  • AIX: make: 1254-055 Dependency line needs colon or double
    We get this compilation issue "make: 1254-055 Dependency line needs colon or double" while compiling C/C++ code in AIX machines. I...
  • 0509-036 Cannot load program p4 because of the following errors
    Here is the full description of error ............ bash-3.00# p4 info exec(): 0509-036 Cannot load program p4 because of the following error...
  • Perforce - can't edit exclusive file already opened
    In perforce, whenever a binary file like doc, xls or ppt files are checked out, it is opened in exclusive lock mode. So no other person can ...

Categories

  • AIX
  • AIX ssh
  • ANT
  • apache
  • appliance
  • awk
  • branching
  • build-failures
  • cgi-perl
  • code-signing
  • commands
  • continuous Integration
  • cvs
  • cygwin
  • DNS
  • Drupal
  • EPM
  • euler
  • Fortify
  • hadoop
  • hpux
  • html
  • InstallShield
  • iptables
  • iso
  • jenkins-hudson
  • Jira
  • kiwi
  • linux
  • Makefile
  • maven
  • Miscellaneous
  • mysql
  • nexus
  • NFS
  • package
  • Perforce
  • Perl
  • php
  • rbuilder
  • rpath
  • rpm
  • rsync
  • Solaris
  • ssh
  • SuseStudio
  • tinderbox
  • unix
  • Visual studio 2008
  • vmware
  • war
  • webserver
  • wget
  • windows
  • xterm

Blog Archive

  • ►  2013 (12)
    • ►  December (1)
    • ►  July (2)
    • ►  April (2)
    • ►  March (2)
    • ►  February (3)
    • ►  January (2)
  • ▼  2012 (43)
    • ►  December (2)
    • ►  November (1)
    • ►  October (4)
    • ►  September (7)
    • ►  August (5)
    • ►  July (4)
    • ►  June (2)
    • ►  May (3)
    • ▼  April (4)
      • Using BUILD_LOG_REGEX in jenkins email notification
      • Using unix find Command to delete files older than...
      • Using wget to download files recursively from http...
      • Ant script to replace tokens (VERSION) in a file (...
    • ►  March (3)
    • ►  February (1)
    • ►  January (7)
  • ►  2011 (23)
    • ►  December (4)
    • ►  November (9)
    • ►  October (4)
    • ►  September (1)
    • ►  June (2)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
  • ►  2010 (15)
    • ►  December (2)
    • ►  November (1)
    • ►  September (3)
    • ►  April (1)
    • ►  February (6)
    • ►  January (2)
  • ►  2009 (28)
    • ►  November (5)
    • ►  October (3)
    • ►  September (2)
    • ►  August (1)
    • ►  July (1)
    • ►  June (5)
    • ►  May (3)
    • ►  April (1)
    • ►  February (2)
    • ►  January (5)
  • ►  2008 (20)
    • ►  December (6)
    • ►  November (3)
    • ►  October (1)
    • ►  September (1)
    • ►  July (8)
    • ►  June (1)
Powered by Blogger.

About Me

Unknown
View my complete profile