Conditions are nested elements of the condition, if and waitfor tasks.
The <not>
element expects exactly one other condition to be
nested into this element, negating the result of the condition. It doesn't have any
attributes and accepts all nested elements of the condition task as nested elements
as well.
The <and>
element doesn't have any attributes and accepts an
arbitrary number of conditions as nested elements. This condition is true if all of
its contained conditions are, conditions will be evaluated in the order they have
been specified in the build file.
The <and>
condition has the same shortcut semantics as the
&&
operator in some programming languages, as soon as
one of the nested conditions is false, no other condition will be evaluated.
The <or>
element doesn't have any attributes and accepts an
arbitrary number of conditions as nested elements. This condition is true if at
least one of its contained conditions is, conditions will be evaluated in the order
they have been specified in the build file.
The <or>
condition has the same shortcut semantics as the
||
operator in some programming languages, as soon as one of
the nested conditions is true, no other condition will be evaluated.
The <xor>
element performs an exclusive or on all nested elements,
similar to the ^ operator in PHP. It only evaluates to true if an odd number of nested
conditions are true. There is no shortcutting of evaluation, unlike the <and> and <or> tests.
It doesn't have any attributes and accepts all nested elements of the condition task as nested elements
as well.
Test whether the current operating system is of a given type.
Table 5.1: OS Attributes
Attribute | Description | Required |
---|---|---|
family | The name of the operating system family to expect. | Yes |
Supported values for the family attribute are:
windows (for all versions of Microsoft Windows)
mac (for all Apple Macintosh systems)
unix (for all Unix and Unix-like operating systems)
Note: machines running OSX match on the mac
and unix
families!
To test for Macs that don't run a Unix-like OS, use the following code:
<condition property="isMacOsButNotMacOsX"> <and> <os family="mac"/> <not> <os family="unix"/> </not> </and> </condition>
Tests whether the two given Strings are identical
Table 5.2: equals Attributes
Attribute | Description | Required |
---|---|---|
arg1 | First string to test. | Yes |
arg2 | Second string to test. | Yes |
casesensitive | Perform a case sensitive comparison. Default is true. | No |
trim | Trim whitespace from arguments before comparing them. Default is false. | No |
Compares two given versions
Table 5.3: versioncompare Attributes
Attribute | Description | Required |
---|---|---|
version | The version you want to compare | Yes |
desiredVersion | The version you want to compare against | Yes |
operator | The operator to use for version comparison. Default
is >= . | No |
debug | Turns on debug mode, that echoes the comparion message. Default is false. | No |
<versioncompare version="${aProperty}" desiredVersion="1.3" operator="gt" />
This condition internally uses PHP version_compare(). Operators and behavior are the same.
Condition to wait for a HTTP request to succeed.
Attributes are:
Table 5.4: http Attributes
Attribute | Description | Required |
---|---|---|
url | The URL of the request. | Yes |
errorsBeginAt | Number at which errors begin at. - Default: 400 | No |
requestMethod | Sets the method to be used when issuing the HTTP request. - Default: GET | No |
followRedirects | Whether redirects sent by the server should be followed. - Default: true | No |
quiet | Set quiet mode, which suppresses warnings and errors. Default is false | No |
<http url="http://url.to.test" errorsBeginAt="404" />
PDOSQLExecTask can also
be used as condition. Returns true
when the
connection to a database succeeds, and false
otherwise. This condition requires the
PDO extension
to work properly.
Table 5.5: PDOSQLExec condition attributes
Attribute | Description | Required |
---|---|---|
url
| The PDO Data Source Name (DSN).
| Yes |
userid
| The username for current DSN. | No |
password
| The password for current DSN. | No |
This is a typical use case for PDOSQLExec
condition:
<target name="wait-for-mysql"> <waitfor timeoutproperty="mysql.timeout" maxwait="60" maxwaitunit="second"> <pdosqlexec url="mysql:host=localhost;port=3306" userid="${db.username}" password="${db.password}"/> </waitfor> <fail if="mysql.timeout">Cannot reach database</fail> </target>
If you also want to check if a specific schema exists, you can
include the schema's name in your url
:
<pdosqlexec url="mysql:host=127.0.0.1;port=3306;dbname=foo" userid="${db.username}" password="${db.password}"/>
This condition uses PDO behind the scenes. Therefore, if you have installed the appropriate driver you should also be able to reach many other DBMS. For example, for a PostgreSQL database:
<pdosqlexec url="pgsql:host=localhost;port=5432;dbname=bar" userid="${db.username}" password="${db.password}"/>
You should never hard-code sensitive data in your
buildfile
, you could use an unversioned
property file
instead. Also, be careful when using verbose
or debug
mode since you can expose sensitive
data.
Condition to test for a (tcp) listener on a specified host and port.
Table 5.6: socket Attributes
Attribute | Description | Required |
---|---|---|
server | The hostname or ip address of the server. | Yes |
port | The port number of the server. | Yes |
<socket server="localhost" port="80" />
Condition returns true if selected partition has the requested space, false otherwise.
Table 5.7: hasfreespace Attributes
Attribute | Description | Required |
---|---|---|
partition | Absolute path to the partition/device to check. | Yes |
needed | The amount of free space required. Examples: 250M ,
10G , 1T . | Yes |
File size can be written using IEC and SI suffixes, bytes are assumed when suffix is not specified. The following suffixes (case-insensitive) are supported:
Table 5.8: Supported file size suffixes
Standard | Suffixes | Equivalence |
---|---|---|
IEC | B .
| 1 byte |
K , Ki , KiB ,
kibi , kibibyte .
| 1024 bytes | |
M , Mi , MiB ,
mebi , mebibyte .
| 1024 kibibytes | |
G , Gi , GiB ,
gibi , gibibyte .
| 1024 mebibytes | |
T , Ti , TiB ,
tebi , tebibyte .
| 1024 gibibytes | |
SI |
kB , kilo , kilobyte .
| 1000 bytes |
MB , mega , megabyte .
| 1000 kilobytes | |
GB , giga , gigabyte .
| 1000 megabytes | |
TB , tera , terabyte .
| 1000 gigabytes |
On Unix-like platforms:
<hasfreespace partition="/" needed="250M" />
On Windows:
<hasfreespace partition="c:" needed="10M" />
This condition internally uses PHP disk_free_space().
Test whether a given property has been set in this project.
Table 5.9: isset Attributes
Attribute | Description | Required |
---|---|---|
property | The name of the property to test. | Yes |
Tests whether a string contains another one.
Table 5.10: contains Attributes
Attribute | Description | Required |
---|---|---|
string | The string to search in. | Yes |
substring | The string to search for. | Yes |
casesensitive | Perform a case sensitive comparison. Default is true. | No |
Tests whether a string evaluates to true.
<istrue value="${someproperty}"/> <istrue value="false"/>
Tests whether a string evaluates to not true, the negation of
<istrue>
<isfalse value="${someproperty}"/> <isfalse value="false"/>
Tests whether a property evaluates to true.
<ispropertytrue property="someproperty"/>
Tests whether a property evaluates to not true, the negation of
<ispropertytrue>
<ispropertyfalse property="someproperty"/>
Tests whether a specified reference exists.
<referenceexists ref="${someid}"/>
This condition is identical to the Available task, all attributes and nested elements of that task are supported, the property and value attributes are redundant and will be ignored.
<if> <available file="README.md"/> <then> <echo message="Please read README.md"/> </then> </if>
Test two files for matching. Nonexistence of one file results in "false", although if neither exists they are considered equal in terms of content. This test does a byte for byte comparison, so test time scales with byte size. NB: if the files are different sizes, one of them is missing or the filenames match the answer is so obvious the detailed test is omitted.
Table 5.16: filesmatch Attributes
Attribute | Description | Required |
---|---|---|
file1 | First file to test. | Yes |
file2 | Second file to test. | Yes |
<filesmatch file1="${file1}" file2="${file2}"/>
Test whether a file passes an embedded selector.
Table 5.17: isfileselected Attributes
Attribute | Description | Required |
---|---|---|
file | The file to check if is passes the embedded selector. | Yes |
basedir | The base directory to use for name based selectors. It this is not set, the project's basedirectory will be used. | No |
<isfileselected file="a.xml"> <date datetime="06/28/2000 2:02 pm" when="equal"/> </isfileselected>
Test the return code of an executable for failure.
<exec command="test" returnProperty="return.code"/> <if> <isfailure code="${return.code}"/> <then><echo msg="${return.code}"/></then> </if>
Test if the specified string matches the specified regular expression pattern.
Table 5.19: matches Attributes
Attribute | Description | Required |
---|---|---|
string | The string to test. | Yes |
pattern | The regular expression pattern used to test. | Yes |
casesensitive | Perform a case sensitive match. Default is true. | No |
multiline | Perform a multi line match. Default is false. | No |
modifiers | The regular expression modifiers used to test. | No |