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.

ant_task_guidelines.html 14 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <html><head>
  2. <title>
  3. Apache Ant Task Design Guidelines
  4. </title>
  5. </head><body>
  6. <h1>Apache Ant Task Design Guidelines</h1>
  7. This document covers how to write ant tasks to a standard required to be
  8. incorporated into the Ant distribution. You may find it useful when
  9. writing tasks for personal use as the issues it addresses are still
  10. there in such a case.
  11. <h2>Use built in helper classes</h2>
  12. Ant includes helper tasks to simplify mauch of your work. Be warned that
  13. these helper classes will look very different in ant2.0 from these 1.x
  14. versions. However it is still better to use them than roll your own, for
  15. development, maintenance and code size reasons.
  16. <h4>Execute</h4>
  17. Execute will spawn off separate programs under all the platforms which
  18. ant supports, dealing with Java version issues as well as platform
  19. issues. Always use this task to invoke other programs.
  20. <h4>Java, ExecuteJava</h4>
  21. These classes can be used to spawn Java programs in a separate VM (they
  22. use execute) or in the same VM -with or without a different classloader.
  23. When deriving tasks from this, it often benefits users to permit the
  24. classpath to be specified, and for forking to be an optional attribute.
  25. <h4>Project</h4>
  26. Project has some helper functions to touch a file, to
  27. copy a file and the like. Use these instead of trying to code them
  28. yourself -or trying to use tasks which may be less stable and fiddlier
  29. to use.
  30. <h2>Obey the Sun/Java style guidelines</h2>
  31. The Ant codebase aims to have a single unified coding standard, and that
  32. standard is the
  33. <a href="http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html">
  34. Sun Java coding guidelines
  35. </a>
  36. <p>
  37. It's not that they are better than any alternatives, but they are a
  38. standard and they are what is consistently used in the rest of the
  39. tasks. Code will not be incorporated into the database until it complies
  40. with these.
  41. <p>
  42. If you are writing a task for your personal or organisational use, you
  43. are free to use whatever style you like. But using the Sun Java style
  44. will help you to become comfortable with the rest of the Ant source,
  45. which may be important.
  46. <p>
  47. One important rule is 'no tabs'. Use four spaces instead. Not two,
  48. not eight, four. Even if your editor is configured to have a tab of four
  49. spaces, lots of others aren't -spaces have more consistency across
  50. editors and platforms.
  51. <h2>Recommended Names for attributes and elements</h2>
  52. The ant1.x tasks are very inconsistent regarding naming of attributes
  53. -some tasks use source, others src. Here is a list of preferred attribute
  54. names.
  55. <table>
  56. <tr>
  57. <td>
  58. failonerror
  59. </td>
  60. <td>
  61. boolean to control whether failure to execute should throw a
  62. <tt>BuildException</tt> or just print an error.
  63. Parameter validation failures should always throw an error, regardless
  64. of this flag
  65. </td>
  66. </tr>
  67. <tr>
  68. <td>
  69. destdir
  70. </td>
  71. <td>
  72. destination directory for output
  73. </td>
  74. </tr>
  75. </table>
  76. Yes, this is a very short list. Try and be vaguely consistent with the core
  77. tasks, at the very least.
  78. <h2>Design for controlled re-use</h2>
  79. Keep member variables private. If read access by subclasses is required.
  80. add accessor methods rather than change the accessiblity of the member.
  81. This enables subclasses to access the contents, yet
  82. still be decoupled from the actual implementation.
  83. <p>
  84. The other common re-use mechanism in ant is for one task to create and
  85. configure another. This is fairly simple.
  86. <h2>Do your own Dependency Checking</h2>
  87. Make has the edge over Ant in its integrated dependency checking: the
  88. command line apps make invokes dont need to do their own work. Ant tasks
  89. do have to do their own dependency work, but if this can be done then
  90. it can be done well. A good dependency aware task can work out the dependencies
  91. without explicit dependency information in the build file, and be smart
  92. enough to work out the real dependencies, perhaps through a bit of file parsing.
  93. The <tt>depends</tt> task is the best example of this. Some of the zip/jar
  94. tasks are pretty good too, as they can update the archive when needed.
  95. Most tasks just compare source and destination timestamps and work from there.
  96. Tasks which don't do any dependency checking do not help users as much as
  97. they can, because their needless work can trickle through the entire build, test
  98. and deploy process.
  99. <h2>Support Java 1.1 through Java 1.4</h2>
  100. Ant is designed to support Java1.1: to build on it, to run on it. Sometimes
  101. functionality of tasks have to degrade in that environment -&lt;touch&gt;
  102. is a case in point- this is usually due to library limitations;
  103. such behaviour change must always be noted in the documentation.
  104. <p>
  105. What is problematic is code which is dependent on Java1.2 features
  106. -Collections, Reader and Writer classes, extra methods in older classes.
  107. These can not be used directly by any code and still be able to compile
  108. and run on a Java 1.1 system. So please stick to the older collection
  109. classes, and the older IO classes. If a new method in an existing class
  110. is to be used, it must be used via reflection and the
  111. <tt>NoSuchMethodException</tt> handled somehow.
  112. <p>
  113. What if code simply does not work on Java1.1? It can happen. It will
  114. probably be OK to have the task as an optional task, with compilation
  115. restricted to Java1.2 or later through build.xml modifications.
  116. Better still, use reflection to link to the classes at run time.
  117. <p>
  118. Java 1.4 adds a new optional change to the language itself, the
  119. <tt>assert</tt> keyword, which is only enabled if the compiler is told
  120. to compile 1.4 version source. Clearly with the 1.1 compatibility requirement,
  121. Ant tasks can not use this keyword. They also need to move away from
  122. using the JUnit <tt>assert()</tt> method and start calling <tt>assertTrue()</tt>
  123. instead.
  124. <h2>Refactor</h2>
  125. If the changes made to a task are making it too unwieldy, split it up
  126. into a cleaner design, refactor the code and submit not just feature
  127. creep but cleaner tasks. A common design pattern which tends to occur in
  128. the ant process is the adoption of the adapter pattern, in which a base
  129. class (say Javac or Rmi) starts off simple, then gets convoluted with
  130. support for multiple back ends -javac, jikes, jvc. A refactoring to
  131. split the programmable front end from the classes which provide the back
  132. end cleans up the design and makes it much easier to add new back ends.
  133. But to carry this off one needs to keep the interface and behaviour of
  134. the front end identical, and to be sure that no subclasses have been
  135. accessing data members directly -because these data members may not
  136. exist in the refactored design. Which is why having private data members
  137. is so important.
  138. <h2>Test</h2>
  139. Look in <tt>jakarta-ant/src/testcases</tt> and you will find JUnit tests for the
  140. shipping ant tasks, to see how it is done and what is expected of a new
  141. task. Most of them are rudimentary, and no doubt you could do better for
  142. your task -feel free to do so!
  143. <p>
  144. A well written set of test cases will break the Ant task while it is in
  145. development, until the code is actually complete. And every bug which
  146. surfaces later should have a test case added to demonstrate the problem,
  147. and to fix it.
  148. <p>
  149. The test cases are a great way of testing your task during development.
  150. A simple call to 'ant run-test' in the ant source tree will run all ant
  151. tests, to verify that your changes don't break anything.
  152. To test a single task, use the one shot <code>ant run-single-test
  153. -Dtestcase=${testname}</code> where ${testname} is the name of your test class.
  154. <p>
  155. The test cases are also used by the committers to verify that changes
  156. and patches do what they say. If you've got test cases it increases your
  157. credibility significantly.
  158. <p>
  159. Remember also that Ant 1.x is designed to compile and run on Java1.1, so
  160. you should test on Java 1.1 as well as any later version which you use.
  161. You can download an old SDK from Sun for this purpose.
  162. <h2>Document</h2>
  163. Without documentation, the task can't be used. So remember to provide a
  164. succint and clear html (soon, xml) page describing the task in a similar
  165. style to that of existing tasks. It should include a list of attributes
  166. and elements, and at least one working example of the task. Many users
  167. cut and paste the examples into their build files as a starting point,
  168. so make the examples practical and test them too.
  169. <h2>Licensing and Copyright</h2>
  170. Any code submitted to the Apache project must be compatible with the
  171. Apache Software License, and the act of submission must be viewed as an
  172. implicit transfer of ownership of the submitted code to the Apache
  173. Software Foundation.
  174. <p>
  175. This is important.
  176. <p>
  177. The fairly laissez-faire license of Apache is not compabitible with
  178. either the GPL or the Lesser GPL of the Free Software Foundation -the
  179. Gnu project. Their license requires all changes to the source to be made
  180. public, and give the licensee of any software the right to distribute
  181. copies. It also requires derivative works to be made available under the
  182. same license terms. None of these requirements are in the Apache Software
  183. Foundation license, which permits people and organisations to build
  184. commercial and closed source applications atop the Apache libraries and
  185. source -but not use the Apache, Ant or Jakarta Project names without
  186. permission.
  187. <p>
  188. Because the Gnu GPL license immediately extends to cover any larger
  189. application (or library, in the case of GLPL) into which it is
  190. incorporated, the Ant team can not incorporate any task based upon GPL
  191. or LGPL source into the Ant codebase. You are free to submit it, but it
  192. will be politely and firmly rejected.
  193. <p>
  194. Once ant-2 adds better dynamic task incorporation, it may be possible to
  195. provide a framework for supporting [L]GPL code, but still no tasks
  196. direcely subject to the Gnu licenses will ever be included in the Ant
  197. CVS tree.
  198. <h3>Dont re-invent the wheel</h3>
  199. We've all done it: written and submitted a task only to discover it
  200. was already implemented in a small corner of another task, or it has
  201. been submitted by someone else and not committed. You can avoid this
  202. by being aware of what is in the latest CVS tree -keep getting the daily
  203. source updates, look at manual changes and subscribe to the ant-dev
  204. mailing list.
  205. <p>
  206. If you are thinking of writing a task, posting a note on your thoughts
  207. to the list can be informative -you well get other peoples insight and
  208. maybe some half written task to do the basics, all without writing a
  209. line of code.
  210. <h2>Submitting to Ant</h2>
  211. The process for submitting an Ant task is documented on the
  212. <a href="http://jakarta.apache.org/site/guidelines.html">
  213. jakarta web site</a>.
  214. The basic mechanism is to mail it to the ant-dev mailing list.
  215. It helps to be on this list, as you will see other submissions, and
  216. any debate about your own submission.
  217. <p>
  218. Patches to existing files should be generated with
  219. <code>cvs diff -u filename</code>
  220. and save the output to a file. If you want to get
  221. the changes made to multiple files in a directory , just use <code>cvs
  222. diff -u</code>. The patches should be sent as an attachment to a message titled [PATCH]
  223. and distinctive one-line summary in the subject of the patch. The
  224. filename/task and the change usually suffices. It's important to include
  225. the changes as an attachment, as too many mailers reformat the text
  226. pasted in, which breaks the patch.
  227. <p>
  228. Then you wait for one of the committers to commit the patch, if it is
  229. felt appropriate to do so. Bug fixes go in quickly, other changes
  230. often spark a bit of discussion before a (perhaps revised) commit is
  231. made.
  232. <p>
  233. New submissions should be proceeded with [SUBMIT]. The mailer-daemon
  234. will reject any messages over 100KB, so any large update should be
  235. zipped up. If your submission is bigger than that, why not break it up
  236. into separate tasks.
  237. <p>
  238. If you hear nothing after a couple of weeks, remind the mailing list.
  239. Sometimes really good submissions get lost in the noise of other issues.
  240. This is particularly the case just prior to a new point release of
  241. the product. At that time anything other than bug fixes will tend
  242. to be neglected.
  243. <h2>Checklists</h2>
  244. These are the things you should verify before submitting patches and new
  245. tasks. Things don't have to be perfect, it may take a couple of
  246. iterations before a patch or submission is committed, and these items
  247. can be addressed in the process. But by the time the code is committed,
  248. everything including the documentation and some test cases will have
  249. been done, so by getting them out the way up front can save time.
  250. The committers look more favourably on patches and submissions with test
  251. cases, while documentation helps sell the reason for a task.
  252. <h3>Checklist before submitting a patch</h3>
  253. <ul>
  254. <li>Added code complies with style guidelines
  255. <li>Code compiles and runs on Java1.1
  256. <li>New member variables are private, and provide public accessor methods
  257. if access is actually needed.
  258. <li>Existing test cases succeed.
  259. <li>New test cases written and succeed.
  260. <li>Documentation page extended as appropriate.
  261. <li>Example task declarations in the documentation tested.
  262. <li>Diff files generated using cvs diff -u
  263. <li>Message to ant-dev contains [PATCH], task name and patch reason in
  264. subject.
  265. <li>Message body contains a rationale for the patch.
  266. <li>Message attachment contains the patch file(s).
  267. </ul>
  268. <h3>Checklist before submitting a new task</h3>
  269. <ul>
  270. <li>Java file begins with Apache copyright and license statement.
  271. <li>Task does not depend on GPL or LGPL code.
  272. <li>Source code complies with style guidelines
  273. <li>Code compiles and runs on Java1.1
  274. <li>Member variables are private, and provide public accessor methods
  275. if access is actually needed.
  276. <li><i>Maybe</i> Task has failonerror attribute to control failure behaviour
  277. <li>New test cases written and succeed
  278. <li>Documentation page written
  279. <li>Example task declarations in the documentation tested.
  280. <li>Patch files generated using cvs diff -u
  281. <li>patch files include a patch to defaults.properties to register the
  282. tasks
  283. <li>patch files include a patch to coretasklist.html or
  284. optionaltasklist.html to link to the new task page
  285. <li>Message to ant-dev contains [SUBMIT] and task name in subject
  286. <li>Message body contains a rationale for the task
  287. <li>Message attachments contain the required files -source, documentation,
  288. test and patches
  289. </ul>
  290. <hr>
  291. <p align="center">Copyright &copy; 2001 Apache Software Foundation. All rights
  292. Reserved.</p>
  293. </body></html>