Check if environment variable has been set in Ant script
20,340
Solution 1
Isn't this as simple as:
<property environment="env"/>
<fail unless="env.FOO" message="FOO not set."/>
Solution 2
and the other thing you can do (additional to David's) is use
<isset property="env.Foo"/> instead of <equals />
Author by
digiarnie
Updated on February 14, 2020Comments
-
digiarnie about 4 years
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>
-
digiarnie almost 13 yearsYes you're right! I only just noticed that I had ${env.FOO} passed to the unless attribute! (i.e. I had the dollar and curly braces). In fact, the reason why I had that issue was that I actually tried to set a property with the name "bar" to be ${env.FOO} and then use the property "bar" in the unless part. Not sure how I'd keep the "bar" property if I wanted to.
-
Paul almost 10 yearsThis doesn't work in Ant 1.9.2. This target does not fail, even if FOO is not set:
<target name="paul"> <fail message="FOO not set."> <condition> <isset property="env.FOO"/> </condition> </fail> </target>
`