Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Ant expand environment variables from a properties file?

Tags:

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.

like image 908
sfitts Avatar asked Jul 13 '10 20:07

sfitts


People also ask

Can we use environment variables in properties file?

You can put environment variables in your properties file, but Java will not automatically recognise them as environment variables and therefore will not resolve them. In order to do this you will have to parse the values and resolve any environment variables you find.

How do I override property value in ant?

Ant Properties are set once and then can never be overridden. That's why setting any property on the command line via a -Dproperty=value will always override anything you've set in the file; the property is set and then nothing can override it. This way: Anything set at the command line takes precedence over build.

What are ant properties?

Properties are key-value pairs where each value is associated to a key. The property is used to set value which can be accessed anywhere in the buildfile. Once a property is set, it cannot be changed. Apache Ant provides <property> tag which can be used to set property.


1 Answers

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.

like image 143
martin clayton Avatar answered Oct 08 '22 01:10

martin clayton