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

I want to not call the target in the build.xml file in case there is a specific 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> 

The purpose of "someTarget" is fulfilled, is there an environment variable APP_MODE or not.

+6
ant
source share
2 answers

Documents for unless attribute say:

the name of the property that should not be set to accomplish this, or something related to false

So, in your case, you need to put the property name , not the property evaluation:

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

Notes

  • In Ant 1.7.1 and earlier, these attributes can only be property names.
  • Compared to Ant 1.8.0 , you can use the property extension instead; true (either enabled or yes) will enable the item, and false (either disabled or not) will disable it. Other values โ€‹โ€‹are still considered property names, so an element is included only if the named property is defined.

Link

+13
source share

If the attribute does not indicate in plain language that if the property is set, the task will not be completed. eg,

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

Here, if you call a clean target, it starts first, then its value is set. And if you want to call it again in the script, this does not mean that the property does not have to be set in order to complete the task.

0
source share

All Articles