2006-09-11 04:19:00 +00:00
|
|
|
<!--
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
-->
|
2003-09-06 15:13:00 +00:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Tutorial: Writing Tasks</title>
|
2007-05-02 06:56:16 +00:00
|
|
|
<link rel="stylesheet" type="text/css" href="stylesheets/style.css">
|
2003-09-06 15:13:00 +00:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Tutorial: Writing Tasks</h1>
|
|
|
|
|
|
|
|
<p>This document provides a step by step tutorial for writing
|
|
|
|
tasks.</p>
|
|
|
|
<h2>Content</h2>
|
|
|
|
<p><ul>
|
|
|
|
<li><a href="#buildenvironment">Set up the build environment</a></li>
|
|
|
|
<li><a href="#write1">Write the Task</a></li>
|
|
|
|
<li><a href="#use1">Use the Task</a></li>
|
|
|
|
<li><a href="#TaskAdapter">Integration with TaskAdapter</a></li>
|
2003-12-23 15:57:02 +00:00
|
|
|
<li><a href="#derivingFromTask">Deriving from Ant's Task</a></li>
|
2003-09-06 15:13:00 +00:00
|
|
|
<li><a href="#attributes">Attributes</a></li>
|
|
|
|
<li><a href="#NestedText">Nested Text</a></li>
|
|
|
|
<li><a href="#NestedElements">Nested Elements</a></li>
|
|
|
|
<li><a href="#complex">Our task in a little more complex version</a></li>
|
|
|
|
<li><a href="#TestingTasks">Test the Task</a></li>
|
|
|
|
<li><a href="#resources">Resources</a></li>
|
|
|
|
</ul></p>
|
|
|
|
|
2003-12-23 15:57:02 +00:00
|
|
|
<a name="buildenvironment"></a>
|
2003-09-06 15:13:00 +00:00
|
|
|
<h2>Set up the build environment</h2>
|
|
|
|
<p>Ant builds itself, we are using Ant too (why we would write
|
|
|
|
a task if not? :-) therefore we should use Ant for our build.<p>
|
|
|
|
<p>We choose a directory as root directory. All things will be done
|
|
|
|
here if I say nothing different. I will reference this directory
|
|
|
|
as <i>root-directory</i> of our project. In this root-directory we
|
|
|
|
create a text file names <i>build.xml</i>. What should Ant do for us?
|
|
|
|
<ul>
|
|
|
|
<li>compiles my stuff</li>
|
|
|
|
<li>make the jar, so that I can deploy it</li>
|
|
|
|
<li>clean up everything</li>
|
|
|
|
</ul>
|
|
|
|
So the buildfile contains three targets.
|
|
|
|
<pre class="code">
|
2004-11-16 08:12:35 +00:00
|
|
|
<?xml version="1.0" encoding="ISO-8859-1"?>
|
|
|
|
<project name="MyTask" basedir="." default="jar">
|
2003-09-06 15:13:00 +00:00
|
|
|
|
2004-11-16 08:12:35 +00:00
|
|
|
<target name="clean" description="Delete all generated files">
|
|
|
|
<delete dir="classes"/>
|
|
|
|
<delete file="MyTasks.jar"/>
|
|
|
|
</target>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
2004-11-16 08:12:35 +00:00
|
|
|
<target name="compile" description="Compiles the Task">
|
|
|
|
<javac srcdir="src" destdir="classes"/>
|
|
|
|
</target>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
2004-11-16 08:12:35 +00:00
|
|
|
<target name="jar" description="JARs the Task">
|
|
|
|
<jar destfile="MyTask.jar" basedir="classes"/>
|
|
|
|
</target>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
2004-11-16 08:12:35 +00:00
|
|
|
</project>
|
2003-09-06 15:13:00 +00:00
|
|
|
</pre>
|
|
|
|
|
|
|
|
This buildfile uses often the same value (src, classes, MyTask.jar), so we should rewrite that
|
2004-11-19 09:07:12 +00:00
|
|
|
using <code><property></code>s. On second there are some handicaps: <code><javac></code> requires that the destination
|
2003-09-06 15:13:00 +00:00
|
|
|
directory exists; a call of "clean" with a non existing classes directory will fail; "jar" requires
|
|
|
|
the execution of some steps bofore. So the refactored code is:
|
|
|
|
|
|
|
|
<pre class="code">
|
2004-11-16 08:12:35 +00:00
|
|
|
<?xml version="1.0" encoding="ISO-8859-1"?>
|
|
|
|
<project name="MyTask" basedir="." default="jar">
|
2003-09-06 15:13:00 +00:00
|
|
|
|
2004-11-16 08:12:35 +00:00
|
|
|
<b><property name="src.dir" value="src"/></b>
|
|
|
|
<b><property name="classes.dir" value="classes"/></b>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
2004-11-16 08:12:35 +00:00
|
|
|
<target name="clean" description="Delete all generated files">
|
|
|
|
<delete dir="<b>${classes.dir}</b>" <b>failonerror="false"</b>/>
|
|
|
|
<delete file="<b>${ant.project.name}.jar</b>"/>
|
|
|
|
</target>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
2004-11-16 08:12:35 +00:00
|
|
|
<target name="compile" description="Compiles the Task">
|
|
|
|
<b><mkdir dir="${classes.dir}"/></b>
|
|
|
|
<javac srcdir="<b>${src.dir}</b>" destdir="${classes.dir}"/>
|
|
|
|
</target>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
2004-11-16 08:12:35 +00:00
|
|
|
<target name="jar" description="JARs the Task" <b>depends="compile"</b>>
|
|
|
|
<jar destfile="${ant.project.name}.jar" basedir="${classes.dir}"/>
|
|
|
|
</target>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
2004-11-16 08:12:35 +00:00
|
|
|
</project>
|
2003-09-06 15:13:00 +00:00
|
|
|
</pre>
|
|
|
|
<i>ant.project.name</i> is one of the
|
|
|
|
<a href="http://ant.apache.org/manual/using.html#built-in-props" target="_blank">
|
|
|
|
build-in properties [1]</a> of Ant.
|
|
|
|
|
|
|
|
|
2003-12-23 15:57:02 +00:00
|
|
|
<a name="write1"></a>
|
2003-09-06 15:13:00 +00:00
|
|
|
<h2>Write the Task</h2>
|
|
|
|
|
|
|
|
Now we write the simplest Task - a HelloWorld-Task (what else?). Create a text file
|
|
|
|
<i>HelloWorld.java</i> in the src-directory with:
|
|
|
|
<pre class="code">
|
|
|
|
public class HelloWorld {
|
|
|
|
public void execute() {
|
|
|
|
System.out.println("Hello World");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</pre>
|
|
|
|
and we can compile and jar it with <tt>ant</tt> (default target is "jar" and via
|
|
|
|
its <i>depends</i>-clause the "compile" is executed before).
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-12-23 15:57:02 +00:00
|
|
|
<a name="use1"></a>
|
2003-09-06 15:13:00 +00:00
|
|
|
<h2>Use the Task</h2>
|
|
|
|
<p>But after creating the jar we want to use our new Task. Therefore we need a
|
|
|
|
new target "use". Before we can use our new task we have to declare it with
|
|
|
|
<a href="http://ant.apache.org/manual/CoreTasks/taskdef.html" target="_blank">
|
2004-11-19 09:07:12 +00:00
|
|
|
<code><taskdef></code> [2]</a>. And for easier process we change the default clause:
|
2003-09-06 15:13:00 +00:00
|
|
|
<pre class="code">
|
2004-11-16 08:12:35 +00:00
|
|
|
<?xml version="1.0" encoding="ISO-8859-1"?>
|
|
|
|
<project name="MyTask" basedir="." default="<b>use</b>">
|
2003-09-06 15:13:00 +00:00
|
|
|
|
|
|
|
...
|
|
|
|
|
2004-11-16 08:12:35 +00:00
|
|
|
<b><target name="use" description="Use the Task" depends="jar">
|
|
|
|
<taskdef name="helloworld" classname="HelloWorld" classpath="${ant.project.name}.jar"/>
|
|
|
|
<helloworld/>
|
|
|
|
</target></b>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
2004-11-16 08:12:35 +00:00
|
|
|
</project>
|
2003-09-06 15:13:00 +00:00
|
|
|
</pre>
|
|
|
|
|
|
|
|
Important is the <i>classpath</i>-attribute. Ant searches in its /lib directory for
|
2003-12-23 15:57:02 +00:00
|
|
|
tasks and our task isn't there. So we have to provide the right location. </p>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
|
|
|
<p>Now we can type in <tt>ant</tt> and all should work ...
|
|
|
|
<pre class="output">
|
|
|
|
Buildfile: build.xml
|
|
|
|
|
|
|
|
compile:
|
|
|
|
[mkdir] Created dir: C:\tmp\anttests\MyFirstTask\classes
|
|
|
|
[javac] Compiling 1 source file to C:\tmp\anttests\MyFirstTask\classes
|
|
|
|
|
|
|
|
jar:
|
|
|
|
[jar] Building jar: C:\tmp\anttests\MyFirstTask\MyTask.jar
|
|
|
|
|
|
|
|
use:
|
|
|
|
[helloworld] Hello World
|
|
|
|
|
|
|
|
BUILD SUCCESSFUL
|
|
|
|
Total time: 3 seconds
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-12-23 15:57:02 +00:00
|
|
|
<a name="TaskAdapter"></a>
|
2003-09-06 15:13:00 +00:00
|
|
|
<h2>Integration with TaskAdapter</h2>
|
|
|
|
<p>Our class has nothing to do with Ant. It extends no superclass and implements
|
|
|
|
no interface. How does Ant know to integrate? Via name convention: our class provides
|
2003-12-23 15:57:02 +00:00
|
|
|
a method with signature <tt>public void execute()</tt>. This class is wrapped by Ant's
|
2003-09-06 15:13:00 +00:00
|
|
|
<tt>org.apache.tools.ant.TaskAdapter</tt> which is a task and uses reflection for
|
|
|
|
setting a reference to the project and calling the <i>execute()</i> method.</p>
|
|
|
|
|
|
|
|
<p><i>Setting a reference to the project</i>? Could be interesting. The Project class
|
2003-12-23 15:57:02 +00:00
|
|
|
gives us some nice abilities: access to Ant's logging facilities getting and setting
|
2003-09-06 15:13:00 +00:00
|
|
|
properties and much more. So we try to use that class:
|
|
|
|
<pre class="code">
|
|
|
|
import org.apache.tools.ant.Project;
|
|
|
|
|
|
|
|
public class HelloWorld {
|
|
|
|
|
|
|
|
private Project project;
|
|
|
|
|
|
|
|
public void setProject(Project proj) {
|
|
|
|
project = proj;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void execute() {
|
|
|
|
String message = project.getProperty("ant.project.name");
|
|
|
|
project.log("Here is project '" + message + "'.", Project.MSG_INFO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</pre>
|
|
|
|
and the execution with <tt>ant</tt> will show us the expected
|
|
|
|
<pre class="output">
|
|
|
|
use:
|
|
|
|
Here is project 'MyTask'.
|
|
|
|
</pre></p>
|
|
|
|
|
|
|
|
|
2003-12-23 15:57:02 +00:00
|
|
|
<a name="derivingFromTask"></a>
|
|
|
|
<h2>Deriving from Ant's Task</h2>
|
2003-09-06 15:13:00 +00:00
|
|
|
<p>Ok, that works ... But usually you will extend <tt>org.apache.tools.ant.Task</tt>.
|
2003-12-23 15:57:02 +00:00
|
|
|
That class is integrated in Ant, get's the project-reference, provides documentation
|
2003-09-06 15:13:00 +00:00
|
|
|
fiels, provides easier access to the logging facility and (very useful) gives you
|
|
|
|
the exact location where <i>in the buildfile</i> this task instance is used.</p>
|
|
|
|
|
2003-12-23 15:57:02 +00:00
|
|
|
<p>Oki-doki - let's us use some of these:
|
2003-09-06 15:13:00 +00:00
|
|
|
<pre class="code">
|
|
|
|
import org.apache.tools.ant.Task;
|
|
|
|
|
|
|
|
public class HelloWorld extends Task {
|
|
|
|
public void execute() {
|
|
|
|
// use of the reference to Project-instance
|
|
|
|
String message = getProject().getProperty("ant.project.name");
|
|
|
|
|
2003-12-23 15:57:02 +00:00
|
|
|
// Task's log method
|
2003-09-06 15:13:00 +00:00
|
|
|
log("Here is project '" + message + "'.");
|
|
|
|
|
|
|
|
// where this task is used?
|
|
|
|
log("I am used in: " + getLocation() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</pre>
|
|
|
|
which gives us when running
|
|
|
|
<pre class="output">
|
|
|
|
use:
|
|
|
|
[helloworld] Here is project 'MyTask'.
|
|
|
|
[helloworld] I am used in: C:\tmp\anttests\MyFirstTask\build.xml:23:
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
|
|
|
|
<a name="attributes">
|
|
|
|
<h2>Attributes</h2>
|
|
|
|
<p>Now we want to specify the text of our message (it seems that we are
|
2004-11-19 09:07:12 +00:00
|
|
|
rewriting the <code><echo/></code> task :-). First we well do that with an attribute.
|
|
|
|
It is very easy - for each attribute provide a <tt>public void set<code><attributename></code>(<code><type></code>
|
2003-09-06 15:13:00 +00:00
|
|
|
newValue)</tt> method and Ant will do the rest via reflection.</p>
|
|
|
|
<pre class="code">
|
|
|
|
import org.apache.tools.ant.Task;
|
|
|
|
import org.apache.tools.ant.BuildException;
|
|
|
|
|
|
|
|
public class HelloWorld extends Task {
|
|
|
|
|
|
|
|
String message;
|
|
|
|
public void setMessage(String msg) {
|
|
|
|
message = msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void execute() {
|
|
|
|
if (message==null) {
|
|
|
|
throw new BuildException("No message set.");
|
|
|
|
}
|
|
|
|
log(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
</pre>
|
2003-12-23 15:57:02 +00:00
|
|
|
<p>Oh, what's that in execute()? Throw a <i>BuildException</i>? Yes, that's the usual
|
2003-09-06 15:13:00 +00:00
|
|
|
way to show Ant that something important is missed and complete build should fail. The
|
2003-12-23 15:57:02 +00:00
|
|
|
string provided there is written as build-failes-message. Here it's necessary because
|
|
|
|
the log() method can't handle a <i>null</i> value as parameter and throws a NullPointerException.
|
2003-09-06 15:13:00 +00:00
|
|
|
(Of course you can initialize the <i>message</i> with a default string.)</p>
|
|
|
|
|
|
|
|
<p>After that we have to modify our buildfile:
|
|
|
|
<pre class="code">
|
2004-11-16 08:12:35 +00:00
|
|
|
<target name="use" description="Use the Task" depends="jar">
|
2003-09-06 15:13:00 +00:00
|
|
|
<taskdef name="helloworld"
|
|
|
|
classname="HelloWorld"
|
2004-11-16 08:12:35 +00:00
|
|
|
classpath="${ant.project.name}.jar"/>
|
|
|
|
<helloworld <b>message="Hello World"</b>/>
|
|
|
|
</target>
|
2003-09-06 15:13:00 +00:00
|
|
|
</pre>
|
2003-12-23 15:57:02 +00:00
|
|
|
That's all.</p>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
|
|
|
<p>Some background for working with attributes: Ant supports any of these datatypes as
|
|
|
|
arguments of the set-method:<ul>
|
|
|
|
<li>elementary data type like <i>int</i>, <i>long</i>, ...</li>
|
|
|
|
<li>its wrapper classes like <i>java.lang.Integer</i>, <i>java.lang.Long</i>, ...</li>
|
|
|
|
<li><i>java.lang.String</i></li>
|
|
|
|
<li>some more classes (e.g. <i>java.io.File</i>; see
|
2008-07-16 17:18:02 +00:00
|
|
|
<a href="develop.html#set-magic">Manual
|
2003-09-06 15:13:00 +00:00
|
|
|
'Writing Your Own Task' [3]</a>)</li>
|
2008-07-16 17:18:02 +00:00
|
|
|
<li>Any Java Object parsed from Ant 1.8's <a href="using.html#propertyhelper">Property
|
|
|
|
Helper</a></li>
|
2003-09-06 15:13:00 +00:00
|
|
|
</ul>
|
2004-11-16 08:12:35 +00:00
|
|
|
Before calling the set-method all properties are resolved. So a <tt><helloworld message="${msg}"/></tt>
|
2003-09-06 15:13:00 +00:00
|
|
|
would not set the message string to "${msg}" if there is a property "msg" with a set value.
|
|
|
|
|
|
|
|
|
2003-12-23 15:57:02 +00:00
|
|
|
<a name="NestedText"></a>
|
2003-09-06 15:13:00 +00:00
|
|
|
<h2>Nested Text</h2>
|
2004-11-19 09:07:12 +00:00
|
|
|
<p>Maybe you have used the <code><echo></code> task in a way like <tt><echo>Hello World</echo></tt>.
|
2003-09-06 15:13:00 +00:00
|
|
|
For that you have to provide a <tt>public void addText(String text)</tt> method.
|
|
|
|
<pre class="code">
|
|
|
|
...
|
|
|
|
public class HelloWorld extends Task {
|
|
|
|
...
|
|
|
|
public void addText(String text) {
|
|
|
|
message = text;
|
|
|
|
}
|
|
|
|
...
|
|
|
|
}
|
|
|
|
</pre>
|
|
|
|
But here properties are <b>not</b> resolved! For resolving properties we have to use
|
2003-12-23 15:57:02 +00:00
|
|
|
Project's <tt>replaceProperties(String propname) : String</tt> method which takes the
|
2003-09-06 15:13:00 +00:00
|
|
|
property name as argument and returns its value (or ${propname} if not set).</p>
|
|
|
|
|
|
|
|
|
2003-12-23 15:57:02 +00:00
|
|
|
<a name="NestedElements"></a>
|
2003-09-06 15:13:00 +00:00
|
|
|
<h2>Nested Elements</h2>
|
|
|
|
<p>There are several ways for inserting the ability of handling nested elements. See
|
|
|
|
the <a href="http://ant.apache.org/manual/develop.html#nested-elements">Manual [4]</a> for other.
|
|
|
|
We use the first way of the three described ways. There are several steps for that:<ol>
|
2007-05-10 03:16:31 +00:00
|
|
|
<li>We create a class for collecting all the info the nested element should contain.
|
2003-09-06 15:13:00 +00:00
|
|
|
This class is created by the same rules for attributes and nested elements
|
2004-11-19 09:07:12 +00:00
|
|
|
as for the task (<code>set<attributename></code>() methods). </li>
|
2003-09-06 15:13:00 +00:00
|
|
|
<li>The task holds multiple instances of this class in a list.</li>
|
|
|
|
<li>A factory method instantiates an object, saves the reference in the list
|
|
|
|
and returns it to Ant Core.</li>
|
|
|
|
<li>The execute() method iterates over the list and evaluates its values.</li>
|
2007-05-10 03:16:31 +00:00
|
|
|
</ol></p>
|
2003-09-06 15:13:00 +00:00
|
|
|
<pre class="code">
|
|
|
|
import java.util.Vector;
|
|
|
|
import java.util.Iterator;
|
|
|
|
...
|
|
|
|
public void execute() {
|
|
|
|
if (message!=null) log(message);
|
|
|
|
for (Iterator it=messages.iterator(); it.hasNext(); ) { <b>// 4</b>
|
|
|
|
Message msg = (Message)it.next();
|
|
|
|
log(msg.getMsg());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Vector messages = new Vector(); <b>// 2</b>
|
|
|
|
|
|
|
|
public Message createMessage() { <b>// 3</b>
|
|
|
|
Message msg = new Message();
|
|
|
|
messages.add(msg);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Message { <b>// 1</b>
|
|
|
|
public Message() {}
|
|
|
|
|
|
|
|
String msg;
|
|
|
|
public void setMsg(String msg) { this.msg = msg; }
|
|
|
|
public String getMsg() { return msg; }
|
|
|
|
}
|
|
|
|
...
|
|
|
|
</pre>
|
|
|
|
<p>Then we can use the new nested element. But where is xml-name for that defined?
|
|
|
|
The mapping XML-name : classname is defined in the factory method:
|
|
|
|
<tt>public <i>classname</i> create<i>XML-name</i>()</tt>. Therefore we write in
|
|
|
|
the buildfile
|
|
|
|
<pre class="code">
|
2004-11-16 08:12:35 +00:00
|
|
|
<helloworld>
|
|
|
|
<message msg="Nested Element 1"/>
|
|
|
|
<message msg="Nested Element 2"/>
|
|
|
|
</helloworld>
|
2003-09-06 15:13:00 +00:00
|
|
|
</pre>
|
2007-05-10 03:16:31 +00:00
|
|
|
<p>Note that if you choose to use methods 2 or 3, the class that represents the nested
|
|
|
|
element must be declared as <pre>static</pre></p>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
2003-12-23 15:57:02 +00:00
|
|
|
<a name="complex"></a>
|
2003-09-06 15:13:00 +00:00
|
|
|
<h2>Our task in a little more complex version</h2>
|
|
|
|
<p>For recapitulation now a little refactored buildfile:
|
|
|
|
<pre class="code">
|
2004-11-16 08:12:35 +00:00
|
|
|
<?xml version="1.0" encoding="ISO-8859-1"?>
|
|
|
|
<project name="MyTask" basedir="." default="use">
|
2003-09-06 15:13:00 +00:00
|
|
|
|
2004-11-16 08:12:35 +00:00
|
|
|
<property name="src.dir" value="src"/>
|
|
|
|
<property name="classes.dir" value="classes"/>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
2004-11-16 08:12:35 +00:00
|
|
|
<target name="clean" description="Delete all generated files">
|
|
|
|
<delete dir="${classes.dir}" failonerror="false"/>
|
|
|
|
<delete file="${ant.project.name}.jar"/>
|
|
|
|
</target>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
2004-11-16 08:12:35 +00:00
|
|
|
<target name="compile" description="Compiles the Task">
|
|
|
|
<mkdir dir="${classes.dir}"/>
|
|
|
|
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
|
|
|
|
</target>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
2004-11-16 08:12:35 +00:00
|
|
|
<target name="jar" description="JARs the Task" depends="compile">
|
|
|
|
<jar destfile="${ant.project.name}.jar" basedir="${classes.dir}"/>
|
|
|
|
</target>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
<target name="use.init"
|
|
|
|
description="Taskdef the HelloWorld-Task"
|
2004-11-16 08:12:35 +00:00
|
|
|
depends="jar">
|
2003-09-06 15:13:00 +00:00
|
|
|
<taskdef name="helloworld"
|
|
|
|
classname="HelloWorld"
|
2004-11-16 08:12:35 +00:00
|
|
|
classpath="${ant.project.name}.jar"/>
|
|
|
|
</target>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
<target name="use.without"
|
|
|
|
description="Use without any"
|
2004-11-16 08:12:35 +00:00
|
|
|
depends="use.init">
|
|
|
|
<helloworld/>
|
|
|
|
</target>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
|
|
|
<target name="use.message"
|
|
|
|
description="Use with attribute 'message'"
|
2004-11-16 08:12:35 +00:00
|
|
|
depends="use.init">
|
|
|
|
<helloworld message="attribute-text"/>
|
|
|
|
</target>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
|
|
|
<target name="use.fail"
|
|
|
|
description="Use with attribute 'fail'"
|
2004-11-16 08:12:35 +00:00
|
|
|
depends="use.init">
|
|
|
|
<helloworld fail="true"/>
|
|
|
|
</target>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
|
|
|
<target name="use.nestedText"
|
|
|
|
description="Use with nested text"
|
2004-11-16 08:12:35 +00:00
|
|
|
depends="use.init">
|
|
|
|
<helloworld>nested-text</helloworld>
|
|
|
|
</target>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
|
|
|
<target name="use.nestedElement"
|
|
|
|
description="Use with nested 'message'"
|
2004-11-16 08:12:35 +00:00
|
|
|
depends="use.init">
|
|
|
|
<helloworld>
|
|
|
|
<message msg="Nested Element 1"/>
|
|
|
|
<message msg="Nested Element 2"/>
|
|
|
|
</helloworld>
|
|
|
|
</target>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
<target name="use"
|
|
|
|
description="Try all (w/out use.fail)"
|
|
|
|
depends="use.without,use.message,use.nestedText,use.nestedElement"
|
2004-11-16 08:12:35 +00:00
|
|
|
/>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
2004-11-16 08:12:35 +00:00
|
|
|
</project>
|
2003-09-06 15:13:00 +00:00
|
|
|
</pre>
|
|
|
|
|
|
|
|
And the code of the task:
|
|
|
|
<pre class="code">
|
|
|
|
import org.apache.tools.ant.Task;
|
|
|
|
import org.apache.tools.ant.BuildException;
|
|
|
|
import java.util.Vector;
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The task of the tutorial.
|
2003-12-23 15:57:02 +00:00
|
|
|
* Print a message or let the build fail.
|
2003-09-06 15:13:00 +00:00
|
|
|
* @since 2003-08-19
|
|
|
|
*/
|
|
|
|
public class HelloWorld extends Task {
|
|
|
|
|
|
|
|
|
|
|
|
/** The message to print. As attribute. */
|
|
|
|
String message;
|
|
|
|
public void setMessage(String msg) {
|
|
|
|
message = msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Should the build fail? Defaults to <i>false</i>. As attribute. */
|
|
|
|
boolean fail = false;
|
|
|
|
public void setFail(boolean b) {
|
|
|
|
fail = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Support for nested text. */
|
|
|
|
public void addText(String text) {
|
|
|
|
message = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Do the work. */
|
|
|
|
public void execute() {
|
|
|
|
// handle attribute 'fail'
|
|
|
|
if (fail) throw new BuildException("Fail requested.");
|
|
|
|
|
|
|
|
// handle attribute 'message' and nested text
|
|
|
|
if (message!=null) log(message);
|
|
|
|
|
|
|
|
// handle nested elements
|
|
|
|
for (Iterator it=messages.iterator(); it.hasNext(); ) {
|
|
|
|
Message msg = (Message)it.next();
|
|
|
|
log(msg.getMsg());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Store nested 'message's. */
|
|
|
|
Vector messages = new Vector();
|
|
|
|
|
|
|
|
/** Factory method for creating nested 'message's. */
|
|
|
|
public Message createMessage() {
|
|
|
|
Message msg = new Message();
|
|
|
|
messages.add(msg);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A nested 'message'. */
|
|
|
|
public class Message {
|
|
|
|
// Bean constructor
|
|
|
|
public Message() {}
|
|
|
|
|
|
|
|
/** Message to print. */
|
|
|
|
String msg;
|
|
|
|
public void setMsg(String msg) { this.msg = msg; }
|
|
|
|
public String getMsg() { return msg; }
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
And it works:
|
|
|
|
<pre class="output">
|
2004-11-16 08:12:35 +00:00
|
|
|
C:\tmp\anttests\MyFirstTask>ant
|
2003-09-06 15:13:00 +00:00
|
|
|
Buildfile: build.xml
|
|
|
|
|
|
|
|
compile:
|
|
|
|
[mkdir] Created dir: C:\tmp\anttests\MyFirstTask\classes
|
|
|
|
[javac] Compiling 1 source file to C:\tmp\anttests\MyFirstTask\classes
|
|
|
|
|
|
|
|
jar:
|
|
|
|
[jar] Building jar: C:\tmp\anttests\MyFirstTask\MyTask.jar
|
|
|
|
|
|
|
|
use.init:
|
|
|
|
|
|
|
|
use.without:
|
|
|
|
|
|
|
|
use.message:
|
|
|
|
[helloworld] attribute-text
|
|
|
|
|
|
|
|
use.nestedText:
|
|
|
|
[helloworld] nested-text
|
|
|
|
|
|
|
|
use.nestedElement:
|
|
|
|
[helloworld]
|
|
|
|
[helloworld]
|
|
|
|
[helloworld]
|
|
|
|
[helloworld]
|
|
|
|
[helloworld] Nested Element 1
|
|
|
|
[helloworld] Nested Element 2
|
|
|
|
|
|
|
|
use:
|
|
|
|
|
|
|
|
BUILD SUCCESSFUL
|
|
|
|
Total time: 3 seconds
|
2004-11-16 08:12:35 +00:00
|
|
|
C:\tmp\anttests\MyFirstTask>ant use.fail
|
2003-09-06 15:13:00 +00:00
|
|
|
Buildfile: build.xml
|
|
|
|
|
|
|
|
compile:
|
|
|
|
|
|
|
|
jar:
|
|
|
|
|
|
|
|
use.init:
|
|
|
|
|
|
|
|
use.fail:
|
|
|
|
|
|
|
|
BUILD FAILED
|
|
|
|
C:\tmp\anttests\MyFirstTask\build.xml:36: Fail requested.
|
|
|
|
|
|
|
|
Total time: 1 second
|
2004-11-16 08:12:35 +00:00
|
|
|
C:\tmp\anttests\MyFirstTask>
|
2003-09-06 15:13:00 +00:00
|
|
|
</pre>
|
|
|
|
Next step: test ...
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-12-23 15:57:02 +00:00
|
|
|
<a name="TestingTasks"></a>
|
2003-09-06 15:13:00 +00:00
|
|
|
<h2>Test the Task</h2>
|
|
|
|
<p>We have written a test already: the use.* tasks in the buildfile. But its
|
|
|
|
difficult to test that automatically. Common (and in Ant) used is JUnit for
|
|
|
|
that. For testing tasks Ant provides a baseclass <tt>org.apache.tools.ant.BuildFileTest</tt>.
|
|
|
|
This class extends <tt>junit.framework.TestCase</tt> and can therefore be integrated
|
|
|
|
into the unit tests. But this class provides some for testing tasks useful methods:
|
|
|
|
initialize Ant, load a buildfile, execute targets,
|
|
|
|
expecting BuildExceptions with a specified text, expect a special text
|
|
|
|
in the output log ... </p>
|
|
|
|
|
|
|
|
<p>In Ant it is usual that the testcase has the same name as the task with a prepending
|
|
|
|
<i>Test</i>, therefore we will create a file <i>HelloWorldTest.java</i>. Because we
|
2003-12-23 15:57:02 +00:00
|
|
|
have a very small project we can put this file into <i>src</i> directory (Ant's own
|
2003-09-06 15:13:00 +00:00
|
|
|
testclasses are in /src/testcases/...). Because we have already written our tests
|
|
|
|
for "hand-test" we can use that for automatic tests, too. But there is one little
|
|
|
|
problem we have to solve: all test supporting classes are not part of the binary
|
|
|
|
distribution of Ant. So you can build the special jar file from source distro with
|
|
|
|
target "test-jar" or you can download a nightly build from
|
|
|
|
<a href="http://gump.covalent.net/jars/latest/ant/ant-testutil.jar">
|
|
|
|
http://gump.covalent.net/jars/latest/ant/ant-testutil.jar [5]</a>.</p>
|
|
|
|
|
2004-11-19 09:07:12 +00:00
|
|
|
<p>For executing the test and creating a report we need the optional tasks <code><junit></code>
|
|
|
|
and <code><junitreport></code>. So we add to the buildfile:
|
2003-09-06 15:13:00 +00:00
|
|
|
<pre class="code">
|
|
|
|
...
|
|
|
|
<font color="#9F9F9F"><project name="MyTask" basedir="." </font>default="test"<font color="#9F9F9F">></font>
|
|
|
|
...
|
2004-11-16 08:12:35 +00:00
|
|
|
<property name="ant.test.lib" value="ant-testutil.jar"/>
|
|
|
|
<property name="report.dir" value="report"/>
|
|
|
|
<property name="junit.out.dir.xml" value="${report.dir}/junit/xml"/>
|
|
|
|
<property name="junit.out.dir.html" value="${report.dir}/junit/html"/>
|
|
|
|
|
|
|
|
<path id="classpath.run">
|
|
|
|
<path path="${java.class.path}"/>
|
|
|
|
<path location="${ant.project.name}.jar"/>
|
|
|
|
</path>
|
|
|
|
|
|
|
|
<path id="classpath.test">
|
|
|
|
<path refid="classpath.run"/>
|
|
|
|
<path location="${ant.test.lib}"/>
|
|
|
|
</path>
|
|
|
|
|
|
|
|
<target name="clean" description="Delete all generated files">
|
|
|
|
<delete failonerror="false" includeEmptyDirs="true">
|
|
|
|
<fileset dir="." includes="${ant.project.name}.jar"/>
|
|
|
|
<fileset dir="${classes.dir}"/>
|
|
|
|
<fileset dir="${report.dir}"/>
|
|
|
|
</delete>
|
|
|
|
</target>
|
|
|
|
|
|
|
|
<font color="#9F9F9F"><target name="compile" description="Compiles the Task">
|
|
|
|
<mkdir dir="${classes.dir}"/>
|
|
|
|
<javac srcdir="${src.dir}" destdir="${classes.dir}" </font>classpath="${ant.test.lib}"<font color="#9F9F9F">/>
|
|
|
|
</target></font>
|
2003-09-06 15:13:00 +00:00
|
|
|
...
|
2004-11-16 08:12:35 +00:00
|
|
|
<target name="junit" description="Runs the unit tests" depends="jar">
|
|
|
|
<delete dir="${junit.out.dir.xml}"/>
|
|
|
|
<mkdir dir="${junit.out.dir.xml}"/>
|
|
|
|
<junit printsummary="yes" haltonfailure="no">
|
|
|
|
<classpath refid="classpath.test"/>
|
|
|
|
<formatter type="xml"/>
|
|
|
|
<batchtest fork="yes" todir="${junit.out.dir.xml}">
|
|
|
|
<fileset dir="${src.dir}" includes="**/*Test.java"/>
|
|
|
|
</batchtest>
|
|
|
|
</junit>
|
|
|
|
</target>
|
|
|
|
|
|
|
|
<target name="junitreport" description="Create a report for the rest result">
|
|
|
|
<mkdir dir="${junit.out.dir.html}"/>
|
|
|
|
<junitreport todir="${junit.out.dir.html}">
|
|
|
|
<fileset dir="${junit.out.dir.xml}">
|
|
|
|
<include name="*.xml"/>
|
|
|
|
</fileset>
|
|
|
|
<report format="frames" todir="${junit.out.dir.html}"/>
|
|
|
|
</junitreport>
|
|
|
|
</target>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
|
|
|
<target name="test"
|
|
|
|
depends="junit,junitreport"
|
|
|
|
description="Runs unit tests and creates a report"
|
2004-11-16 08:12:35 +00:00
|
|
|
/>
|
2003-09-06 15:13:00 +00:00
|
|
|
...
|
|
|
|
</pre></p>
|
|
|
|
|
|
|
|
<p>Back to the <i>src/HelloWorldTest.java</i>. We create a class extending
|
|
|
|
<i>BuildFileTest</i> with String-constructor (JUnit-standard), a <i>setUp()</i>
|
|
|
|
method initializing Ant and for each testcase (targets use.*) a <i>testXX()</i>
|
|
|
|
method invoking that target.
|
|
|
|
<pre class="code">
|
|
|
|
import org.apache.tools.ant.BuildFileTest;
|
|
|
|
|
|
|
|
public class HelloWorldTest extends BuildFileTest {
|
|
|
|
|
|
|
|
public HelloWorldTest(String s) {
|
|
|
|
super(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setUp() {
|
|
|
|
// initialize Ant
|
|
|
|
configureProject("build.xml");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testWithout() {
|
|
|
|
executeTarget("use.without");
|
|
|
|
assertEquals("Message was logged but should not.", getLog(), "");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testMessage() {
|
|
|
|
// execute target 'use.nestedText' and expect a message
|
|
|
|
// 'attribute-text' in the log
|
|
|
|
expectLog("use.message", "attribute-text");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testFail() {
|
|
|
|
// execute target 'use.fail' and expect a BuildException
|
|
|
|
// with text 'Fail requested.'
|
|
|
|
expectBuildException("use.fail", "Fail requested.");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testNestedText() {
|
|
|
|
expectLog("use.nestedText", "nested-text");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testNestedElement() {
|
|
|
|
executeTarget("use.nestedElement");
|
|
|
|
assertLogContaining("Nested Element 1");
|
|
|
|
assertLogContaining("Nested Element 2");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</pre></p>
|
|
|
|
|
2003-12-23 15:57:02 +00:00
|
|
|
<p>When starting <tt>ant</tt> we'll get a short message to STDOUT and
|
2003-09-06 15:13:00 +00:00
|
|
|
a nice HTML-report.
|
|
|
|
<pre class="output">
|
2004-11-16 08:12:35 +00:00
|
|
|
C:\tmp\anttests\MyFirstTask>ant
|
2003-09-06 15:13:00 +00:00
|
|
|
Buildfile: build.xml
|
|
|
|
|
|
|
|
compile:
|
|
|
|
[mkdir] Created dir: C:\tmp\anttests\MyFirstTask\classes
|
|
|
|
[javac] Compiling 2 source files to C:\tmp\anttests\MyFirstTask\classes
|
|
|
|
|
|
|
|
jar:
|
|
|
|
[jar] Building jar: C:\tmp\anttests\MyFirstTask\MyTask.jar
|
|
|
|
|
|
|
|
junit:
|
|
|
|
[mkdir] Created dir: C:\tmp\anttests\MyFirstTask\report\junit\xml
|
|
|
|
[junit] Running HelloWorldTest
|
|
|
|
[junit] Tests run: 5, Failures: 0, Errors: 0, Time elapsed: 2,334 sec
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
junitreport:
|
|
|
|
[mkdir] Created dir: C:\tmp\anttests\MyFirstTask\report\junit\html
|
|
|
|
[junitreport] Using Xalan version: Xalan Java 2.4.1
|
|
|
|
[junitreport] Transform time: 661ms
|
|
|
|
|
|
|
|
test:
|
|
|
|
|
|
|
|
BUILD SUCCESSFUL
|
|
|
|
Total time: 7 seconds
|
2004-11-16 08:12:35 +00:00
|
|
|
C:\tmp\anttests\MyFirstTask>
|
2003-09-06 15:13:00 +00:00
|
|
|
</pre></p>
|
|
|
|
|
|
|
|
|
2003-12-23 15:57:02 +00:00
|
|
|
<a name="resources"></a>
|
2003-09-06 15:13:00 +00:00
|
|
|
<h2>Resources</h2>
|
|
|
|
<p>This tutorial and its resources are available via
|
2005-01-01 20:27:34 +00:00
|
|
|
<a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=22570">BugZilla [6]</a>.
|
2003-09-06 15:13:00 +00:00
|
|
|
The ZIP provided there contains<ul>
|
|
|
|
<li>this tutorial</li>
|
|
|
|
<li>the buildfile (last version)</li>
|
|
|
|
<li>the source of the task (last version)</li>
|
|
|
|
<li>the source of the unit test (last version)</li>
|
|
|
|
<li>the ant-testutil.jar (nightly build of 2003-08-18)</li>
|
|
|
|
<li>generated classes</li>
|
|
|
|
<li>generated jar</li>
|
|
|
|
<li>generated reports</li>
|
|
|
|
</ul>
|
|
|
|
The last sources and the buildfile are also available
|
|
|
|
<a href="tutorial-writing-tasks-src.zip">here [7]</a> inside the manual.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
2003-12-23 15:57:02 +00:00
|
|
|
Used Links:<br>
|
|
|
|
[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>
|
|
|
|
[2] <a href="http://ant.apache.org/manual/CoreTasks/taskdef.html">http://ant.apache.org/manual/CoreTasks/taskdef.html</a><br>
|
|
|
|
[3] <a href="http://ant.apache.org/manual/develop.html#set-magic">http://ant.apache.org/manual/develop.html#set-magic</a><br>
|
|
|
|
[4] <a href="http://ant.apache.org/manual/develop.html#nested-elements">http://ant.apache.org/manual/develop.html#nested-elements</a><br>
|
|
|
|
[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>
|
2005-01-01 20:27:34 +00:00
|
|
|
[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>
|
2003-12-23 15:57:02 +00:00
|
|
|
[7] <a href="tutorial-writing-tasks-src.zip">tutorial-writing-tasks-src.zip</a><br>
|
2003-09-06 15:13:00 +00:00
|
|
|
|
2006-09-11 04:33:25 +00:00
|
|
|
|
2003-09-06 15:13:00 +00:00
|
|
|
|
|
|
|
</body>
|
2003-12-23 15:57:02 +00:00
|
|
|
</html>
|