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

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