~ubuntu-branches/ubuntu/jaunty/ant/jaunty-proposed

« back to all changes in this revision

Viewing changes to docs/manual/develop.html

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2002-02-14 14:28:48 UTC
  • Revision ID: james.westby@ubuntu.com-20020214142848-2ww7ynmqkj31vlmn
Tags: upstream-1.4.1
ImportĀ upstreamĀ versionĀ 1.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<html>
 
2
 
 
3
<head>
 
4
<meta http-equiv="Content-Language" content="en-us">
 
5
<title>Ant User Manual</title>
 
6
</head>
 
7
 
 
8
<body>
 
9
<h1>Developing with Ant</h1>
 
10
 
 
11
<h2><a name="writingowntask">Writing Your Own Task</a></h2>
 
12
<p>It is very easy to write your own task:</p>
 
13
<ol>
 
14
  <li>Create a Java class that extends <code>org.apache.tools.ant.Task</code>.</li>
 
15
  <li>For each attribute, write a <i>setter</i> method. The setter method must be a 
 
16
    <code>public void</code> method that takes a single argument. The
 
17
    name of the method must begin with <code>set</code>, followed by the
 
18
    attribute name, with the first character of the name in uppercase, and the rest in
 
19
    lowercase. The type of the attribute can be:
 
20
<ul>
 
21
<li>
 
22
<code>String</code>
 
23
</li>
 
24
<li>
 
25
any primitive type (they are converted for you from their String-representation
 
26
in the buildfile)
 
27
</li>
 
28
<li>
 
29
boolean - your method will be passed the value 
 
30
<i>true</i> if the value specified in the buildfile is one of <code>true</code>,
 
31
<code>yes</code>, or <code>on</code>)
 
32
</li>
 
33
<li>
 
34
<code>Class</code>
 
35
</li>
 
36
<li>
 
37
<code>File</code>
 
