Volodymyr Bezuglyy
Volodymyr Bezuglyy

Reputation: 16795

How to call some Ant target only if some environment variable was not set?

I would like to not call a target in build.xml in the case that there is a certain environment variable.

Using Ant 1.7.0, the following code does not work:

<property environment="env"/>
<property name="app.mode" value="${env.APP_MODE}"/>

<target name="someTarget" unless="${app.mode}">    
   ...
</target>

<target name="all" description="Creates app">
   <antcall target="someTarget" />
</target>

Target "someTarget" executes whether there is the environment variable APP_MODE or not.

Upvotes: 8

Views: 14635

Answers (2)

skaffman
skaffman

Reputation: 403461

The docs for the unlessattribute say:

the name of the property that must not be set in order for this target to execute, or something evaluating to false

So in your case, you need to put the name of the property, rather than an evaluation of the property:

<target name="someTarget" unless="app.mode">    
   ...
</target>

Notes

  • In Ant 1.7.1 and earlier, these attributes could only be property names.
  • As of Ant 1.8.0, you may instead use property expansion; a value of true (or on or yes) will enable the item, while false (or off or no) will disable it. Other values are still assumed to be property names and so the item is enabled only if the named property is defined.

Reference

Upvotes: 15

SteveScm
SteveScm

Reputation: 565

Unless attribute suggest in simple language that if property is set then the task would not be get executed. for ex.

<target name="clean" unless="clean.not">
 <delete dir="${src}" />
<property name="clean.not" value="true" />
 <delete dir="${dest}" />
</target>

Here , if you call clean target , it gets executed first then its value is set. And if you want to call it again in script then it would not as property must not be set in order to get the task executed.

Upvotes: 0

Related Questions