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

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