« Ant1.7 for Build, SmartFrog for Deployment | Main | Disappointed by SONY Network Camera »

Ant Tip: Checking for environment variable

Recently I had a defect report that the default value of a configurable parameter was nonsensical to the user, something like "${env.TEST_HOME}".

Of course, this is not nonsensical at all to someone familiar with Ant, though bit embarassing as it reveals an implementation detail: An Ant script is responsible for assigning the default value, taking the value from an environment varaible. When this varaible is not set then the default is set to the string "${env.TEST_HOME}".

So, how to fix this?

To add some checking and conditional processing, I changed the original fragment


<property environment="env"/>
<property="test.home" value="${env.TEST_HOME}"/>

to


<property environment="env" />
<property name="test.home.0" value="${env.TEST_HOME}"/>
<condition property="test.home" value="TO_BE_REPLACED">
<equals arg1="${test.home.0}" arg2="\${env.TEST_HOME}"/>
</condition>
<property name="test.home" value="${env.TEST_HOME}"/>

And though it worked, I didn't like the verbosity of this. So, I asked around for a better alternative. Not surprisingly, I got one from Antoine Levy-Lambert:


<property environment="env"/>
<property name="env.TEST_HOME" value="TO_BE_REPLACED"/>

Much more concise and elegant than my version!

About

This page contains a single entry from the blog posted on August 10, 2005 8:22 AM.

The previous post in this blog was Ant1.7 for Build, SmartFrog for Deployment.

The next post in this blog is Disappointed by SONY Network Camera.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33