B.71 Variable

The Variable task provides a mutable property to Phing and works much like variable assignment in PHP. This task is similar to the standard Phing Property task, except that THESE PROPERTIES ARE MUTABLE. While this goes against the standard Phing use of properties, occasionally it is useful to be able to change a property value within the build. In general, use of this task is DISCOURAGED, and the standard Phing Property should be used if possible. Having said that, in real life I use this a lot.

Variables can be set individually or loaded from a standard properties file. A 'feature' of variables is that they can override properties, but properties cannot override variables. So if an already established property exists, its value can be reassigned by use of this task.

Table B.76: Attributes

nameStringThe name of the property to set.NoneYes, unless 'file' is used.
valueStringThe value of the property.""No
unsetBooleanRemoves the property from the project as if it had never been set.falseNo
fileStringThe name of a standard properties file to load variables from.NoneNo

B.71.1 Example

<var name="x" value="6"/>
                <echo>x = ${x}</echo>   <!-- print: 6 -->

                <var name="x" value="12"/>
                <echo>x = ${x}</echo>   <!-- print: 12 -->

                <var name="x" value="6 + ${x}"/>
                <echo>x = ${x}</echo>   <!-- print: 6 + 12 -->

                <var name="str" value="I "/>
                <var name="str" value="${str} am "/>
                <var name="str" value="${str} a "/>
                <var name="str" value="${str} string."/>
                <echo>${str}</echo>     <!-- print: I am a string. -->

                <var name="x" value="6"/>
                <echo>x = ${x}</echo>   <!-- print: 6 -->

                <property name="x" value="12"/>
                <echo>x = ${x}</echo>   <!-- print: 6 (property can't override) -->

                <var name="x" value="blue"/>
                <format property="x" pattern="%A"/>
                <echo>Today is ${x}.</echo> <!-- print: Today is blue. -->

                <var name="x" value="" unset="true"/>
                <format property="x" pattern="%A"/>
                <echo>Today is ${x}.</echo> <!-- print: Today is Friday. -->