source: release-kits/lirk3/ant-scripts/tasks/antelope/docs/manual/bk03ch07.html@ 14982

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

initial import of LiRK3

File size: 6.9 KB
Line 
1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Chapter 7. Try 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="bk03ch06.html" title="Chapter 6. SwitchTask"><link rel="next" href="bk03ch08.html" title="Chapter 8. Unset Task"></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 7. Try Task</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="bk03ch06.html">Prev</a> </td><th width="60%" align="center">Additional Ant Tasks</th><td width="20%" align="right"> <a accesskey="n" href="bk03ch08.html">Next</a></td></tr></table><hr></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="TryTask"></a>Chapter 7. Try Task</h2></div></div></div>
2<STYLE TYPE="text/css"> <!-- @import url(./style.css); --> </STYLE>
3 <p>
4The "Try" task works similarly to the try/catch/finally construct in Java. This task is useful when a particular task might fail, but the build should not fail if it does. An example is the "mail" task will fail if the mail server is not available, but the build should not fail if the mail message cannot be delivered.
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="try" classname="ise.antelope.tasks.TryTask"/&gt;
11
12</pre><p>
13</p><p>
14A quick example is probably all that is necessary:
15</p><pre class="programlisting">
16
17 &lt;tempfile property="temp.file" destdir="${java.io.tmpdir}"
18 prefix="delete" suffix=".tmp"/&gt;
19 &lt;try&gt;
20 &lt;!-- use 'get' task to post to the unit test status servlet. It
21 would be better to use a post for this, but this shows a good
22 use of 'finally'. --&gt;
23 &lt;get
24 src="http://mycompany.com/servlet/junit?testnum=${test.num}&amp;status="${status}"
25 dest="${temp.file}"/&gt;
26
27 &lt;catch&gt;
28 &lt;echo&gt;Unit test servlet update failed.&lt;/echo&gt;
29 &lt;/catch&gt;
30
31 &lt;finally&gt;
32 &lt;delete file="${temp.file}"/&gt;
33 &lt;/finally&gt;
34 &lt;/try&gt;
35
36</pre><p>
37</p><p>
38Unlike the Java "try", neither the "catch" block nor the "finally" block are required. Also, the order does not matter, the "catch" block may be listed first, followed by the "finally", followed by the tasks that may fail.
39</p><p>
40</p><div class="table"><a name="id2520781"></a><p class="title"><b>Table 7.1. Try Task Attributes</b></p><table summary="Try 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>break</td><td>If true and a nested task fails, no other nested tasks will execute. If false, all nested tasks will execute regardless of whether a previous task failed. Note that for each failed task, the 'catch' block (if defined) will execute.</td><td>true</td><td>No</td></tr><tr><td>printstacktrace</td><td>If true, the exception stack trace from a failed task will be logged. </td><td>false</td><td>No</td></tr><tr><td>stacktraceproperty</td><td>Specify a property to store the stack trace of any exception. </td><td>None</td><td>No</td></tr><tr><td>printmessage</td><td>If true, the exception message from a failed task will be logged. If printstacktrace is set to true, this attribute is ignored as the exception message is printed as part of the stack trace.</td><td>true</td><td>No</td></tr><tr><td>messageproperty</td><td>Specify a property to store the message line of any exception.</td><td>None</td><td>No</td></tr></tbody></table></div><p>
41</p><p>
42The next example shows the "break" attribute set to "no". In this case, the second echo task will execute.
43</p><p>
44</p><pre class="programlisting">
45
46 &lt;target name="test" description="This exercises the Try task."&gt;
47 &lt;try break="no"&gt;
48 &lt;echo&gt;I am trying...&lt;/echo&gt;
49 &lt;fail message=" and I failed..."/&gt;
50 &lt;echo&gt; but I did not die!&lt;/echo&gt; &lt;!-- this WILL print --&gt;
51 &lt;/try&gt;
52 &lt;/target&gt;
53
54</pre><p>
55</p><p>
56This slightly more practical example uses the <a href="bk03ch09.html" title="Chapter 9. Variable Task">Variable</a> task coupled with "try" to run a series of tests. All tests will run even if a preceding test fails. The "catch" block logs a message of each failed test.
57</p><p>
58</p><pre class="programlisting">
59
60 &lt;target name="runTests" messageproperty="msg"&gt;
61 &lt;try catch="testFailed" break="no"&gt;
62 &lt;var name="testname" value="fileUtilTests"/&gt;
63 &lt;antcall target="runFileUtilTests"/&gt;
64 &lt;var name="testname" value="imageUtilTests"/&gt;
65 &lt;antcall target="runImageUtilTests"/&gt;
66 &lt;var name="testname" value="imageConversionTests"/&gt;
67 &lt;antcall target="runImageConversionTests"/&gt;
68
69 &lt;catch&gt;
70 &lt;!-- log a test failure --&gt;
71 &lt;echo file="test.log" append="yes"&gt;
72 Test suite ${testname} failed: ${msg}
73 &lt;/echo&gt;
74 &lt;/catch&gt;
75 &lt;/try&gt;
76 &lt;/target&gt;
77 &lt;/target&gt;
78
79</pre><p>
80</p><p>
81The following example uses a nested Finally to clean up resources:
82</p><pre class="programlisting">
83
84 &lt;tempfile property="temp.file" destdir="${java.io.tmpdir}"
85 prefix="delete" suffix=".tmp"/&gt;
86 &lt;try&gt;
87 &lt;!-- use 'get' task to post to the unit test status servlet. It
88 would be better use use a post for this, but this shows a good
89 use of 'finally'. --&gt;
90 &lt;get
91 src="http://mycompany.com/servlet/junit?testnum=${test.num}&amp;status="${status}"
92 dest="${temp.file}"/&gt;
93
94 &lt;catch&gt;
95 &lt;echo&gt;Unit test servlet update failed.&lt;/echo&gt;
96 &lt;/catch&gt;
97
98 &lt;finally&gt;
99 &lt;delete file="${temp.file}"/&gt;
100 &lt;/finally&gt;
101 &lt;/try&gt;
102
103</pre><p>
104See the <a href="bk03ch17.html" title="Chapter 17. HTTP Post">post</a> task for a better way to do a post.
105</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="bk03ch06.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="bk03ch08.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 6. SwitchTask </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 8. Unset Task</td></tr></table></div></body></html>
Note: See TracBrowser for help on using the repository browser.