views:

3290

answers:

3

Hello Friends,

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

For this I would like to defined a property in ant, call it (for ex.) 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.

+9  A: 

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>
Michael Myers
Yesterday I would not have been able to answer this question, but SO has forced me to research. Hooray for StackOverflow!
Michael Myers
Thank you! A strange behavior of ant: If environment parameter is not set, doing an echo on ${env.ANT_HOME} will print "${env.ANT_HOME}". The default ant invocation does not set it (at least on this machine: RH WS 5, bash)
Maxim Veksler
A: 

I'm sure there are easier ways than this, but how about:

<project name="example" default="show-props">

    <property environment="env" />

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

    <condition property="fileTargetName" value="dev">
        <not>
            <isset property="env.RELEASE_VER" />
        </not>
    </condition>

    <target name="show-props">
        <echo>property is ${fileTargetName}</echo>
    </target>

</project>
toolkit
Didn't know about the else attribute to condition - @mmyers FTW
toolkit
I guess having to look everything up in the manual has its benefits. :)
Michael Myers
+4  A: 

You don't need to use a <condition> for this. Properties in ant are immutable, so you can just use this:

<property environment="env"/>
<property name="env.RELEASE_VER" value="dev"/>

If the RELEASE_VER environment variable is set, then the property will get its value from the environment and the second <property> statement will have no effect. Otherwise, the property will be unset after the first statement, and the second statement will set its value to "dev".

Jason Day
Not quite the same effect, but I guess you could just add another <property> to give it whatever name you want, right?
Michael Myers
Sure, you could add a third line like <property name="release.version" value="${env.RELEASE_VER}"/>.
Jason Day
Thanks, that's a nice twist on the issue. Because of readability reasons I will stick with the condition.
Maxim Veksler
To each his own, of course. But property immutability is a fairly common idiom in the build files I've come across.
Jason Day