$variable:

   The builtin $variable: (or more commonly just $v: or $var:) function shortcut lets you read the value of an AntXtras variable. You can also use this funcut to supply a default value if the named variable is undefined. The $variable: funcut is a query only feature– you cannot use it to modify existing data.

The $variable: funcut and its alias $v: and $var: are automatically installed and enabled by the standard AntXtras funcuts antlib. Because it’s built into AntXtras, you cannot uninstall the $variable: funcut like you can most other funcuts; if funcuts are enabled, you get the $variable:, $var:, and $v: funcut handlers always.

Parameters

The general form of the function shortcut is: $variable:name[?default-value] where name is the name of the variable to be read and default-value is the value to use if the named variable does not exist. Note that if the variable exists but is empty it is still read and the funcut’s output value will be the empty string.

Examples

Defining And Using A Variable

The following snippet uses the $var: funcut to display the value of a variable ‘.started’ that is updated everytime the macro “run-test” is called. Note that the macro’s caller will have access the same variable using $var: when the macro returns.

1: <macrodef name="run-test">
2:   <attribute name="name">
3:   <sequential>
4:     <assign var=".started" value="${$isomillis:}"/>
5:     <echo level="info" message="RUN: [${$var:.started}] - @{name}"/>
6:7:     <assign var=".stopped" value="${$isomillis:}"/>
Using A Default Value For Missing Variable

The following snippet builds on the previous one and uses the $var: funcut to access the ‘.stopped’ variable set by the “run-test” macro. However, if that macro prematurely aborted (the test failed), then this snippet uses “INCOMPLETE” as a fallback variable value.

 1: <macrodef name="run-tests">
 2:   <attribute name="names"/>
 3:   <sequential>
 4: 5:     <doforeach i="name" list="@{names}">
 6:       <run-test name="${name}"/>
 7:       <echo level="info" message="END: [${$var:.stopped?INCOMPLETE}]"/>
 8:     </doforeach>
 9:

Aliases

  • $variable:
  • $var:
  • $v:

Related Topics

  • The $property: funcut lets you read a property.
  • The $system: funcut lets you read a System property.

Navigation
Personal Tools