Debian Codegraph @

docs/manual/tasks/variable_task.html

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