source: release-kits/wirk3/ant-scripts/tasks/antelope/docs/manual/bk03ch09.html@ 15023

Last change on this file since 15023 was 15023, checked in by oranfry, 16 years ago

did the bulk of the work on wirk3

File size: 6.2 KB
Line 
1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Chapter 9. Variable Task</title><meta name="generator" content="DocBook XSL Stylesheets V1.68.1"><link rel="start" href="index.html" title="Antelope Users Guide"><link rel="up" href="bk03.html" title="Additional Ant Tasks"><link rel="prev" href="bk03ch08.html" title="Chapter 8. Unset Task"><link rel="next" href="bk03ch10.html" title="Chapter 10. Stopwatch"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 9. Variable Task</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="bk03ch08.html">Prev</a> </td><th width="60%" align="center">Additional Ant Tasks</th><td width="20%" align="right"> <a accesskey="n" href="bk03ch10.html">Next</a></td></tr></table><hr></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="Variable"></a>Chapter 9. Variable Task</h2></div></div></div>
2<STYLE TYPE="text/css"> <!-- @import url(./style.css); --> </STYLE>
3 <p>
4The 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"><strong>In general, use of this task is DISCOURAGED, and the standard Ant Property should be used if possible.</strong></span> Having said that, in real life I use this a lot.
5</p><p>
6To use this task in your build files, include a task definition like this:
7</p><p>
8</p><pre class="programlisting">
9
10 &lt;taskdef name="var" classname="ise.antelope.tasks.Variable"/&gt;
11
12</pre><p>
13</p><p>
14Variables 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.
15</p><p>
16</p><div class="table"><a name="id2521241"></a><p class="title"><b>Table 9.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>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>
17</p><p>
18In the following example, the property <code class="computeroutput">x</code> is first set to "6", then evaluated by the <code class="computeroutput">if</code>, and reassigned the value "12". The <code class="computeroutput">echo</code> task will print out 12.
19</p><p>
20</p><pre class="programlisting">
21
22 &lt;var name="x" value="6"/&gt;
23 &lt;if name="x" value="6"&gt;
24 &lt;var name="x" value="12"/&gt;
25 &lt;/if&gt;
26 &lt;echo&gt;${x}&lt;/echo&gt; &lt;!-- will print 12 --&gt;
27
28</pre><p>
29</p><p>
30The 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, however, if the var value is set to "", then it can as in the case of the format example.
31</p><p>
32</p><pre class="programlisting">
33
34 &lt;var name="x" value="6"/&gt;
35 &lt;echo&gt;x = ${x}&lt;/echo&gt; &lt;!-- print: 6 --&gt;
36
37 &lt;var name="x" value="12"/&gt;
38 &lt;echo&gt;x = ${x}&lt;/echo&gt; &lt;!-- print: 12 --&gt;
39
40 &lt;var name="x" value="6 + ${x}"/&gt;
41 &lt;echo&gt;x = ${x}&lt;/echo&gt; &lt;!-- print: 6 + 12 --&gt;
42
43 &lt;var name="str" value="I "/&gt;
44 &lt;var name="str" value="${str} am "/&gt;
45 &lt;var name="str" value="${str} a "/&gt;
46 &lt;var name="str" value="${str} string."/&gt;
47 &lt;echo&gt;${str}&lt;/echo&gt; &lt;!-- print: I am a string. --&gt;
48
49 &lt;var name="x" value="6"/&gt;
50 &lt;echo&gt;x = ${x}&lt;/echo&gt; &lt;!-- print: 6 --&gt;
51
52 &lt;property name="x" value="12"/&gt;
53 &lt;echo&gt;x = ${x}&lt;/echo&gt; &lt;!-- print: 6 (property can't override) --&gt;
54
55 &lt;var name="x" value="blue"/&gt;
56 &lt;tstamp&gt;
57 &lt;format property="x" pattern="EEEE"/&gt;
58 &lt;/tstamp&gt;
59 &lt;echo&gt;Today is ${x}.&lt;/echo&gt; &lt;!-- print: Today is blue. --&gt;
60
61 &lt;var name="x" value=""/&gt;
62 &lt;tstamp&gt;
63 &lt;format property="x" pattern="EEEE"/&gt;
64 &lt;/tstamp&gt;
65 &lt;echo&gt;Today is ${x}.&lt;/echo&gt; &lt;!-- print: Today is Friday. --&gt;
66
67
68</pre><p>
69</p><p>
70The 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.
71</p><p>
72</p><pre class="programlisting">
73
74 &lt;var name="valid_email" value="false"/&gt;
75 &lt;if name="email_from" value="[email protected]"&gt;
76 &lt;var name="valid_email" value="true"/&gt;
77 &lt;/if&gt;
78 &lt;if name="email_from" value="[email protected]"&gt;
79 &lt;var name="valid_email" value="true"/&gt;
80 &lt;/if&gt;
81 &lt;assert name="valid_email" value="true" failonerror="false"&gt;
82 &lt;try&gt;
83 &lt;mail from="${email_from}" tolist="${email_to}"
84 message="New release available"/&gt;
85 &lt;/try&gt;
86 &lt;/assert&gt;
87
88</pre><p>
89</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="bk03ch08.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="bk03.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="bk03ch10.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 8. Unset Task </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 10. Stopwatch</td></tr></table></div></body></html>
Note: See TracBrowser for help on using the repository browser.