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 19 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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>
  12. or <a href="base_task_classes.html">another class</a> that was desgined to be extended.</li>
  13. <li>For each attribute, write a <i>setter</i> method. The setter method must be a
  14. <code>public void</code> method that takes a single argument. The
  15. name of the method must begin with <code>set</code>, followed by the
  16. attribute name, with the first character of the name in uppercase, and the rest in
  17. lowercase<a href="#footnote-1"><sup>*</sup></a>. That is, to support an attribute named
  18. <code>file</code> you create a method <code>setFile</code>.
  19. Depending on the type of the argument, Ant will perform some
  20. conversions for you, see <a href="#set-magic">below</a>.</li>
  21. <li>If your task shall contain other tasks as nested elements (like
  22. <a href="CoreTasks/parallel.html"><code>parallel</code></a>), your
  23. class must implement the interface
  24. <code>org.apache.tools.ant.TaskContainer</code>. If you do so, your
  25. task can not support any other nested elements. See
  26. <a href="#taskcontainer">below</a>.</li>
  27. <li>If the task should support character data (text nested between the
  28. start end end tags), write a <code>public void addText(String)</code>
  29. method. Note that Ant does <strong>not</strong> expand properties on
  30. the text it passes to the task.</li>
  31. <li>For each nested element, write a <i>create</i>, <i>add</i> or
  32. <i>addConfigured</i> method. A create method must be a
  33. <code>public</code> method that takes no arguments and returns an
  34. <code>Object</code> type. The name of the create method must begin
  35. with <code>create</code>, followed by the element name. An add (or
  36. addConfigured) method must be a <code>public void</code> method that
  37. takes a single argument of an <code>Object</code> type with a
  38. no-argument constructor. The name of the add (addConfigured) method
  39. must begin with <code>add</code> (<code>addConfigured</code>),
  40. followed by the element name. For a more complete discussion see
  41. <a href="#nested-elements">below</a>.</li>
  42. <li>Write a <code>public void execute</code> method, with no arguments, that
  43. throws a <code>BuildException</code>. This method implements the task
  44. itself.</li>
  45. </ol>
  46. <hr>
  47. <p><a name="footnote-1">*</a> Actually the case of the letters after
  48. the first one doesn't really matter to Ant, using all lower case is a
  49. good convention, though.</p>
  50. <h3>The Life-cycle of a Task</h3>
  51. <ol>
  52. <li>The task gets instantiated using a no-argument constructor, at parser
  53. time. This means even tasks that are never executed get
  54. instantiated.</li>
  55. <li>The task gets references to its project and location inside the
  56. buildfile via its inherited <code>project</code> and
  57. <code>location</code> variables.</li>
  58. <li>If the user specified an <code>id</code> attribute to this task,
  59. the project
  60. registers a reference to this newly created task, at parser
  61. time.</li>
  62. <li>The task gets a reference to the target it belongs to via its
  63. inherited <code>target</code> variable.</li>
  64. <li><code>init()</code> is called at parser time.</li>
  65. <li>All child elements of the XML element corresponding to this task
  66. are created via this task's <code>createXXX()</code> methods or
  67. instantiated and added to this task via its <code>addXXX()</code>
  68. methods, at parser time.</li>
  69. <li>All attributes of this task get set via their corresponding
  70. <code>setXXX</code> methods, at runtime.</li>
  71. <li>The content character data sections inside the XML element
  72. corresponding to this task is added to the task via its
  73. <code>addText</code> method, at runtime.</li>
  74. <li>All attributes of all child elements get set via their corresponding
  75. <code>setXXX</code> methods, at runtime.</li>
  76. <li><a name="execute"><code>execute()</code></a> is called at runtime. While the above initialization
  77. steps only occur once, the execute() method may be
  78. called more than once, if the task is invoked more than once. For example,
  79. if <code>target1</code> and <code>target2</code> both depend
  80. on <code>target3</code>, then running
  81. <code>'ant target1 target2'</code> will run all tasks in
  82. <code>target3</code> twice.</li>
  83. </ol>
  84. <h3><a name="set-magic">Conversions Ant will perform for attributes</a></h3>
  85. <p>Ant will always expand properties before it passes the value of an
  86. attribute to the corresponding setter method.</p>
  87. <p>The most common way to write an attribute setter is to use a
  88. <code>java.lang.String</code> argument. In this case Ant will pass
  89. the literal value (after property expansion) to your task. But there
  90. is more! If the argument of you setter method is</p>
  91. <ul>
  92. <li><code>boolean</code>, your method will be passed the value
  93. <i>true</i> if the value specified in the build file is one of
  94. <code>true</code>, <code>yes</code>, or <code>on</code> and
  95. <i>false</i> otherwise.</li>
  96. <li><code>char</code> or <code>java.lang.Character</code>, your
  97. method will be passed the first character of the value specified in
  98. the build file.</li>
  99. <li>any other primitive type (<code>int</code>, <code>short</code>
  100. and so on), Ant will convert the value of the attribute into this
  101. type, thus making sure that you'll never receive input that is not a
  102. number for that attribute.</li>
  103. <li><code>java.io.File</code>, Ant will first determine whether the
  104. value given in the build file represents an absolute path name. If
  105. not, Ant will interpret the value as a path name relative to the
  106. project's basedir.</li>
  107. <li><code>org.apache.tools.ant.types.Path</code>, Ant will tokenize
  108. the value specified in the build file, accepting <code>:</code> and
  109. <code>;</code> as path separators. Relative path names will be
  110. interpreted as relative to the project's basedir.</li>
  111. <li><code>java.lang.Class</code>, Ant will interpret the value
  112. given in the build file as a Java class name and load the named
  113. class from the system class loader.</li>
  114. <li>any other type that has a constructor with a single
  115. <code>String</code> argument, Ant will use this constructor to
  116. create a new instance from the value given in the build file.</li>
  117. <li>A subclass of
  118. <code>org.apache.tools.ant.types.EnumeratedAttribute</code>, Ant
  119. will invoke this classes <code>setValue</code> method. Use this if
  120. your task should support enumerated attributes (attributes with
  121. values that must be part of a predefined set of values). See
  122. <code>org/apache/tools/ant/taskdefs/FixCRLF.java</code> and the
  123. inner <code>AddAsisRemove</code> class used in <code>setCr</code>
  124. for an example.</li>
  125. </ul>
  126. <p>What happens if more than one setter method is present for a given
  127. attribute? A method taking a <code>String</code> argument will always
  128. lose against the more specific methods. If there are still more
  129. setters Ant could chose from, only one of them will be called, but we
  130. don't know which, this depends on the implementation of your Java
  131. virtual machine.</p>
  132. <h3><a name="nested-elements">Supporting nested elements</a></h3>
  133. <p>Let's assume your task shall support nested elements with the name
  134. <code>inner</code>. First of all, you need a class that represents
  135. this nested element. Often you simply want to use one of Ant's
  136. classes like <code>org.apache.tools.ant.types.FileSet</code> to
  137. support nested <code>fileset</code> elements.</p>
  138. <p>Attributes of the nested elements or nested child elements of them
  139. will be handled using the same mechanism used for tasks (i.e. setter
  140. methods for attributes, addText for nested text and
  141. create/add/addConfigured methods for child elements).</p>
  142. <p>Now you have a class <code>NestedElement</code> that is supposed to
  143. be used for your nested <code>&lt;inner&gt;</code> elements, you have
  144. three options:</p>
  145. <ol>
  146. <li><code>public NestedElement createInner()</code></li>
  147. <li><code>public void addInner(NestedElement anInner)</code></li>
  148. <li><code>public void addConfiguredInner(NestedElement anInner)</code></li>
  149. </ol>
  150. <p>What is the difference?</p>
  151. <p>Option 1 makes the task create the instance of
  152. <code>NestedElement</code>, there are no restrictions on the type.
  153. For the options 2 and 3, Ant has to create an instance of
  154. <code>NestedInner</code> before it can pass it to the task, this
  155. means, <code>NestedInner</code> must have a <code>public</code> no-arg
  156. constructor or a <code>public</code> one-arg constructor
  157. taking a Project class as a parameter.
  158. This is the only difference between options 1 and 2.</p>
  159. <p>The difference between 2 and 3 is what Ant has done to the object
  160. before it passes it to the method. <code>addInner</code> will receive
  161. an object directly after the constructor has been called, while
  162. <code>addConfiguredInner</code> gets the object <em>after</em> the
  163. attributes and nested children for this new object have been
  164. handled.</p>
  165. <p>What happens if you use more than one of the options? Only one of
  166. the methods will be called, but we don't know which, this depends on
  167. the implementation of your Java virtual machine.</p>
  168. <h3><a name="nestedtype">Nested Types</a></h3>
  169. If your task needs to nest an arbitary type that has been defined
  170. using &lt;typedef&gt; you have two options.
  171. <ol>
  172. <li><code>public void add(Type type)</code></li>
  173. <li><code>public void addConfigured(Type type)</code></li>
  174. </ol>
  175. The difference between 1 and 2 is the same as between 2 and 3 in the
  176. previous section.
  177. <p>
  178. For example suppose one wanted to handle objects object of type
  179. org.apache.tools.ant.taskdefs.condition.Condition, one may
  180. have a class:
  181. </p>
  182. <blockquote>
  183. <pre>
  184. public class MyTask extends Task {
  185. private List conditions = new ArrayList();
  186. public void add(Condition c) {
  187. conditions.add(c);
  188. }
  189. public void execute() {
  190. // iterator over the conditions
  191. }
  192. }
  193. </pre>
  194. </blockquote>
  195. <p>
  196. One may define and use this class like this:
  197. </p>
  198. <blockquote>
  199. <pre>
  200. &lt;taskdef name="mytask" classname="MyTask" classpath="classes"/&gt;
  201. &lt;typedef name="condition.equals"
  202. classname="org.apache.tools.ant.taskdefs.conditions.Equals"/&gt;
  203. &lt;mytask&gt;
  204. &lt;condition.equals arg1="${debug}" arg2="true"/&gt;
  205. &lt;/mytask&gt;
  206. </pre>
  207. </blockquote>
  208. <p>
  209. A more complicated example follows:
  210. </p>
  211. <blockquote>
  212. <pre>
  213. public class Sample {
  214. public static class MyFileSelector implements FileSelector {
  215. public void setAttrA(int a) {}
  216. public void setAttrB(int b) {}
  217. public void add(Path path) {}
  218. public boolean isSelected(File basedir, String filename, File file) {
  219. return true;
  220. }
  221. }
  222. interface MyInterface {
  223. void setVerbose(boolean val);
  224. }
  225. public static class BuildPath extends Path {
  226. public BuildPath(Project project) {
  227. super(project);
  228. }
  229. public void add(MyInterface inter) {}
  230. public void setUrl(String url) {}
  231. }
  232. public static class XInterface implements MyInterface {
  233. public void setVerbose(boolean x) {}
  234. public void setCount(int c) {}
  235. }
  236. }
  237. </pre>
  238. </blockquote>
  239. <p>
  240. This class defines a number of static classes that implement/extend
  241. Path, MyFileSelector and MyInterface. These may be defined and used
  242. as follows:
  243. </p>
  244. <pre>
  245. <blockquote>
  246. &lt;typedef name="myfileselector" classname="Sample$MyFileSelector"
  247. classpath="classes" loaderref="classes"/&gt;
  248. &lt;typedef name="buildpath" classname="Sample$BuildPath"
  249. classpath="classes" loaderref="classes"/&gt;
  250. &lt;typedef name="xinterface" classname="Sample$XInterface"
  251. classpath="classes" loaderref="classes"/&gt;
  252. &lt;copy todir="copy-classes"&gt;
  253. &lt;fileset dir="classes"&gt;
  254. &lt;myfileselector attra="10" attrB="-10"&gt;
  255. &lt;buildpath path="." url="abc"&gt;
  256. &lt;xinterface count="4"/&gt;
  257. &lt;/buildpath&gt;
  258. &lt;/myfileselector&gt;
  259. &lt;/fileset&gt;
  260. &lt;/copy&gt;
  261. </blockquote>
  262. </pre>
  263. <h3><a name="taskcontainer">TaskContainer</a></h3>
  264. <p>The <code>TaskContainer</code> consists of a single method,
  265. <code>addTask</code> that basically is the same as an <a
  266. href="#nested-elements">add method</a> for nested elements. The task
  267. instances will be configured (their attributes and nested elements
  268. have been handled) when your task's <code>execute</code> method gets
  269. invoked, but not before that.</p>
  270. <p>When we <a href="#execute">said</a> <code>execute</code> would be
  271. called, we lied ;-). In fact, Ant will call the <code>perform</code>
  272. method in <code>org.apache.tools.ant.Task</code>, which in turn calls
  273. <code>execute</code>. This method makes sure that <a
  274. href="#buildevents">Build Events</a> will be triggered. If you
  275. execute the task instances nested into your task, you should also
  276. invoke <code>perform</code> on these instances instead of
  277. <code>execute</code>.</p>
  278. <h3>Example</h3>
  279. <p>Let's write our own task, which prints a message on the
  280. <code>System.out</code> stream.
  281. The
  282. task has one attribute, called <code>message</code>.</p>
  283. <blockquote>
  284. <pre>
  285. package com.mydomain;
  286. import org.apache.tools.ant.BuildException;
  287. import org.apache.tools.ant.Task;
  288. public class MyVeryOwnTask extends Task {
  289. private String msg;
  290. // The method executing the task
  291. public void execute() throws BuildException {
  292. System.out.println(msg);
  293. }
  294. // The setter for the &quot;message&quot; attribute
  295. public void setMessage(String msg) {
  296. this.msg = msg;
  297. }
  298. }
  299. </pre>
  300. </blockquote>
  301. <p>It's really this simple ;-)</p>
  302. <p>Adding your task to the system is rather simple too:</p>
  303. <ol>
  304. <li>Make sure the class that implements your task is in the classpath when
  305. starting Ant.</li>
  306. <li>Add a <code>&lt;taskdef&gt;</code> element to your project.
  307. This actually adds your task to the system.</li>
  308. <li>Use your task in the rest of the buildfile.</li>
  309. </ol>
  310. <h3>Example</h3>
  311. <blockquote>
  312. <pre>
  313. &lt;?xml version=&quot;1.0&quot;?&gt;
  314. &lt;project name=&quot;OwnTaskExample&quot; default=&quot;main&quot; basedir=&quot;.&quot;&gt;
  315. &lt;taskdef name=&quot;mytask&quot; classname=&quot;com.mydomain.MyVeryOwnTask&quot;/&gt;
  316. &lt;target name=&quot;main&quot;&gt;
  317. &lt;mytask message=&quot;Hello World! MyVeryOwnTask works!&quot;/&gt;
  318. &lt;/target&gt;
  319. &lt;/project&gt;
  320. </pre>
  321. </blockquote>
  322. <h3>Example 2</h3>
  323. To use a task directly from the buildfile which created it, place the
  324. <code>&lt;taskdef&gt;</code> declaration inside a target
  325. <i>after the compilation</i>. Use the <code>classpath</code> attribute of
  326. <code>&lt;taskdef&gt;</code> to point to where the code has just been
  327. compiled.
  328. <blockquote>
  329. <pre>
  330. &lt;?xml version=&quot;1.0&quot;?&gt;
  331. &lt;project name=&quot;OwnTaskExample2&quot; default=&quot;main&quot; basedir=&quot;.&quot;&gt;
  332. &lt;target name=&quot;build&quot; &gt;
  333. &lt;mkdir dir=&quot;build&quot;/&gt;
  334. &lt;javac srcdir=&quot;source&quot; destdir=&quot;build&quot;/&gt;
  335. &lt;/target&gt;
  336. &lt;target name=&quot;declare&quot; depends=&quot;build&quot;&gt;
  337. &lt;taskdef name=&quot;mytask&quot;
  338. classname=&quot;com.mydomain.MyVeryOwnTask&quot;
  339. classpath=&quot;build&quot;/&gt;
  340. &lt;/target&gt;
  341. &lt;target name=&quot;main&quot; depends=&quot;declare&quot;&gt;
  342. &lt;mytask message=&quot;Hello World! MyVeryOwnTask works!&quot;/&gt;
  343. &lt;/target&gt;
  344. &lt;/project&gt;
  345. </pre>
  346. </blockquote>
  347. <p>Another way to add a task (more permanently), is to add the task name and
  348. implementing class name to the <code>default.properties</code> file in the
  349. <code>org.apache.tools.ant.taskdefs</code>
  350. package. Then you can use it as if it were a built-in task.</p>
  351. <hr>
  352. <h2><a name="buildevents">Build Events</a></h2>
  353. <P>Ant is capable of generating build events as it performs the tasks necessary to build a project.
  354. Listeners can be attached to Ant to receive these events. This capability could be used, for example,
  355. to connect Ant to a GUI or to integrate Ant with an IDE.
  356. </P>
  357. <p>To use build events you need to create an ant <code>Project</code> object. You can then call the
  358. <code>addBuildListener</code> method to add your listener to the project. Your listener must implement
  359. the <code>org.apache.tools.antBuildListener</code> interface. The listener will receive BuildEvents
  360. for the following events</P>
  361. <ul>
  362. <li>Build started</li>
  363. <li>Build finished</li>
  364. <li>Target started</li>
  365. <li>Target finished</li>
  366. <li>Task started</li>
  367. <li>Task finished</li>
  368. <li>Message logged</li>
  369. </ul>
  370. <p>If the build file invokes another build file via <a
  371. href="CoreTasks/ant.html">&lt;ant&gt;</a> or <a
  372. href="CoreTasks/subant.html">&lt;subant&gt;</a> or uses <a
  373. href="CoreTasks/antcall.html">&lt;antcall&gt;</a>, you are creating a
  374. new Ant "project" that will send target and task level events of its
  375. own but never sends build started/finished events. Ant 1.6.2
  376. introduces an extension of the BuildListener interface named
  377. SubBuildListener that will receive two new events for</p>
  378. <ul>
  379. <li>SubBuild started</li>
  380. <li>SubBuild finished</li>
  381. </ul>
  382. <p>If you are interested in those events, all you need to do is to
  383. implement the new interface instead of BuildListener (and register the
  384. listener, of course).</p>
  385. <p>
  386. If you wish to attach a listener from the command line you may use the
  387. <code>-listener</code> option. For example:</p>
  388. <blockquote>
  389. <pre>ant -listener org.apache.tools.ant.XmlLogger</pre>
  390. </blockquote>
  391. <p>will run Ant with a listener that generates an XML representation of the build progress. This
  392. listener is included with Ant, as is the default listener, which generates the logging to standard output.</p>
  393. <p><b>Note: </b>A listener must not access System.out and System.err directly since ouput on
  394. these streams is redirected by Ant's core to the build event system. Accessing these
  395. streams can cause an infinite loop in Ant. Depending on the version of Ant, this will
  396. either cause the build to terminate or the Java VM to run out of Stack space. A logger, also, may
  397. not access System.out and System.err directly. It must use the streams with which it has
  398. been configured.
  399. </p>
  400. <hr>
  401. <h2><a name="integration">Source code integration</a></h2>
  402. The other way to extend Ant through Java is to make changes to existing tasks, which is positively encouraged.
  403. Both changes to the existing source and new tasks can be incorporated back into the Ant codebase, which
  404. benefits all users and spreads the maintenance load around.
  405. <p>
  406. Please consult the
  407. <a href="http://jakarta.apache.org/site/getinvolved.html">Getting Involved</a> pages on the Jakarta web site
  408. for details on how to fetch the latest source and how to submit changes for reincorporation into the
  409. source tree.
  410. <p>
  411. Ant also has some
  412. <a href="http://ant.apache.org/ant_task_guidelines.html">task guidelines</a>
  413. which provides some advice to people developing and testing tasks. Even if you intend to
  414. keep your tasks to yourself, you should still read this as it should be informative.
  415. <hr>
  416. <p align="center">Copyright &copy; 2000-2004 The Apache Software Foundation. All rights
  417. Reserved.</p>
  418. </body>
  419. </html>