define ant property from environment with default value

Maxim Veksler picture Maxim Veksler · Jun 1, 2009 · Viewed 51.1k times · Source

I would like my build script to act properly for release and development environments.

For this I would like to define a property in ant, call it (e.g.) fileTargetName

fileTargetName will get it's value from the environment variable RELEASE_VER if it's available, if it is not available it will get the default value of dev

Help with ant <condition><value></condition> & <property> to get it working is appreciated.

Answer

Michael Myers picture Michael Myers · Jun 1, 2009

An example from the Ant documentation of how to get an environment variable into a property:

<property environment="env"/>
<echo message="Number of Processors = ${env.NUMBER_OF_PROCESSORS}"/>
<echo message="ANT_HOME is set to = ${env.ANT_HOME}"/>

In your case, you would use ${env.RELEASE_VER}.

Then for the conditional part, the documentation here says that there are three possible attributes:

Attribute  Description                                             Required 
property   The name of the property to set.                        Yes 
value      The value to set the property to. Defaults to "true".   No 
else       The value to set the property to if the condition       No
           evaluates to false. By default the property will
           remain unset. Since Ant 1.6.3

Putting it together:

<property environment="env"/>
<condition property="fileTargetName" value="${env.RELEASE_VER}" else="dev">
    <isset property="env.RELEASE_VER" />
</condition>