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

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