Reputation: 75
How to check whether a Boolean property of Apache Ant is set to “true” rather than merely defined?
I have used the <isset>
in the <condition>
and the ‘if’ in the <target>
, but if I set the property to “false” from a command-line, or from ant <antcall>
, the target’s if
condition is ignored. The target is still built as if the property evaluates to “true”. Similarly, when I have <target "build-installers" unless "skip_installers"> and run ant -Dskip_installers=false
, the target to build installers are skipped.
Upvotes: 1
Views: 1266
Reputation: 4782
Ant properties are read-only. Once you set them, you cannot change their value.
The <isset>
of the <condition>
and if
of the check whether a property is defined regardless of its value (“true” of “false”), if you give just a property name. When just specifying the property name, the build target is enabled when a property with that name is defined, even containing an empty string or false, and disabled when the property is not defined. For example, the following works, but there is no way to override the skip of the target by setting a negative value to a property.
<!-- build the target if the property is not defined; once defined, the value does not matter, be it “true” or “false” -->
<target name="build-installers" unless="skip_installers">
The unless
attribute would not help, since it is a negation of if
. First, it validates the condition as if
and then applies logical NOT operation. Therefore, the unless
checks whether the property is not defined, rather than it is false.
However, in Apache Ant version 1.8.0 or later, you can use property expansion instead. A property value of “true” (or “on” or “yes”) enables the build target, while “false” (or “off” or “no”) disables it. Other values are still assumed to be attribute names, so the build target will only be activated if the named attribute is defined.
<!-- build the target if the property is not defined or defined as “false” (or “off” or “no”) -->
<target name="build-installers" unless="${skip_installers}">
If you are unwilling to use a property expansion, you may use an internal property which is only set when the parent property is both defined and set to “true” – just use the combination of <isset>
(to avoid a warning in verbose mode) and <istrue>
conditions. Therefore, you will set a property “skip_installers_internal” only if “skip_installers” is both defined and set to “true” (or “on” or “yes”). Otherwise, the “skip_installers_internal” property will be undefined. Therefore, you will be able to use it as a condition
<!-- compatible with Ant 1.7.1 or earlier -->
<condition property="skip_installers_internal" value="true">
<and>
<isset property="skip_installers"/>
<istrue value="${skip_installers}"/>
</and>
</condition>
<target name="build-installers" unless="${skip_installers_internal}">
Upvotes: 1