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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  4. <title>Apache Ant User Manual</title>
  5. </head>
  6. <body>
  7. <h1>Developing with Ant</h1>
  8. <h2><a name="writingowntask">Writing Your Own Task</a></h2>
  9. <p>It is very easy to write your own task:</p>
  10. <ol>
  11. <li>Create a Java class that extends <code>org.apache.tools.ant.Task</code>.</li>
  12. <li>For each attribute, write a <i>setter</i> method. The setter method must be a
  13. <code>public void</code> method that takes a single argument. The
  14. name of the method must begin with <code>set</code>, followed by the
  15. attribute name, with the first character of the name in uppercase, and the rest in
  16. lowercase. The type of the attribute can be:
  17. <ul>
  18. <li>
  19. <code>String</code>
  20. </li>
  21. <li>
  22. any primitive type (they are converted for you from their String-representation
  23. in the buildfile)
  24. </li>
  25. <li>
  26. boolean - your method will be passed the value
  27. <i>true</i> if the value specified in the buildfile is one of <code>true</code>,
  28. <code>yes</code>, or <code>on</code>)
  29. </li>
  30. <li>
  31. <code>Class</code>
  32. </li>
  33. <li>
  34. <code>File</code>
  35. (in which case the value of the attribute is interpreted relative to the
  36. project's basedir)
  37. </li>
  38. <li>
  39. any other type that has a constructor with a single
  40. <code>String</code> argument
  41. </li>
  42. </ul>
  43. </li>
  44. <li>If your task has enumerated attributes, you should consider using
  45. a subclass of <code>org.apache.tools.ant.types.EnumeratedAttribute</code>
  46. as an argument
  47. to your setter method. See
  48. <code>org/apache/tools/ant/taskdefs/FixCRLF.java</code> for
  49. an example.</li>
  50. <li>If the task should support character data, write a <code>public void
  51. addText(String)</code> method.</li>
  52. <li>For each nested element, write a <i>create</i> or <i>add</i> method.
  53. A create method
  54. must be a <code>public</code> method that takes no arguments and returns
  55. an <code>Object</code> type. The name of the create method must begin with
  56. <code>create</code>, followed by the element name. An add method must be
  57. a <code>public void</code> method that takes a single argument of an
  58. <code>Object</code> type with a no-argument constructor.
  59. The name of the add method
  60. must begin with <code>add</code>, followed by the element name.</li>
  61. <li>Write a <code>public void execute</code> method, with no arguments, that
  62. throws a <code>BuildException</code>. This method implements the task
  63. itself.</li>
  64. </ol>
  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><code>execute()</code> 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>Example</h3>
  100. <p>Let's write our own task, which prints a message on the
  101. <code>System.out</code> stream.
  102. The
  103. task has one attribute, called <code>message</code>.</p>
  104. <blockquote>
  105. <pre>
  106. package com.mydomain;
  107. import org.apache.tools.ant.BuildException;
  108. import org.apache.tools.ant.Task;
  109. public class MyVeryOwnTask extends Task {
  110. private String msg;
  111. // The method executing the task
  112. public void execute() throws BuildException {
  113. System.out.println(msg);
  114. }
  115. // The setter for the &quot;message&quot; attribute
  116. public void setMessage(String msg) {
  117. this.msg = msg;
  118. }
  119. }
  120. </pre>
  121. </blockquote>
  122. <p>It's really this simple ;-)</p>
  123. <p>Adding your task to the system is rather simple too:</p>
  124. <ol>
  125. <li>Make sure the class that implements your task is in the classpath when
  126. starting Ant.</li>
  127. <li>Add a <code>&lt;taskdef&gt;</code> element to your project.
  128. This actually adds your task to the system.</li>
  129. <li>Use your task in the rest of the buildfile.</li>
  130. </ol>
  131. <h3>Example</h3>
  132. <blockquote>
  133. <pre>
  134. &lt;?xml version=&quot;1.0&quot;?&gt;
  135. &lt;project name=&quot;OwnTaskExample&quot; default=&quot;main&quot; basedir=&quot;.&quot;&gt;
  136. &lt;taskdef name=&quot;mytask&quot; classname=&quot;com.mydomain.MyVeryOwnTask&quot;/&gt;
  137. &lt;target name=&quot;main&quot;&gt;
  138. &lt;mytask message=&quot;Hello World! MyVeryOwnTask works!&quot;/&gt;
  139. &lt;/target&gt;
  140. &lt;/project&gt;
  141. </pre>
  142. </blockquote>
  143. <h3>Example 2</h3>
  144. To use a task directly from the buildfile which created it, place the
  145. <code>&lt;taskdef&gt;</code> declaration inside a target
  146. <i>after the compilation</i>. Use the <code>classpath</code> attribute of
  147. <code>&lt;taskdef&gt;</code> to point to where the code has just been
  148. compiled.
  149. <blockquote>
  150. <pre>
  151. &lt;?xml version=&quot;1.0&quot;?&gt;
  152. &lt;project name=&quot;OwnTaskExample2&quot; default=&quot;main&quot; basedir=&quot;.&quot;&gt;
  153. &lt;target name=&quot;build&quot; &gt;
  154. &lt;mkdir dir=&quot;build&quot;/&gt;
  155. &lt;javac srcdir=&quot;source&quot; destdir=&quot;build&quot;/&gt;
  156. &lt;/target&gt;
  157. &lt;target name=&quot;declare&quot; depends=&quot;build&quot;&gt;
  158. &lt;taskdef name=&quot;mytask&quot;
  159. classname=&quot;com.mydomain.MyVeryOwnTask&quot;
  160. classpath=&quot;build&quot;/&gt;
  161. &lt;/target&gt;
  162. &lt;target name=&quot;main&quot; depends=&quot;declare&quot;&gt;
  163. &lt;mytask message=&quot;Hello World! MyVeryOwnTask works!&quot;/&gt;
  164. &lt;/target&gt;
  165. &lt;/project&gt;
  166. </pre>
  167. </blockquote>
  168. <p>Another way to add a task (more permanently), is to add the task name and
  169. implementing class name to the <code>default.properties</code> file in the
  170. <code>org.apache.tools.ant.taskdefs</code>
  171. package. Then you can use it as if it were a built-in task.</p>
  172. <hr>
  173. <h2><a name="buildevents">Build Events</a></h2>
  174. <P>Ant is capable of generating build events as it performs the tasks necessary to build a project.
  175. Listeners can be attached to Ant to receive these events. This capability could be used, for example,
  176. to connect Ant to a GUI or to integrate Ant with an IDE.
  177. </P>
  178. <p>To use build events you need to create an ant <code>Project</code> object. You can then call the
  179. <code>addBuildListener</code> method to add your listener to the project. Your listener must implement
  180. the <code>org.apache.tools.antBuildListener</code> interface. The listener will receive BuildEvents
  181. for the following events</P>
  182. <ul>
  183. <li>Build started</li>
  184. <li>Build finished</li>
  185. <li>Target started</li>
  186. <li>Target finished</li>
  187. <li>Task started</li>
  188. <li>Task finished</li>
  189. <li>Message logged</li>
  190. </ul>
  191. <p>
  192. If you wish to attach a listener from the command line you may use the
  193. <code>-listener</code> option. For example:</p>
  194. <blockquote>
  195. <pre>ant -listener org.apache.tools.ant.XmlLogger</pre>
  196. </blockquote>
  197. <p>will run Ant with a listener that generates an XML representation of the build progress. This
  198. listener is included with Ant, as is the default listener, which generates the logging to standard output.</p>
  199. <hr>
  200. <h2><a name="integration">Source code integration</a></h2>
  201. The other way to extend Ant through Java is to make changes to existing tasks, which is positively encouraged.
  202. Both changes to the existing source and new tasks can be incorporated back into the Ant codebase, which
  203. benefits all users and spreads the maintenance load around.
  204. <p>
  205. Please consult the
  206. <a href="http://jakarta.apache.org/site/getinvolved.html">Getting Involved</a> pages on the Jakarta web site
  207. for details on how to fetch the latest source and how to submit changes for reincorporation into the
  208. source tree.
  209. <p>
  210. Ant also has some
  211. <a href="http://jakarta.apache.org/ant/ant_task_guidelines.html">task guidelines</a>
  212. which provides some advice to people developing and testing tasks. Even if you intend to
  213. keep your tasks to yourself, you should still read this as it should be informative.
  214. <hr>
  215. <p align="center">Copyright &copy; 2001 Apache Software Foundation. All rights
  216. Reserved.</p>
  217. </body>
  218. </html>