2
votes

Ant script

    <property environment="env">
    <if> <equals arg1="${env.PARA} arg2=""/>
    <then>
        <property name="${env.PARA}" value="abc"/>
    <then>
    <if>

    <echo message="${env.PARA}">

Output is

${env.PARA}

Expected output is

abc

I have not defined the environment variable PARA in dos. How to get the expected output.

Note : I am using ant 1.8.2 and antcontrib in windows 7

3

3 Answers

1
votes

The following is the "ANT way" to conditionally set properties.

<project name="test" default="run">

    <property environment="env"/>

    <target name="check-prop" unless="${env.PARA}">
        <property name="env.PARA" value="abc"/>
    </target>

    <target name="run" depends="check-prop">
        <echo message="${env.PARA}"/>
    </target>

</project>

Testing

I'm a linux user, however, it should work the same on windows.

No environment variable

$ ant
Buildfile: /home/mark/tmp/build.xml

check-prop:

run:
     [echo] abc

BUILD SUCCESSFUL

environment variable

$ (export PARA="hello world"; ant)
Buildfile: /home/mark/tmp/build.xml

check-prop:

run:
     [echo] hello world

BUILD SUCCESSFUL
3
votes

Ant properties, once set, are immutable. You can therefore just set the property.

If it had been already set via environment variable, it will have a value and will not be set to "abc". If it wasn't set via environment variable, your <property/> statement will be applied.

<property name="env.PARA" value="abc"/>

<echo message="${env.PARA}"/>
2
votes

The code below will set the 'port' property to the environment variable 'CATALINA_PORT' if it is defined and to '8080' otherwise:

<property environment="env" />
<condition property="port" value="${env.CATALINA_PORT}" else="8080">
    <isset property="env.CATALINA_PORT"/>
</condition>