1 | <html>
|
---|
2 | <head>
|
---|
3 | <title>Tutorial: Writing Tasks</title>
|
---|
4 | <meta name="author" content="Jan Matèrne">
|
---|
5 | <style type="text/css">
|
---|
6 | <!--
|
---|
7 | .code { background: #EFEFEF; margin-top: }
|
---|
8 | .output { color: #FFFFFF; background: #837A67; }
|
---|
9 | -->
|
---|
10 | </style>
|
---|
11 | <link rel="stylesheet" type="text/css" href="stylesheets/antmanual.css">
|
---|
12 | </head>
|
---|
13 | <body>
|
---|
14 | <h1>Tutorial: Writing Tasks</h1>
|
---|
15 |
|
---|
16 | <p>This document provides a step by step tutorial for writing
|
---|
17 | tasks.</p>
|
---|
18 | <h2>Content</h2>
|
---|
19 | <p><ul>
|
---|
20 | <li><a href="#buildenvironment">Set up the build environment</a></li>
|
---|
21 | <li><a href="#write1">Write the Task</a></li>
|
---|
22 | <li><a href="#use1">Use the Task</a></li>
|
---|
23 | <li><a href="#TaskAdapter">Integration with TaskAdapter</a></li>
|
---|
24 | <li><a href="#derivingFromTask">Deriving from Ant's Task</a></li>
|
---|
25 | <li><a href="#attributes">Attributes</a></li>
|
---|
26 | <li><a href="#NestedText">Nested Text</a></li>
|
---|
27 | <li><a href="#NestedElements">Nested Elements</a></li>
|
---|
28 | <li><a href="#complex">Our task in a little more complex version</a></li>
|
---|
29 | <li><a href="#TestingTasks">Test the Task</a></li>
|
---|
30 | <li><a href="#resources">Resources</a></li>
|
---|
31 | </ul></p>
|
---|
32 |
|
---|
33 | <a name="buildenvironment"></a>
|
---|
34 | <h2>Set up the build environment</h2>
|
---|
35 | <p>Ant builds itself, we are using Ant too (why we would write
|
---|
36 | a task if not? :-) therefore we should use Ant for our build.<p>
|
---|
37 | <p>We choose a directory as root directory. All things will be done
|
---|
38 | here if I say nothing different. I will reference this directory
|
---|
39 | as <i>root-directory</i> of our project. In this root-directory we
|
---|
40 | create a text file names <i>build.xml</i>. What should Ant do for us?
|
---|
41 | <ul>
|
---|
42 | <li>compiles my stuff</li>
|
---|
43 | <li>make the jar, so that I can deploy it</li>
|
---|
44 | <li>clean up everything</li>
|
---|
45 | </ul>
|
---|
46 | So the buildfile contains three targets.
|
---|
47 | <pre class="code">
|
---|
48 | <?xml version="1.0" encoding="ISO-8859-1"?>
|
---|
49 | <project name="MyTask" basedir="." default="jar">
|
---|
50 |
|
---|
51 | <target name="clean" description="Delete all generated files">
|
---|
52 | <delete dir="classes"/>
|
---|
53 | <delete file="MyTasks.jar"/>
|
---|
54 | </target>
|
---|
55 |
|
---|
56 | <target name="compile" description="Compiles the Task">
|
---|
57 | <javac srcdir="src" destdir="classes"/>
|
---|
58 | </target>
|
---|
59 |
|
---|
60 | <target name="jar" description="JARs the Task">
|
---|
61 | <jar destfile="MyTask.jar" basedir="classes"/>
|
---|
62 | </target>
|
---|
63 |
|
---|
64 | </project>
|
---|
65 | </pre>
|
---|
66 |
|
---|
67 | This buildfile uses often the same value (src, classes, MyTask.jar), so we should rewrite that
|
---|
68 | using <code><property></code>s. On second there are some handicaps: <code><javac></code> requires that the destination
|
---|
69 | directory exists; a call of "clean" with a non existing classes directory will fail; "jar" requires
|
---|
70 | the execution of some steps bofore. So the refactored code is:
|
---|
71 |
|
---|
72 | <pre class="code">
|
---|
73 | <?xml version="1.0" encoding="ISO-8859-1"?>
|
---|
74 | <project name="MyTask" basedir="." default="jar">
|
---|
75 |
|
---|
76 | <b><property name="src.dir" value="src"/></b>
|
---|
77 | <b><property name="classes.dir" value="classes"/></b>
|
---|
78 |
|
---|
79 | <target name="clean" description="Delete all generated files">
|
---|
80 | <delete dir="<b>${classes.dir}</b>" <b>failonerror="false"</b>/>
|
---|
81 | <delete file="<b>${ant.project.name}.jar</b>"/>
|
---|
82 | </target>
|
---|
83 |
|
---|
84 | <target name="compile" description="Compiles the Task">
|
---|
85 | <b><mkdir dir="${classes.dir}"/></b>
|
---|
86 | <javac srcdir="<b>${src.dir}</b>" destdir="${classes.dir}"/>
|
---|
87 | </target>
|
---|
88 |
|
---|
89 | <target name="jar" description="JARs the Task" <b>depends="compile"</b>>
|
---|
90 | <jar destfile="${ant.project.name}.jar" basedir="${classes.dir}"/>
|
---|
91 | </target>
|
---|
92 |
|
---|
93 | </project>
|
---|
94 | </pre>
|
---|
95 | <i>ant.project.name</i> is one of the
|
---|
96 | <a href="http://ant.apache.org/manual/using.html#built-in-props" target="_blank">
|
---|
97 | build-in properties [1]</a> of Ant.
|
---|
98 |
|
---|
99 |
|
---|
100 | <a name="write1"></a>
|
---|
101 | <h2>Write the Task</h2>
|
---|
102 |
|
---|
103 | Now we write the simplest Task - a HelloWorld-Task (what else?). Create a text file
|
---|
104 | <i>HelloWorld.java</i> in the src-directory with:
|
---|
105 | <pre class="code">
|
---|
106 | public class HelloWorld {
|
---|
107 | public void execute() {
|
---|
108 | System.out.println("Hello World");
|
---|
109 | }
|
---|
110 | }
|
---|
111 | </pre>
|
---|
112 | and we can compile and jar it with <tt>ant</tt> (default target is "jar" and via
|
---|
113 | its <i>depends</i>-clause the "compile" is executed before).
|
---|
114 |
|
---|
115 |
|
---|
116 |
|
---|
117 | <a name="use1"></a>
|
---|
118 | <h2>Use the Task</h2>
|
---|
119 | <p>But after creating the jar we want to use our new Task. Therefore we need a
|
---|
120 | new target "use". Before we can use our new task we have to declare it with
|
---|
121 | <a href="http://ant.apache.org/manual/CoreTasks/taskdef.html" target="_blank">
|
---|
122 | <code><taskdef></code> [2]</a>. And for easier process we change the default clause:
|
---|
123 | <pre class="code">
|
---|
124 | <?xml version="1.0" encoding="ISO-8859-1"?>
|
---|
125 | <project name="MyTask" basedir="." default="<b>use</b>">
|
---|
126 |
|
---|
127 | ...
|
---|
128 |
|
---|
129 | <b><target name="use" description="Use the Task" depends="jar">
|
---|
130 | <taskdef name="helloworld" classname="HelloWorld" classpath="${ant.project.name}.jar"/>
|
---|
131 | <helloworld/>
|
---|
132 | </target></b>
|
---|
133 |
|
---|
134 | </project>
|
---|
135 | </pre>
|
---|
136 |
|
---|
137 | Important is the <i>classpath</i>-attribute. Ant searches in its /lib directory for
|
---|
138 | tasks and our task isn't there. So we have to provide the right location. </p>
|
---|
139 |
|
---|
140 | <p>Now we can type in <tt>ant</tt> and all should work ...
|
---|
141 | <pre class="output">
|
---|
142 | Buildfile: build.xml
|
---|
143 |
|
---|
144 | compile:
|
---|
145 | [mkdir] Created dir: C:\tmp\anttests\MyFirstTask\classes
|
---|
146 | [javac] Compiling 1 source file to C:\tmp\anttests\MyFirstTask\classes
|
---|
147 |
|
---|
148 | jar:
|
---|
149 | [jar] Building jar: C:\tmp\anttests\MyFirstTask\MyTask.jar
|
---|
150 |
|
---|
151 | use:
|
---|
152 | [helloworld] Hello World
|
---|
153 |
|
---|
154 | BUILD SUCCESSFUL
|
---|
155 | Total time: 3 seconds
|
---|
156 | </pre>
|
---|
157 |
|
---|
158 |
|
---|
159 |
|
---|
160 | <a name="TaskAdapter"></a>
|
---|
161 | <h2>Integration with TaskAdapter</h2>
|
---|
162 | <p>Our class has nothing to do with Ant. It extends no superclass and implements
|
---|
163 | no interface. How does Ant know to integrate? Via name convention: our class provides
|
---|
164 | a method with signature <tt>public void execute()</tt>. This class is wrapped by Ant's
|
---|
165 | <tt>org.apache.tools.ant.TaskAdapter</tt> which is a task and uses reflection for
|
---|
166 | setting a reference to the project and calling the <i>execute()</i> method.</p>
|
---|
167 |
|
---|
168 | <p><i>Setting a reference to the project</i>? Could be interesting. The Project class
|
---|
169 | gives us some nice abilities: access to Ant's logging facilities getting and setting
|
---|
170 | properties and much more. So we try to use that class:
|
---|
171 | <pre class="code">
|
---|
172 | import org.apache.tools.ant.Project;
|
---|
173 |
|
---|
174 | public class HelloWorld {
|
---|
175 |
|
---|
176 | private Project project;
|
---|
177 |
|
---|
178 | public void setProject(Project proj) {
|
---|
179 | project = proj;
|
---|
180 | }
|
---|
181 |
|
---|
182 | public void execute() {
|
---|
183 | String message = project.getProperty("ant.project.name");
|
---|
184 | project.log("Here is project '" + message + "'.", Project.MSG_INFO);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | </pre>
|
---|
188 | and the execution with <tt>ant</tt> will show us the expected
|
---|
189 | <pre class="output">
|
---|
190 | use:
|
---|
191 | Here is project 'MyTask'.
|
---|
192 | </pre></p>
|
---|
193 |
|
---|
194 |
|
---|
195 | <a name="derivingFromTask"></a>
|
---|
196 | <h2>Deriving from Ant's Task</h2>
|
---|
197 | <p>Ok, that works ... But usually you will extend <tt>org.apache.tools.ant.Task</tt>.
|
---|
198 | That class is integrated in Ant, get's the project-reference, provides documentation
|
---|
199 | fiels, provides easier access to the logging facility and (very useful) gives you
|
---|
200 | the exact location where <i>in the buildfile</i> this task instance is used.</p>
|
---|
201 |
|
---|
202 | <p>Oki-doki - let's us use some of these:
|
---|
203 | <pre class="code">
|
---|
204 | import org.apache.tools.ant.Task;
|
---|
205 |
|
---|
206 | public class HelloWorld extends Task {
|
---|
207 | public void execute() {
|
---|
208 | // use of the reference to Project-instance
|
---|
209 | String message = getProject().getProperty("ant.project.name");
|
---|
210 |
|
---|
211 | // Task's log method
|
---|
212 | log("Here is project '" + message + "'.");
|
---|
213 |
|
---|
214 | // where this task is used?
|
---|
215 | log("I am used in: " + getLocation() );
|
---|
216 | }
|
---|
217 | }
|
---|
218 | </pre>
|
---|
219 | which gives us when running
|
---|
220 | <pre class="output">
|
---|
221 | use:
|
---|
222 | [helloworld] Here is project 'MyTask'.
|
---|
223 | [helloworld] I am used in: C:\tmp\anttests\MyFirstTask\build.xml:23:
|
---|
224 | </pre>
|
---|
225 |
|
---|
226 |
|
---|
227 | <a name="attributes">
|
---|
228 | <h2>Attributes</h2>
|
---|
229 | <p>Now we want to specify the text of our message (it seems that we are
|
---|
230 | rewriting the <code><echo/></code> task :-). First we well do that with an attribute.
|
---|
231 | It is very easy - for each attribute provide a <tt>public void set<code><attributename></code>(<code><type></code>
|
---|
232 | newValue)</tt> method and Ant will do the rest via reflection.</p>
|
---|
233 | <pre class="code">
|
---|
234 | import org.apache.tools.ant.Task;
|
---|
235 | import org.apache.tools.ant.BuildException;
|
---|
236 |
|
---|
237 | public class HelloWorld extends Task {
|
---|
238 |
|
---|
239 | String message;
|
---|
240 | public void setMessage(String msg) {
|
---|
241 | message = msg;
|
---|
242 | }
|
---|
243 |
|
---|
244 | public void execute() {
|
---|
245 | if (message==null) {
|
---|
246 | throw new BuildException("No message set.");
|
---|
247 | }
|
---|
248 | log(message);
|
---|
249 | }
|
---|
250 |
|
---|
251 | }
|
---|
252 | </pre>
|
---|
253 | <p>Oh, what's that in execute()? Throw a <i>BuildException</i>? Yes, that's the usual
|
---|
254 | way to show Ant that something important is missed and complete build should fail. The
|
---|
255 | string provided there is written as build-failes-message. Here it's necessary because
|
---|
256 | the log() method can't handle a <i>null</i> value as parameter and throws a NullPointerException.
|
---|
257 | (Of course you can initialize the <i>message</i> with a default string.)</p>
|
---|
258 |
|
---|
259 | <p>After that we have to modify our buildfile:
|
---|
260 | <pre class="code">
|
---|
261 | <target name="use" description="Use the Task" depends="jar">
|
---|
262 | <taskdef name="helloworld"
|
---|
263 | classname="HelloWorld"
|
---|
264 | classpath="${ant.project.name}.jar"/>
|
---|
265 | <helloworld <b>message="Hello World"</b>/>
|
---|
266 | </target>
|
---|
267 | </pre>
|
---|
268 | That's all.</p>
|
---|
269 |
|
---|
270 | <p>Some background for working with attributes: Ant supports any of these datatypes as
|
---|
271 | arguments of the set-method:<ul>
|
---|
272 | <li>elementary data type like <i>int</i>, <i>long</i>, ...</li>
|
---|
273 | <li>its wrapper classes like <i>java.lang.Integer</i>, <i>java.lang.Long</i>, ...</li>
|
---|
274 | <li><i>java.lang.String</i></li>
|
---|
275 | <li>some more classes (e.g. <i>java.io.File</i>; see
|
---|
276 | <a href="http://ant.apache.org/manual/develop.html#set-magic">Manual
|
---|
277 | 'Writing Your Own Task' [3]</a>)</li>
|
---|
278 | </ul>
|
---|
279 | Before calling the set-method all properties are resolved. So a <tt><helloworld message="${msg}"/></tt>
|
---|
280 | would not set the message string to "${msg}" if there is a property "msg" with a set value.
|
---|
281 |
|
---|
282 |
|
---|
283 | <a name="NestedText"></a>
|
---|
284 | <h2>Nested Text</h2>
|
---|
285 | <p>Maybe you have used the <code><echo></code> task in a way like <tt><echo>Hello World</echo></tt>.
|
---|
286 | For that you have to provide a <tt>public void addText(String text)</tt> method.
|
---|
287 | <pre class="code">
|
---|
288 | ...
|
---|
289 | public class HelloWorld extends Task {
|
---|
290 | ...
|
---|
291 | public void addText(String text) {
|
---|
292 | message = text;
|
---|
293 | }
|
---|
294 | ...
|
---|
295 | }
|
---|
296 | </pre>
|
---|
297 | But here properties are <b>not</b> resolved! For resolving properties we have to use
|
---|
298 | Project's <tt>replaceProperties(String propname) : String</tt> method which takes the
|
---|
299 | property name as argument and returns its value (or ${propname} if not set).</p>
|
---|
300 |
|
---|
301 |
|
---|
302 | <a name="NestedElements"></a>
|
---|
303 | <h2>Nested Elements</h2>
|
---|
304 | <p>There are several ways for inserting the ability of handling nested elements. See
|
---|
305 | the <a href="http://ant.apache.org/manual/develop.html#nested-elements">Manual [4]</a> for other.
|
---|
306 | We use the first way of the three described ways. There are several steps for that:<ol>
|
---|
307 | <li>We create a class for collecting all the infos the nested element should contain.
|
---|
308 | This class is created by the same rules for attributes and nested elements
|
---|
309 | as for the task (<code>set<attributename></code>() methods). </li>
|
---|
310 | <li>The task holds multiple instances of this class in a list.</li>
|
---|
311 | <li>A factory method instantiates an object, saves the reference in the list
|
---|
312 | and returns it to Ant Core.</li>
|
---|
313 | <li>The execute() method iterates over the list and evaluates its values.</li>
|
---|
314 | </li></p>
|
---|
315 | <pre class="code">
|
---|
316 | import java.util.Vector;
|
---|
317 | import java.util.Iterator;
|
---|
318 | ...
|
---|
319 | public void execute() {
|
---|
320 | if (message!=null) log(message);
|
---|
321 | for (Iterator it=messages.iterator(); it.hasNext(); ) { <b>// 4</b>
|
---|
322 | Message msg = (Message)it.next();
|
---|
323 | log(msg.getMsg());
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 |
|
---|
328 | Vector messages = new Vector(); <b>// 2</b>
|
---|
329 |
|
---|
330 | public Message createMessage() { <b>// 3</b>
|
---|
331 | Message msg = new Message();
|
---|
332 | messages.add(msg);
|
---|
333 | return msg;
|
---|
334 | }
|
---|
335 |
|
---|
336 | public class Message { <b>// 1</b>
|
---|
337 | public Message() {}
|
---|
338 |
|
---|
339 | String msg;
|
---|
340 | public void setMsg(String msg) { this.msg = msg; }
|
---|
341 | public String getMsg() { return msg; }
|
---|
342 | }
|
---|
343 | ...
|
---|
344 | </pre>
|
---|
345 | <p>Then we can use the new nested element. But where is xml-name for that defined?
|
---|
346 | The mapping XML-name : classname is defined in the factory method:
|
---|
347 | <tt>public <i>classname</i> create<i>XML-name</i>()</tt>. Therefore we write in
|
---|
348 | the buildfile
|
---|
349 | <pre class="code">
|
---|
350 | <helloworld>
|
---|
351 | <message msg="Nested Element 1"/>
|
---|
352 | <message msg="Nested Element 2"/>
|
---|
353 | </helloworld>
|
---|
354 | </pre>
|
---|
355 |
|
---|
356 |
|
---|
357 | <a name="complex"></a>
|
---|
358 | <h2>Our task in a little more complex version</h2>
|
---|
359 | <p>For recapitulation now a little refactored buildfile:
|
---|
360 | <pre class="code">
|
---|
361 | <?xml version="1.0" encoding="ISO-8859-1"?>
|
---|
362 | <project name="MyTask" basedir="." default="use">
|
---|
363 |
|
---|
364 | <property name="src.dir" value="src"/>
|
---|
365 | <property name="classes.dir" value="classes"/>
|
---|
366 |
|
---|
367 | <target name="clean" description="Delete all generated files">
|
---|
368 | <delete dir="${classes.dir}" failonerror="false"/>
|
---|
369 | <delete file="${ant.project.name}.jar"/>
|
---|
370 | </target>
|
---|
371 |
|
---|
372 | <target name="compile" description="Compiles the Task">
|
---|
373 | <mkdir dir="${classes.dir}"/>
|
---|
374 | <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
|
---|
375 | </target>
|
---|
376 |
|
---|
377 | <target name="jar" description="JARs the Task" depends="compile">
|
---|
378 | <jar destfile="${ant.project.name}.jar" basedir="${classes.dir}"/>
|
---|
379 | </target>
|
---|
380 |
|
---|
381 |
|
---|
382 | <target name="use.init"
|
---|
383 | description="Taskdef the HelloWorld-Task"
|
---|
384 | depends="jar">
|
---|
385 | <taskdef name="helloworld"
|
---|
386 | classname="HelloWorld"
|
---|
387 | classpath="${ant.project.name}.jar"/>
|
---|
388 | </target>
|
---|
389 |
|
---|
390 |
|
---|
391 | <target name="use.without"
|
---|
392 | description="Use without any"
|
---|
393 | depends="use.init">
|
---|
394 | <helloworld/>
|
---|
395 | </target>
|
---|
396 |
|
---|
397 | <target name="use.message"
|
---|
398 | description="Use with attribute 'message'"
|
---|
399 | depends="use.init">
|
---|
400 | <helloworld message="attribute-text"/>
|
---|
401 | </target>
|
---|
402 |
|
---|
403 | <target name="use.fail"
|
---|
404 | description="Use with attribute 'fail'"
|
---|
405 | depends="use.init">
|
---|
406 | <helloworld fail="true"/>
|
---|
407 | </target>
|
---|
408 |
|
---|
409 | <target name="use.nestedText"
|
---|
410 | description="Use with nested text"
|
---|
411 | depends="use.init">
|
---|
412 | <helloworld>nested-text</helloworld>
|
---|
413 | </target>
|
---|
414 |
|
---|
415 | <target name="use.nestedElement"
|
---|
416 | description="Use with nested 'message'"
|
---|
417 | depends="use.init">
|
---|
418 | <helloworld>
|
---|
419 | <message msg="Nested Element 1"/>
|
---|
420 | <message msg="Nested Element 2"/>
|
---|
421 | </helloworld>
|
---|
422 | </target>
|
---|
423 |
|
---|
424 |
|
---|
425 | <target name="use"
|
---|
426 | description="Try all (w/out use.fail)"
|
---|
427 | depends="use.without,use.message,use.nestedText,use.nestedElement"
|
---|
428 | />
|
---|
429 |
|
---|
430 | </project>
|
---|
431 | </pre>
|
---|
432 |
|
---|
433 | And the code of the task:
|
---|
434 | <pre class="code">
|
---|
435 | import org.apache.tools.ant.Task;
|
---|
436 | import org.apache.tools.ant.BuildException;
|
---|
437 | import java.util.Vector;
|
---|
438 | import java.util.Iterator;
|
---|
439 |
|
---|
440 | /**
|
---|
441 | * The task of the tutorial.
|
---|
442 | * Print a message or let the build fail.
|
---|
443 | * @author Jan Matèrne
|
---|
444 | * @since 2003-08-19
|
---|
445 | */
|
---|
446 | public class HelloWorld extends Task {
|
---|
447 |
|
---|
448 |
|
---|
449 | /** The message to print. As attribute. */
|
---|
450 | String message;
|
---|
451 | public void setMessage(String msg) {
|
---|
452 | message = msg;
|
---|
453 | }
|
---|
454 |
|
---|
455 | /** Should the build fail? Defaults to <i>false</i>. As attribute. */
|
---|
456 | boolean fail = false;
|
---|
457 | public void setFail(boolean b) {
|
---|
458 | fail = b;
|
---|
459 | }
|
---|
460 |
|
---|
461 | /** Support for nested text. */
|
---|
462 | public void addText(String text) {
|
---|
463 | message = text;
|
---|
464 | }
|
---|
465 |
|
---|
466 |
|
---|
467 | /** Do the work. */
|
---|
468 | public void execute() {
|
---|
469 | // handle attribute 'fail'
|
---|
470 | if (fail) throw new BuildException("Fail requested.");
|
---|
471 |
|
---|
472 | // handle attribute 'message' and nested text
|
---|
473 | if (message!=null) log(message);
|
---|
474 |
|
---|
475 | // handle nested elements
|
---|
476 | for (Iterator it=messages.iterator(); it.hasNext(); ) {
|
---|
477 | Message msg = (Message)it.next();
|
---|
478 | log(msg.getMsg());
|
---|
479 | }
|
---|
480 | }
|
---|
481 |
|
---|
482 |
|
---|
483 | /** Store nested 'message's. */
|
---|
484 | Vector messages = new Vector();
|
---|
485 |
|
---|
486 | /** Factory method for creating nested 'message's. */
|
---|
487 | public Message createMessage() {
|
---|
488 | Message msg = new Message();
|
---|
489 | messages.add(msg);
|
---|
490 | return msg;
|
---|
491 | }
|
---|
492 |
|
---|
493 | /** A nested 'message'. */
|
---|
494 | public class Message {
|
---|
495 | // Bean constructor
|
---|
496 | public Message() {}
|
---|
497 |
|
---|
498 | /** Message to print. */
|
---|
499 | String msg;
|
---|
500 | public void setMsg(String msg) { this.msg = msg; }
|
---|
501 | public String getMsg() { return msg; }
|
---|
502 | }
|
---|
503 |
|
---|
504 | }
|
---|
505 | </pre>
|
---|
506 |
|
---|
507 | And it works:
|
---|
508 | <pre class="output">
|
---|
509 | C:\tmp\anttests\MyFirstTask>ant
|
---|
510 | Buildfile: build.xml
|
---|
511 |
|
---|
512 | compile:
|
---|
513 | [mkdir] Created dir: C:\tmp\anttests\MyFirstTask\classes
|
---|
514 | [javac] Compiling 1 source file to C:\tmp\anttests\MyFirstTask\classes
|
---|
515 |
|
---|
516 | jar:
|
---|
517 | [jar] Building jar: C:\tmp\anttests\MyFirstTask\MyTask.jar
|
---|
518 |
|
---|
519 | use.init:
|
---|
520 |
|
---|
521 | use.without:
|
---|
522 |
|
---|
523 | use.message:
|
---|
524 | [helloworld] attribute-text
|
---|
525 |
|
---|
526 | use.nestedText:
|
---|
527 | [helloworld] nested-text
|
---|
528 |
|
---|
529 | use.nestedElement:
|
---|
530 | [helloworld]
|
---|
531 | [helloworld]
|
---|
532 | [helloworld]
|
---|
533 | [helloworld]
|
---|
534 | [helloworld] Nested Element 1
|
---|
535 | [helloworld] Nested Element 2
|
---|
536 |
|
---|
537 | use:
|
---|
538 |
|
---|
539 | BUILD SUCCESSFUL
|
---|
540 | Total time: 3 seconds
|
---|
541 | C:\tmp\anttests\MyFirstTask>ant use.fail
|
---|
542 | Buildfile: build.xml
|
---|
543 |
|
---|
544 | compile:
|
---|
545 |
|
---|
546 | jar:
|
---|
547 |
|
---|
548 | use.init:
|
---|
549 |
|
---|
550 | use.fail:
|
---|
551 |
|
---|
552 | BUILD FAILED
|
---|
553 | C:\tmp\anttests\MyFirstTask\build.xml:36: Fail requested.
|
---|
554 |
|
---|
555 | Total time: 1 second
|
---|
556 | C:\tmp\anttests\MyFirstTask>
|
---|
557 | </pre>
|
---|
558 | Next step: test ...
|
---|
559 |
|
---|
560 |
|
---|
561 |
|
---|
562 | <a name="TestingTasks"></a>
|
---|
563 | <h2>Test the Task</h2>
|
---|
564 | <p>We have written a test already: the use.* tasks in the buildfile. But its
|
---|
565 | difficult to test that automatically. Common (and in Ant) used is JUnit for
|
---|
566 | that. For testing tasks Ant provides a baseclass <tt>org.apache.tools.ant.BuildFileTest</tt>.
|
---|
567 | This class extends <tt>junit.framework.TestCase</tt> and can therefore be integrated
|
---|
568 | into the unit tests. But this class provides some for testing tasks useful methods:
|
---|
569 | initialize Ant, load a buildfile, execute targets,
|
---|
570 | expecting BuildExceptions with a specified text, expect a special text
|
---|
571 | in the output log ... </p>
|
---|
572 |
|
---|
573 | <p>In Ant it is usual that the testcase has the same name as the task with a prepending
|
---|
574 | <i>Test</i>, therefore we will create a file <i>HelloWorldTest.java</i>. Because we
|
---|
575 | have a very small project we can put this file into <i>src</i> directory (Ant's own
|
---|
576 | testclasses are in /src/testcases/...). Because we have already written our tests
|
---|
577 | for "hand-test" we can use that for automatic tests, too. But there is one little
|
---|
578 | problem we have to solve: all test supporting classes are not part of the binary
|
---|
579 | distribution of Ant. So you can build the special jar file from source distro with
|
---|
580 | target "test-jar" or you can download a nightly build from
|
---|
581 | <a href="http://gump.covalent.net/jars/latest/ant/ant-testutil.jar">
|
---|
582 | http://gump.covalent.net/jars/latest/ant/ant-testutil.jar [5]</a>.</p>
|
---|
583 |
|
---|
584 | <p>For executing the test and creating a report we need the optional tasks <code><junit></code>
|
---|
585 | and <code><junitreport></code>. So we add to the buildfile:
|
---|
586 | <pre class="code">
|
---|
587 | ...
|
---|
588 | <font color="#9F9F9F"><project name="MyTask" basedir="." </font>default="test"<font color="#9F9F9F">></font>
|
---|
589 | ...
|
---|
590 | <property name="ant.test.lib" value="ant-testutil.jar"/>
|
---|
591 | <property name="report.dir" value="report"/>
|
---|
592 | <property name="junit.out.dir.xml" value="${report.dir}/junit/xml"/>
|
---|
593 | <property name="junit.out.dir.html" value="${report.dir}/junit/html"/>
|
---|
594 |
|
---|
595 | <path id="classpath.run">
|
---|
596 | <path path="${java.class.path}"/>
|
---|
597 | <path location="${ant.project.name}.jar"/>
|
---|
598 | </path>
|
---|
599 |
|
---|
600 | <path id="classpath.test">
|
---|
601 | <path refid="classpath.run"/>
|
---|
602 | <path location="${ant.test.lib}"/>
|
---|
603 | </path>
|
---|
604 |
|
---|
605 | <target name="clean" description="Delete all generated files">
|
---|
606 | <delete failonerror="false" includeEmptyDirs="true">
|
---|
607 | <fileset dir="." includes="${ant.project.name}.jar"/>
|
---|
608 | <fileset dir="${classes.dir}"/>
|
---|
609 | <fileset dir="${report.dir}"/>
|
---|
610 | </delete>
|
---|
611 | </target>
|
---|
612 |
|
---|
613 | <font color="#9F9F9F"><target name="compile" description="Compiles the Task">
|
---|
614 | <mkdir dir="${classes.dir}"/>
|
---|
615 | <javac srcdir="${src.dir}" destdir="${classes.dir}" </font>classpath="${ant.test.lib}"<font color="#9F9F9F">/>
|
---|
616 | </target></font>
|
---|
617 | ...
|
---|
618 | <target name="junit" description="Runs the unit tests" depends="jar">
|
---|
619 | <delete dir="${junit.out.dir.xml}"/>
|
---|
620 | <mkdir dir="${junit.out.dir.xml}"/>
|
---|
621 | <junit printsummary="yes" haltonfailure="no">
|
---|
622 | <classpath refid="classpath.test"/>
|
---|
623 | <formatter type="xml"/>
|
---|
624 | <batchtest fork="yes" todir="${junit.out.dir.xml}">
|
---|
625 | <fileset dir="${src.dir}" includes="**/*Test.java"/>
|
---|
626 | </batchtest>
|
---|
627 | </junit>
|
---|
628 | </target>
|
---|
629 |
|
---|
630 | <target name="junitreport" description="Create a report for the rest result">
|
---|
631 | <mkdir dir="${junit.out.dir.html}"/>
|
---|
632 | <junitreport todir="${junit.out.dir.html}">
|
---|
633 | <fileset dir="${junit.out.dir.xml}">
|
---|
634 | <include name="*.xml"/>
|
---|
635 | </fileset>
|
---|
636 | <report format="frames" todir="${junit.out.dir.html}"/>
|
---|
637 | </junitreport>
|
---|
638 | </target>
|
---|
639 |
|
---|
640 | <target name="test"
|
---|
641 | depends="junit,junitreport"
|
---|
642 | description="Runs unit tests and creates a report"
|
---|
643 | />
|
---|
644 | ...
|
---|
645 | </pre></p>
|
---|
646 |
|
---|
647 | <p>Back to the <i>src/HelloWorldTest.java</i>. We create a class extending
|
---|
648 | <i>BuildFileTest</i> with String-constructor (JUnit-standard), a <i>setUp()</i>
|
---|
649 | method initializing Ant and for each testcase (targets use.*) a <i>testXX()</i>
|
---|
650 | method invoking that target.
|
---|
651 | <pre class="code">
|
---|
652 | import org.apache.tools.ant.BuildFileTest;
|
---|
653 |
|
---|
654 | public class HelloWorldTest extends BuildFileTest {
|
---|
655 |
|
---|
656 | public HelloWorldTest(String s) {
|
---|
657 | super(s);
|
---|
658 | }
|
---|
659 |
|
---|
660 | public void setUp() {
|
---|
661 | // initialize Ant
|
---|
662 | configureProject("build.xml");
|
---|
663 | }
|
---|
664 |
|
---|
665 | public void testWithout() {
|
---|
666 | executeTarget("use.without");
|
---|
667 | assertEquals("Message was logged but should not.", getLog(), "");
|
---|
668 | }
|
---|
669 |
|
---|
670 | public void testMessage() {
|
---|
671 | // execute target 'use.nestedText' and expect a message
|
---|
672 | // 'attribute-text' in the log
|
---|
673 | expectLog("use.message", "attribute-text");
|
---|
674 | }
|
---|
675 |
|
---|
676 | public void testFail() {
|
---|
677 | // execute target 'use.fail' and expect a BuildException
|
---|
678 | // with text 'Fail requested.'
|
---|
679 | expectBuildException("use.fail", "Fail requested.");
|
---|
680 | }
|
---|
681 |
|
---|
682 | public void testNestedText() {
|
---|
683 | expectLog("use.nestedText", "nested-text");
|
---|
684 | }
|
---|
685 |
|
---|
686 | public void testNestedElement() {
|
---|
687 | executeTarget("use.nestedElement");
|
---|
688 | assertLogContaining("Nested Element 1");
|
---|
689 | assertLogContaining("Nested Element 2");
|
---|
690 | }
|
---|
691 | }
|
---|
692 | </pre></p>
|
---|
693 |
|
---|
694 | <p>When starting <tt>ant</tt> we'll get a short message to STDOUT and
|
---|
695 | a nice HTML-report.
|
---|
696 | <pre class="output">
|
---|
697 | C:\tmp\anttests\MyFirstTask>ant
|
---|
698 | Buildfile: build.xml
|
---|
699 |
|
---|
700 | compile:
|
---|
701 | [mkdir] Created dir: C:\tmp\anttests\MyFirstTask\classes
|
---|
702 | [javac] Compiling 2 source files to C:\tmp\anttests\MyFirstTask\classes
|
---|
703 |
|
---|
704 | jar:
|
---|
705 | [jar] Building jar: C:\tmp\anttests\MyFirstTask\MyTask.jar
|
---|
706 |
|
---|
707 | junit:
|
---|
708 | [mkdir] Created dir: C:\tmp\anttests\MyFirstTask\report\junit\xml
|
---|
709 | [junit] Running HelloWorldTest
|
---|
710 | [junit] Tests run: 5, Failures: 0, Errors: 0, Time elapsed: 2,334 sec
|
---|
711 |
|
---|
712 |
|
---|
713 |
|
---|
714 | junitreport:
|
---|
715 | [mkdir] Created dir: C:\tmp\anttests\MyFirstTask\report\junit\html
|
---|
716 | [junitreport] Using Xalan version: Xalan Java 2.4.1
|
---|
717 | [junitreport] Transform time: 661ms
|
---|
718 |
|
---|
719 | test:
|
---|
720 |
|
---|
721 | BUILD SUCCESSFUL
|
---|
722 | Total time: 7 seconds
|
---|
723 | C:\tmp\anttests\MyFirstTask>
|
---|
724 | </pre></p>
|
---|
725 |
|
---|
726 |
|
---|
727 | <a name="resources"></a>
|
---|
728 | <h2>Resources</h2>
|
---|
729 | <p>This tutorial and its resources are available via
|
---|
730 | <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=22570">BugZilla [6]</a>.
|
---|
731 | The ZIP provided there contains<ul>
|
---|
732 | <li>this tutorial</li>
|
---|
733 | <li>the buildfile (last version)</li>
|
---|
734 | <li>the source of the task (last version)</li>
|
---|
735 | <li>the source of the unit test (last version)</li>
|
---|
736 | <li>the ant-testutil.jar (nightly build of 2003-08-18)</li>
|
---|
737 | <li>generated classes</li>
|
---|
738 | <li>generated jar</li>
|
---|
739 | <li>generated reports</li>
|
---|
740 | </ul>
|
---|
741 | The last sources and the buildfile are also available
|
---|
742 | <a href="tutorial-writing-tasks-src.zip">here [7]</a> inside the manual.
|
---|
743 | </p>
|
---|
744 |
|
---|
745 |
|
---|
746 | Used Links:<br/>
|
---|
747 | [1] <a href="http://ant.apache.org/manual/using.html#built-in-props">http://ant.apache.org/manual/using.html#built-in-props</a><br>
|
---|
748 | [2] <a href="http://ant.apache.org/manual/CoreTasks/taskdef.html">http://ant.apache.org/manual/CoreTasks/taskdef.html</a><br>
|
---|
749 | [3] <a href="http://ant.apache.org/manual/develop.html#set-magic">http://ant.apache.org/manual/develop.html#set-magic</a><br>
|
---|
750 | [4] <a href="http://ant.apache.org/manual/develop.html#nested-elements">http://ant.apache.org/manual/develop.html#nested-elements</a><br>
|
---|
751 | [5] <a href="http://gump.covalent.net/jars/latest/ant/ant-testutil.jar">http://gump.covalent.net/jars/latest/ant/ant-testutil.jar</a><br>
|
---|
752 | [6] <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=22570">http://issues.apache.org/bugzilla/show_bug.cgi?id=22570</a><br>
|
---|
753 | [7] <a href="tutorial-writing-tasks-src.zip">tutorial-writing-tasks-src.zip</a><br>
|
---|
754 |
|
---|
755 | <hr>
|
---|
756 | <p align="center">Copyright © 2003-2005 The Apache Software Foundation. All rights
|
---|
757 | Reserved.</p>
|
---|
758 |
|
---|
759 | </body>
|
---|
760 | </html>
|
---|