Can Ant expand environment variables from a properties file

ant

I have a question regarding Ant and its treatment of environment variables.
To illustrate I have a small sample.

Given the Ant build file test.xml:

<project name="myproj" default="testProps">

    <property environment="env"/>

    <target name="testProps">
            <echo message="${env.MyEnvVar}"/>
            <echo message="${MY_PROPERTY}"/>
    </target>
</project>

And the properties file test.props:

MY_PROPERTY=${env.MyEnvVar}

Now set the environment variable MyEnvVar to some value (foo in my case) and run Ant using this command line:

ant -f test.xml -propertyfile test.props testProps

The output I get is:

[echo] foo
[echo] ${env.MyEnvVar}

What I would like to know is whether there is any way to structure the input properties file such that I get

[echo] foo
[echo] foo

That is, I would like to name an environment variable in the properties file which is replaced within the Ant script. Note – I know how to access environment variables directly (as is done here). What I need to do is make use of a set of Ant scripts that expect one collection of properties in an environment that defines the same properties using different names. Thus the thought of "bridging" them in a properties file.

I am using Ant version 1.6.5.

Best Answer

You need to read the test.props property file after the environment - you could do so using another property task, i.e. add

<property file="test.props" />

after your existing property environment task.

In full:

<property environment="env" />
<property file="test.props" />

<target name="testProps">
    <echo message="${env.MyEnvVar}"/>
    <echo message="${MY_PROPERTY}"/>
</target>

When you supply the properties file on the command line this gets processed before the content of the build, but at that time ${env.MyEnvVar} is not yet set.

Related Topic