1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>
Variable Task</title>
<meta content="DocBook XSL Stylesheets V1.60.1" name="generator">
<link rel="home" href="index.html" title="Antelope Users Guide">
<link rel="up" href="bk03.html" title="Additional Ant Tasks">
<link rel="previous" href="bk03ch07.html" title="Chapter 7. Try Task">
<link rel="next" href="bk03ch09.html" title="Chapter 9. Stopwatch">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="chapter" lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title">
<a name="Variable">
</a>
Chapter 8. Variable Task</h2>
</div>
</div>
<div>
</div>
</div>
<p>
The Variable task provides a mutable property to Ant and works much like variable assignment in Java. This task is similar to the standard Ant Property task, except that THESE PROPERTIES ARE MUTABLE. While this goes against the standard Ant use of properties, occasionally it is useful to be able to change a property value within the build. <span class="bold">
<b>
In general, use of this task is DISCOURAGED, and the standard Ant Property should be used if possible.</b>
</span>
Having said that, in real life I use this a lot.
</p>
<p>
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.
</p>
<p>
<div class="table">
<a name="N108CD">
</a>
<p class="title">
<b>
Table 8.1. Variable Task Attributes</b>
</p>
<table summary="Variable Task Attributes" border="1">
<colgroup>
<col>
<col>
<col>
<col>
</colgroup>
<thead>
<tr>
<th>
Attribute</th>
<th>
Description</th>
<th>
Default</th>
<th>
Required</th>
</tr>
</thead>
<tbody>
<tr>
<td>
name</td>
<td>
The name of the property to set.</td>
<td>
None</td>
<td>
Yes, unless 'file' is used.</td>
</tr>
<tr>
<td>
value</td>
<td>
The value of the property.</td>
<td>
""</td>
<td>
No</td>
</tr>
<tr>
<td>
unset</td>
<td>
Removes the property from the project as if it had never been set.</td>
<td>
false
</td>
<td>
No
</td>
</tr>
<tr>
<td>
file</td>
<td>
The name of a standard properties file to load variables from.</td>
<td>
None</td>
<td>
No</td>
</tr>
</tbody>
</table>
</div>
</p>
<p>
In the following example, the property <tt class="computeroutput">
x</tt>
is first set to "6", then evaluated by the <tt class="computeroutput">
if</tt>
, and reassigned the value "12". The <tt class="computeroutput">
echo</tt>
task will print out 12.
</p>
<p>
<table border="0" bgcolor="#E0E0E0">
<tr>
<td>
<pre class="programlisting">
<var name="x" value="6"/>
<if>
<equals arg1="${x}" arg2="6" />
<then>
<var name="x" value="12"/>
</then>
</if>
<echo>${x}</echo> <!-- will print 12 -->
</pre>
</td>
</tr>
</table>
</p>
<p>
The next example shows a property being set, echoed, unset, then reset:
<table border="0" bgcolor="#E0E0E0">
<tr>
<td>
<pre class="programlisting">
<property name="x" value="6"/>
<echo>${x}</echo> <!-- will print 6 -->
<var name="x" unset="true"/>
<property name="x" value="12"/>
<echo>${x}</echo> <!-- will print 12 -->
</pre>
</td>
</tr>
</table>
</p>
<p>
The following shows some more uses of the Variable task. It is especially handy for property appending. Notice a couple of things: the property task can't override a var value, in general, you should use var with the unset attribute to change the value of a property.
</p>
<p>
<table border="0" bgcolor="#E0E0E0">
<tr>
<td>
<pre class="programlisting">
<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"/>
<tstamp>
<format property="x" pattern="EEEE"/>
</tstamp>
<echo>Today is ${x}.</echo> <!-- print: Today is blue. -->
<var name="x" value="" unset="true"/>
<tstamp>
<format property="x" pattern="EEEE"/>
</tstamp>
<echo>Today is ${x}.</echo> <!-- print: Today is Friday. -->
</pre>
</td>
</tr>
</table>
</p>
<p>
<!--
The next example shows Variable, If, Assert, and Try working together to make sure e-mail is sent from the right address and that if the mail fails to be sent for any reason, the build will not fail.
</p>
<p>
<table border="0" bgcolor="#E0E0E0">
<tr>
<td>
<pre class="programlisting">
<var name="valid_email" value="false"/>
<if name="email_from" value="buildteam@mycompany.com">
<var name="valid_email" value="true"/>
</if>
<if name="email_from" value="buildsite@mycompany.com">
<var name="valid_email" value="true"/>
</if>
<assert name="valid_email" value="true" failonerror="false">
<try>
<mail from="${email_from}" tolist="${email_to}"
message="New release available"/>
</try>
</assert>
</pre>
</td>
</tr>
</table>
-->
</p>
</div>
<hr>
<p align="center">Copyright © 2003-2004 Ant-Contrib Project. All
rights Reserved.</p>
</body>
</html>
|