Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ant

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.

like image 950
Volodymyr Bezuglyy Avatar asked Oct 20 '10 12:10

Volodymyr Bezuglyy


People also ask

How do I run a specific target in Ant?

Enclose the task name in quotes. Targets beginning with a hyphen such as "-restart" are valid, and can be used to name targets that should not be called directly from the command line. For Ants main class every option starting with hyphen is an option for Ant itself and not a target.

What is the default target in Ant?

the default target to use when no target is supplied. No; however, since Ant 1.6. 0, every project includes an implicit target that contains any and all top-level tasks and/or types. This target will always be executed as part of the project's initialization, even when Ant is run with the -projecthelp option.

What is Ant environment?

Ants play an important role in the environment. Ants turn and aerate the soil, allowing water and oxygen to reach plant roots. Ants take seeds down into their tunnel to eat the nutritious elaiosomes that are part of the seed. These seeds often sprout and grow new plants (seed dispersal).


1 Answers

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

  • if/unless on the ant manual
like image 76
skaffman Avatar answered Sep 22 '22 04:09

skaffman