You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

develop.html 14 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  4. <title>Writing Your Own Task</title>
  5. </head>
  6. <body>
  7. <h1>Developing with Ant</h1>
  8. <h2><a name="writingowntask">Writing Your Own Task</a></h2>
  9. <p>It is very easy to write your own task:</p>
  10. <ol>
  11. <li>Create a Java class that extends <code>org.apache.tools.ant.Task</code>.</li>
  12. <li>For each attribute, write a <i>setter</i> method. The setter method must be a
  13. <code>public void</code> method that takes a single argument. The
  14. name of the method must begin with <code>set</code>, followed by the
  15. attribute name, with the first character of the name in uppercase, and the rest in
  16. lowercase. That is, to support an attribute named
  17. <code>file</code> you create a method <code>setFile</code>.
  18. Depending on the type of the argument, Ant will perform some
  19. conversions for you, see <a href="#set-magic">below</a>.</li>
  20. <li>If your task shall contain other tasks as nested elements (like
  21. <a href="CoreTasks/parallel.html"><code>parallel</code></a>), your
  22. class must implement the interface
  23. <code>org.apache.tools.ant.TaskContainer</code>. If you do so, your
  24. task can not support any other nested elements. See
  25. <a href="#taskcontainer">below</a>.</li>
  26. <li>If the task should support character data (text nested between the
  27. start end end tags), write a <code>public void addText(String)</code>
  28. method. Note that Ant does <strong>not</strong> expand properties on
  29. the text it passes to the task.</li>
  30. <li>For each nested element, write a <i>create</i>, <i>add</i> or
  31. <i>addConfigured</i> method. A create method must be a
  32. <code>public</code> method that takes no arguments and returns an
  33. <code>Object</code> type. The name of the create method must begin
  34. with <code>create</code>, followed by the element name. An add (or
  35. addConfigured) method must be a <code>public void</code> method that
  36. takes a single argument of an <code>Object</code> type with a
  37. no-argument constructor. The name of the add (addConfigured) method
  38. must begin with <code>add</code> (<code>addConfigured</code>),
  39. followed by the element name. For a more complete discussion see
  40. <a href="#nested-elements">below</a>.</li>
  41. <li>Write a <code>public void execute</code> method, with no arguments, that
  42. throws a <code>BuildException</code>. This method implements the task
  43. itself.</li>
  44. </ol>
  45. <h3>The Life-cycle of a Task</h3>
  46. <ol>
  47. <li>The task gets instantiated using a no-argument constructor, at parser
  48. time. This means even tasks that are never executed get
  49. instantiated.</li>
  50. <li>The task gets references to its project and location inside the
  51. buildfile via its inherited <code>project</code> and
  52. <code>location</code> variables.</li>
  53. <li>If the user specified an <code>id</code> attribute to this task,
  54. the project
  55. registers a reference to this newly created task, at parser
  56. time.</li>
  57. <li>The task gets a reference to the target it belongs to via its
  58. inherited <code>target</code> variable.</li>
  59. <li><code>init()</code> is called at parser time.</li>
  60. <li>All child elements of the XML element corresponding to this task
  61. are created via this task's <code>createXXX()</code> methods or
  62. instantiated and added to this task via its <code>addXXX()</code>
  63. methods, at parser time.</li>
  64. <li>All attributes of this task get set via their corresponding
  65. <code>setXXX</code> methods, at runtime.</li>
  66. <li>The content character data sections inside the XML element
  67. corresponding to this task is added to the task via its
  68. <code>addText</code> method, at runtime.</li>
  69. <li>All attributes of all child elements get set via their corresponding
  70. <code>setXXX</code> methods, at runtime.</li>
  71. <li><a name="execute"><code>execute()</code></a> is called at runtime. While the above initialization
  72. steps only occur once, the execute() method may be
  73. called more than once, if the task is invoked more than once. For example,
  74. if <code>target1</code> and <code>target2</code> both depend
  75. on <code>target3</code>, then running
  76. <code>'ant target1 target2'</code> will run all tasks in
  77. <code>target3</code> twice.</li>
  78. </ol>
  79. <h3><a name="set-magic">Conversions Ant will perform for attributes</a></h3>
  80. <p>Ant will always expand properties before it passes the value of an
  81. attribute to the corresponding setter method.</p>
  82. <p>The most common way to write an attribute setter is to use a
  83. <code>java.lang.String</code> argument. In this case Ant will pass
  84. the literal value (after property expansion) to your task. But there
  85. is more! If the argument of you setter method is</p>
  86. <ul>
  87. <li><code>boolean</code>, your method will be passed the value
  88. <i>true</i> if the value specified in the build file is one of
  89. <code>true</code>, <code>yes</code>, or <code>on</code> and
  90. <i>false</i> otherwise.</li>
  91. <li><code>char</code> or <code>java.lang.Character</code>, your
  92. method will be passed the first character of the value specified in
  93. the build file.</li>
  94. <li>any other primitive type (<code>int</code>, <code>short</code>
  95. and so on), Ant will convert the value of the attribute into this
  96. type, thus making sure that you'll never receive input that is not a
  97. number for that attribute.</li>
  98. <li><code>java.io.File</code>, Ant will first determine whether the
  99. value given in the build file represents an absolute path name. If
  100. not, Ant will interpret the value as a path name relative to the
  101. project's basedir.</li>
  102. <li><code>org.apache.tools.ant.types.Path</code>, Ant will tokenize
  103. the value specified in the build file, accepting <code>:</code> and
  104. <code>;</code> as path separators. Relative path names will be
  105. interpreted as relative to the project's basedir.</li>
  106. <li><code>java.lang.Class</code>, Ant will interpret the value
  107. given in the build file as a Java class name and load the named
  108. class from the system class loader.</li>
  109. <li>any other type that has a constructor with a single
  110. <code>String</code> argument, Ant will use this constructor to
  111. create a new instance from the value given in the build file.</li>
  112. <li>A subclass of
  113. <code>org.apache.tools.ant.types.EnumeratedAttribute</code>, Ant
  114. will invoke this classes <code>setValue</code> method. Use this if
  115. your task should support enumerated attributes (attributes with
  116. values that must be part of a predefined set of values). See
  117. <code>org/apache/tools/ant/taskdefs/FixCRLF.java</code> and the
  118. inner <code>AddAsisRemove</code> class used in <code>setCr</code>
  119. for an example.</li>
  120. </ul>
  121. <p>What happens if more than one setter method is present for a given
  122. attribute? A method taking a <code>String</code> argument will always
  123. lose against the more specific methods. If there are still more
  124. setters Ant could chose from, only one of them will be called, but we
  125. don't know which, this depends on the implementation of your Java
  126. virtual machine.</p>
  127. <h3><a name="nested-elements">Supporting nested elements</a></h3>
  128. <p>Let's assume your task shall support nested elements with the name
  129. <code>inner</code>. First of all, you need a class that represents
  130. this nested element. Often you simply want to use one of Ant's
  131. classes like <code>org.apache.tools.ant.types.FileSet</code> to
  132. support nested <code>fileset</code> elements.</p>
  133. <p>Attributes of the nested elements or nested child elements of them
  134. will be handled using the same mechanism used for tasks (i.e. setter
  135. methods for attributes, addText for nested text and
  136. create/add/addConfigured methods for child elements).</p>
  137. <p>Now you have a class <code>NestedElement</code> that is supposed to
  138. be used for your nested <code>&lt;inner&gt;</code> elements, you have
  139. three options:</p>
  140. <ol>
  141. <li><code>public NestedElement createInner()</code></li>
  142. <li><code>public void addInner(NestedElement anInner)</code></li>
  143. <li><code>public void addConfiguredInner(NestedElement anInner)</code></li>
  144. </ol>
  145. <p>What is the difference?</p>
  146. <p>Option 1 makes the task create the instance of
  147. <code>NestedElement</code>, there are no restrictions on the type.
  148. For the options 2 and 3, Ant has to create an instance of
  149. <code>NestedInner</code> before it can pass it to the task, this
  150. means, <code>NestedInner</code> must have a <code>public</code> no-arg
  151. constructor. This is the only difference between options 1 and 2.</p>
  152. <p>The difference between 2 and 3 is what Ant has done to the object
  153. before it passes it to the method. <code>addInner</code> will receive
  154. an object directly after the constructor has been called, while
  155. <code>addConfiguredInner</code> gets the object <em>after</em> the
  156. attributes and nested children for this new object have been
  157. handled.</p>
  158. <p>What happens if you use more than one of the options? Only one of
  159. the methods will be called, but we don't know which, this depends on
  160. the implementation of your Java virtual machine.</p>
  161. <h3><a name="taskcontainer">TaskContainer</a></h3>
  162. <p>The <code>TaskContainer</code> consists of a single method,
  163. <code>addTask</code> that basically is the same as an <a
  164. href="#nested-elements">add method</a> for nested elements. The task
  165. instances will be configured (their attributes and nested elements
  166. have been handled) when your task's <code>execute</code> method gets
  167. invoked, but not before that.</p>
  168. <p>When we <a href="#execute">said</a> <code>execute</code> would be
  169. called, we lied ;-). In fact, Ant will call the <code>perform</code>
  170. method in <code>org.apache.tools.ant.Task</code>, which in turn calls
  171. <code>execute</code>. This method makes sure that <a
  172. href="#buildevents">Build Events</a> will be triggered. If you
  173. execute the task instances nested into your task, you should also
  174. invoke <code>perform</code> on these instances instead of
  175. <code>execute</code>.</p>
  176. <h3>Example</h3>
  177. <p>Let's write our own task, which prints a message on the
  178. <code>System.out</code> stream.
  179. The
  180. task has one attribute, called <code>message</code>.</p>
  181. <blockquote>
  182. <pre>
  183. package com.mydomain;
  184. import org.apache.tools.ant.BuildException;
  185. import org.apache.tools.ant.Task;
  186. public class MyVeryOwnTask extends Task {
  187. private String msg;
  188. // The method executing the task
  189. public void execute() throws BuildException {
  190. System.out.println(msg);
  191. }
  192. // The setter for the &quot;message&quot; attribute
  193. public void setMessage(String msg) {
  194. this.msg = msg;
  195. }
  196. }
  197. </pre>
  198. </blockquote>
  199. <p>It's really this simple ;-)</p>
  200. <p>Adding your task to the system is rather simple too:</p>
  201. <ol>
  202. <li>Make sure the class that implements your task is in the classpath when
  203. starting Ant.</li>
  204. <li>Add a <code>&lt;taskdef&gt;</code> element to your project.
  205. This actually adds your task to the system.</li>
  206. <li>Use your task in the rest of the buildfile.</li>
  207. </ol>
  208. <h3>Example</h3>
  209. <blockquote>
  210. <pre>
  211. &lt;?xml version=&quot;1.0&quot;?&gt;
  212. &lt;project name=&quot;OwnTaskExample&quot; default=&quot;main&quot; basedir=&quot;.&quot;&gt;
  213. &lt;taskdef name=&quot;mytask&quot; classname=&quot;com.mydomain.MyVeryOwnTask&quot;/&gt;
  214. &lt;target name=&quot;main&quot;&gt;
  215. &lt;mytask message=&quot;Hello World! MyVeryOwnTask works!&quot;/&gt;
  216. &lt;/target&gt;
  217. &lt;/project&gt;
  218. </pre>
  219. </blockquote>
  220. <h3>Example 2</h3>
  221. To use a task directly from the buildfile which created it, place the
  222. <code>&lt;taskdef&gt;</code> declaration inside a target
  223. <i>after the compilation</i>. Use the <code>classpath</code> attribute of
  224. <code>&lt;taskdef&gt;</code> to point to where the code has just been
  225. compiled.
  226. <blockquote>
  227. <pre>
  228. &lt;?xml version=&quot;1.0&quot;?&gt;
  229. &lt;project name=&quot;OwnTaskExample2&quot; default=&quot;main&quot; basedir=&quot;.&quot;&gt;
  230. &lt;target name=&quot;build&quot; &gt;
  231. &lt;mkdir dir=&quot;build&quot;/&gt;
  232. &lt;javac srcdir=&quot;source&quot; destdir=&quot;build&quot;/&gt;
  233. &lt;/target&gt;
  234. &lt;target name=&quot;declare&quot; depends=&quot;build&quot;&gt;
  235. &lt;taskdef name=&quot;mytask&quot;
  236. classname=&quot;com.mydomain.MyVeryOwnTask&quot;
  237. classpath=&quot;build&quot;/&gt;
  238. &lt;/target&gt;
  239. &lt;target name=&quot;main&quot; depends=&quot;declare&quot;&gt;
  240. &lt;mytask message=&quot;Hello World! MyVeryOwnTask works!&quot;/&gt;
  241. &lt;/target&gt;
  242. &lt;/project&gt;
  243. </pre>
  244. </blockquote>
  245. <p>Another way to add a task (more permanently), is to add the task name and
  246. implementing class name to the <code>default.properties</code> file in the
  247. <code>org.apache.tools.ant.taskdefs</code>
  248. package. Then you can use it as if it were a built-in task.</p>
  249. <hr>
  250. <h2><a name="buildevents">Build Events</a></h2>
  251. <P>Ant is capable of generating build events as it performs the tasks necessary to build a project.
  252. Listeners can be attached to Ant to receive these events. This capability could be used, for example,
  253. to connect Ant to a GUI or to integrate Ant with an IDE.
  254. </P>
  255. <p>To use build events you need to create an ant <code>Project</code> object. You can then call the
  256. <code>addBuildListener</code> method to add your listener to the project. Your listener must implement
  257. the <code>org.apache.tools.antBuildListener</code> interface. The listener will receive BuildEvents
  258. for the following events</P>
  259. <ul>
  260. <li>Build started</li>
  261. <li>Build finished</li>
  262. <li>Target started</li>
  263. <li>Target finished</li>
  264. <li>Task started</li>
  265. <li>Task finished</li>
  266. <li>Message logged</li>
  267. </ul>
  268. <p>
  269. If you wish to attach a listener from the command line you may use the
  270. <code>-listener</code> option. For example:</p>
  271. <blockquote>
  272. <pre>ant -listener org.apache.tools.ant.XmlLogger</pre>
  273. </blockquote>
  274. <p>will run Ant with a listener that generates an XML representation of the build progress. This
  275. listener is included with Ant, as is the default listener, which generates the logging to standard output.</p>
  276. <hr>
  277. <h2><a name="integration">Source code integration</a></h2>
  278. The other way to extend Ant through Java is to make changes to existing tasks, which is positively encouraged.
  279. Both changes to the existing source and new tasks can be incorporated back into the Ant codebase, which
  280. benefits all users and spreads the maintenance load around.
  281. <p>
  282. Please consult the
  283. <a href="http://jakarta.apache.org/site/getinvolved.html">Getting Involved</a> pages on the Jakarta web site
  284. for details on how to fetch the latest source and how to submit changes for reincorporation into the
  285. source tree.
  286. <p>
  287. Ant also has some
  288. <a href="http://jakarta.apache.org/ant/ant_task_guidelines.html">task guidelines</a>
  289. which provides some advice to people developing and testing tasks. Even if you intend to
  290. keep your tasks to yourself, you should still read this as it should be informative.
  291. <hr>
  292. <p align="center">Copyright &copy; 2001-2002 Apache Software Foundation. All rights
  293. Reserved.</p>
  294. </body>
  295. </html>