Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if environment variable has been set in Ant script

Tags:

java

ant

What is the most efficient way of checking if an environment variable has been set prior to executing the rest of an Ant script?

Let's say my Ant script requires the environment variable "FOO" to be set. I got the following to work, but I was wondering whether there was a less convulated way of achieving the same result:

<property environment="env"/>
<property name="env.FOO" value=""/>

<target name="my-target">
    <condition property="foo.found">
        <not>
            <equals arg1="${env.FOO}" arg2=""/>
        </not>
    </condition>
    <fail unless="foo.found" message="FOO not set."/>
    <!-- do stuff here that uses the FOO environment variable -->
</target>
like image 559
digiarnie Avatar asked Jun 06 '11 01:06

digiarnie


2 Answers

Isn't this as simple as:

<property environment="env"/>
<fail unless="env.FOO" message="FOO not set."/>
like image 151
matt Avatar answered Nov 02 '22 02:11

matt


and the other thing you can do (additional to David's) is use

<isset property="env.Foo"/> instead of <equals />
like image 36
Op De Cirkel Avatar answered Nov 02 '22 03:11

Op De Cirkel