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