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

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