Automatic Version Numbering in Web Applications with Hudson (Add-On)

In the 3 parts of the tutorial (part 1, part 2, part3) we setup a Netbeans/Web-Project/Hudson/ZK environment that creates and reads automatically version numbers. Unfortunately while building outside HUDSON, or better with your local Netbeans IDE, ANT can’t read the HUDSON variables, resulting in a Manifest like this:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 14.2-b01 (Sun Microsystems Inc.)
builduser: sven
version: ${env.BUILD_VERSION}
id: ${env.BUILD_ID}
tag: ${env.BUILD_TAG}
server: ${env.BUILD_URL}

And potentially the website will display ${env.BUILD_VERSION} as version. Not very professional. Of course a local build should not find its way to production system, but at least it should state it properly.

After joggling with ANT’s condition I came to this solution for an updated build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="VersionServletDemo" default="default" basedir=".">
 <description>Builds, tests, and runs the project VersionServletDemo.</description>
 <import file="nbproject/build-impl.xml"/>

 <target name="-post-compile">
 <property environment="env" />

 <!-- Set timestamp for LOCAL BUILD -->
 <tstamp>
 <format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" />
 </tstamp>

 <!-- Check if local build or HUDSON environment -->
 <condition property="version" value="${env.BUILD_NUMBER}" else="LOCAL BUILD">
 <isset property="env.BUILD_NUMBER" />
 </condition>

 <condition property="id" value="${env.BUILD_ID}" else="${TODAY}">
 <isset property="env.BUILD_ID" />
 </condition>

 <condition property="tag" value="${env.BUILD_TAG}" else="LOCAL BUILD">
 <isset property="env.BUILD_TAG" />
 </condition>

 <condition property="server" value="${env.HUDSON_URL}" else="LOCAL BUILD">
 <isset property="env.HUDSON_URL" />
 </condition>

 <manifest file="${build.web.dir}/META-INF/MANIFEST.MF">
 <attribute name="builduser" value="${user.name}"/>
 <attribute name="version" value="${version}"/>
 <attribute name="id" value="${id}"/>
 <attribute name="tag" value="${tag}"/>
 <attribute name="server" value="${server}"/>
 </manifest>
 </target>

</project>

creating this MANIFEST.MF for a local build

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 14.2-b01 (Sun Microsystems Inc.)
builduser: sven
version: LOCAL BUILD
id: 2010-03-29 08:32:56
tag: LOCAL BUILD
server: LOCAL BUILD

and Hudson creates this (as before)

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 14.2-b01 (Sun Microsystems Inc.)
builduser: sven
version: 8
id: 2010-03-29_08-32-11
tag: hudson-VersionDemo-8
server: http://localhost:8080/

Note:

  1. You can’t use conditions in the ANT manifest job ! (Thats the reason for using separate variables)
  2. You can’t set or override environment variables from ANT (except triggering an external shell which does not really help your cross-platform consistency)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s