38
(in which case the value of the attribute is interpreted relative to the 
 
39
project's basedir)
 
40
</li>
 
41
<li>
 
42
any other type that has a constructor with a single 
 
43
<code>String</code> argument
 
44
</li>
 
45
</ul>
 
46
</li>
 
47
<li>If your task has enumerated attributes, you should consider using
 
48
a subclass of <code>org.apache.tools.ant.types.EnumeratedAttribute</code>
 
49
as an argument
 
50
to your setter method. See
 
51
<code>org/apache/tools/ant/taskdefs/FixCRLF.java</code> for
 
52
an example.</li>
 
53
<li>If the task should support character data, write a <code>public void 
 
54
addText(String)</code> method.</li>
 
55
<li>For each nested element, write a <i>create</i> or <i>add</i> method.
 
56
A create method 
 
57
must be a <code>public</code> method that takes no arguments and returns 
 
58
an <code>Object</code> type. The name of the create method must begin with 
 
59
<code>create</code>, followed by the element name. An add method must be 
 
60
a <code>public void</code> method that takes a single argument of an 
 
61
<code>Object</code> type with a no-argument constructor.
 
62
The name of the add method 
 
63
must begin with <code>add</code>, followed by the element name.</li>
 
64
<li>Write a <code>public void execute</code> method, with no arguments, that
 
65
throws a <code>BuildException</code>. This method implements the task
 
66
itself.</li>
 
67
</ol>
 
68
 
 
69
<h3>The Life-cycle of a Task</h3>
 
70
<ol>
 
71
  <li>The task gets instantiated using a no-argument constructor, at parser
 
72
    time. This means even tasks that are never executed get
 
73
    instantiated.</li>
 
74
 
 
75
  <li>The task gets references to its project and location inside the
 
76
    buildfile via its inherited <code>project</code> and
 
77
    <code>location</code> variables.</li>
 
78
 
 
79
  <li>If the user specified an <code>id</code> attribute to this task,
 
80
    the project
 
81
    registers a reference to this newly created task, at parser
 
82
    time.</li>
 
83
 
 
84
  <li>The task gets a reference to the target it belongs to via its
 
85
    inherited <code>target</code> variable.</li>
 
86
 
 
87
  <li><code>init()</code> is called at parser time.</li>
 
88
 
 
89
  <li>All child elements of the XML element corresponding to this task
 
90
    are created via this task's <code>createXXX()</code> methods or
 
91
    instantiated and added to this task via its <code>addXXX()</code>
 
92
    methods, at parser time.</li>
 
93
 
 
94
  <li>All attributes of this task get set via their corresponding
 
95
    <code>setXXX</code> methods, at runtime.</li>
 
96
 
 
97
  <li>The content character data sections inside the XML element
 
98
    corresponding to this task is added to the task via its
 
99
    <code>addText</code> method, at runtime.</li>
 
100
 
 
101
  <li>All attributes of all child elements get set via their corresponding
 
102
    <code>setXXX</code> methods, at runtime.</li>
 
103
 
 
104
  <li><code>execute()</code> is called at runtime. While the above initialization 
 
105
    steps only occur once, the execute() method may be 
 
106
    called more than once, if the task is invoked more than once. For example, 
 
107
    if <code>target1</code> and <code>target2</code> both depend
 
108
    on <code>target3</code>, then running 
 
109
    <code>'ant target1 target2'</code> will run all tasks in
 
110
    <code>target3</code> twice.</li>
 
111
</ol>
 
112
 
 
113
<h3>Example</h3>
 
114
<p>Let's write our own task, which prints a message on the
 
115
<code>System.out</code> stream.
 
116
The
 
117
task has one attribute, called <code>message</code>.</p>
 
118
<blockquote>
 
119
<pre>
 
120
package com.mydomain;
 
121
 
 
122
import org.apache.tools.ant.BuildException;
 
123
import org.apache.tools.ant.Task;
 
124
 
 
125
public class MyVeryOwnTask extends Task {
 
126
  private String msg;
 
127
 
 
128
  // The method executing the task
 
129
  public void execute() throws BuildException {
 
130
    System.out.println(msg);
 
131
  }
 
132
 
 
133
  // The setter for the &quot;message&quot; attribute
 
134
  public void setMessage(String msg) {
 
135
    this.msg = msg;
 
136
  }
 
137
}
 
138
</pre>
 
139
</blockquote>
 
140
<p>It's really this simple ;-)</p>
 
141
<p>Adding your task to the system is rather simple too:</p>
 
142
<ol>
 
143
  <li>Make sure the class that implements your task is in the classpath when
 
144
    starting Ant.</li>
 
145
  <li>Add a <code>&lt;taskdef&gt;</code> element to your project.
 
146
    This actually adds your task to the system.</li>
 
147
  <li>Use your task in the rest of the buildfile.</li>
 
148
</ol>
 
149
 
 
150
<h3>Example</h3>
 
151
<blockquote>
 
152
<pre>
 
153
&lt;?xml version=&quot;1.0&quot;?&gt;
 
154
 
 
155
&lt;project name=&quot;OwnTaskExample&quot; default=&quot;main&quot; basedir=&quot;.&quot;&gt;
 
156
  &lt;taskdef name=&quot;mytask&quot; classname=&quot;com.mydomain.MyVeryOwnTask&quot;/&gt;
 
157
 
 
158
  &lt;target name=&quot;main&quot;&gt;
 
159
    &lt;mytask message=&quot;Hello World! MyVeryOwnTask works!&quot;/&gt;
 
160
  &lt;/target&gt;
 
161
&lt;/project&gt;
 
162
</pre>
 
163
</blockquote>
 
164
<p>Another way to add a task (more permanently), is to add the task name and
 
165
implementing class name to the <code>default.properties</code> file in the
 
166
<code>org.apache.tools.ant.taskdefs</code>
 
167
package. Then you can use it as if it were a built-in task.</p>
 
168
 
 
169
<hr>
 
170
<h2><a name="buildevents">Build Events</a></h2>
 
171
<P>Ant is capable of generating build events as it performs the tasks necessary to build a project. 
 
172
Listeners can be attached to Ant to receive these events. This capability could be used, for example,
 
173
to connect Ant to a GUI or to integrate Ant with an IDE. 
 
174
</P>
 
175
<p>To use build events you need to create an ant <code>Project</code> object. You can then call the 
 
176
<code>addBuildListener</code> method to add your listener to the project. Your listener must implement
 
177
the <code>org.apache.tools.antBuildListener</code> interface. The listener will receive BuildEvents 
 
178
for the following events</P>
 
179
<ul>
 
180
<li>Build started</li>
 
181
<li>Build finished</li>
 
182
<li>Target started</li>
 
183
<li>Target finished</li>
 
184
<li>Task started</li>
 
185
<li>Task finished</li>
 
186
<li>Message logged</li>
 
187
</ul>
 
188
 
 
189
<p>
 
190
If you wish to attach a listener from the command line you may use the
 
191
<code>-listener</code> option. For example:</p>
 
192
<blockquote>
 
193
  <pre>ant -listener org.apache.tools.ant.XmlLogger</pre>
 
194
</blockquote>
 
195
<p>will run Ant with a listener that generates an XML representation of the build progress. This 
 
196
listener is included with Ant, as is the default listener, which generates the logging to standard output.</p>
 
197
 
 
198
<hr>
 
199
<p align="center">Copyright &copy; 2000,2001 Apache Software Foundation. All rights
 
200
Reserved.</p>
 
201
 
 
202
</body>
 
203
</html>
 
204