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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <html><head>
  2. <title>
  3. Ant Task Design Guidelines
  4. </title>
  5. </head><body>
  6. <h1>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 sublties 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. <h4>Project</h4>
  24. Project has some helper functions to touch a file, to
  25. copy a file and the like. Use these instead of trying to code them
  26. yourself -or trying to use tasks which may be less stable and fiddlier
  27. to use.
  28. <h2>Obey the Sun/Java style guidelines</h2>
  29. The Ant codebase aims to have a single unified coding standard, and that
  30. standard is the
  31. <a href="http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html">
  32. Sun Java coding guidelines
  33. </a>
  34. <p>
  35. It's not that they are better than any alternatives, but they are a
  36. standard and they are what is consistently used in the rest of the
  37. tasks. Code will not be incorporated into the database until it complies
  38. with these.
  39. <p>
  40. If you are writing a task for your personal or organisational use, you
  41. are free to use whatever style you like. But using the Sun Java style
  42. will help you to become comfortable with the rest of the Ant source,
  43. which may be important.
  44. <p>
  45. One controversial rule is 'no tabs'. Use four spaces instead. Not two,
  46. not eight, four. Even if your editor is configured to have a tab of four
  47. spaces, lots of others aren't -spaces have more consistency across
  48. editors and platforms.
  49. <h2>Recommended Names for attributes and elements</h2>
  50. The ant1.x tasks are fairly inconsistent regarding naming of attributes
  51. -some tasks use source, others src. Here is a list of what is likely to
  52. be the preferred attribute names for ant 2.0.
  53. <i>TODO: list attribute/element names which should be standardised, and meaning</i>
  54. failonerror, source, dest...
  55. <h2>Design for controlled re-use</h2>
  56. Keep member variables private. If read access by subclasses is required.
  57. add accessor methods rather than change the accessiblity of the member.
  58. This enables subclasses to access the contents, yet
  59. still be decoupled from the actual implementation.
  60. <p>
  61. The other common re-use mechanism in ant is for one task to create and
  62. configure another. This is fairly simple. (TODO: example)
  63. <h2>Refactor</h2>
  64. If the changes made to a task are making it too unwieldy, split it up
  65. into a cleaner design, refactor the code and submit not just feature
  66. creep but cleaner tasks. A common design pattern which tends to occur in
  67. the ant process is the adoption of the adapter pattern, in which a base
  68. class (say Javac or Rmi) starts off simple, then gets convoluted with
  69. support for multiple back ends -javac, jikes, jvc. A refactoring to
  70. split the programmable front end from the classes which provide the back
  71. end cleans up the design and makes it much easier to add new back ends.
  72. But to carry this off one needs to keep the interface and behaviour of
  73. the front end identical, and to be sure that no subclasses have been
  74. accessing data members directly -because these data members may not
  75. exist in the refactored design. Which is why having private data members
  76. is so important.
  77. <h2>Test</h2>
  78. Look in jakarta-ant/src/testcases and you will find Junit tests for the
  79. shipping ant tasks, to see how it is done and what is expected of a new
  80. task. Most of them are rudimentary, and no doubt you could do better for
  81. your task -feel free to do so!
  82. <p>
  83. A well written set of test cases will break the ant task while it is in
  84. development, until the code is actually complete. And every bug which
  85. surfaces later should have a test case added to demonstrate the problem,
  86. and to fix it.
  87. <p>
  88. The test cases are a great way of testing your task during development.
  89. A simple call to 'ant run-test' in the ant source tree will run all ant
  90. tests, to verify that your changes don't break anything.
  91. To test a single task, use the one shot <code>ant run-single-test
  92. -Dtestcase=${testname}</code> where ${testname} is the name of your test class.
  93. <p>
  94. The test cases are also used by the committers to verify that changes
  95. and patches do what they say. If you've got test cases it increases your
  96. credibility significantly.
  97. <p>
  98. Remember also that ant 1.x is designed to compile and run on Java1.1, so
  99. you should test on java 1.1 as well as any later version which you use.
  100. If you are developing on Windows you may well have the Microsoft JVM at
  101. hand for this, otherwise you can download an old SDK or runtime from
  102. Sun.
  103. <h2>Document</h2>
  104. Without documentation, the task can't be used. So remember to provide a
  105. succint and clear html (soon, xml) page describing the task in a similar
  106. style to that of existing tasks. It should include a list of attributes
  107. and elements, and at least one working example of the task. Many users
  108. cut and paste the examples into their build files as a starting point,
  109. so make the examples practical and test them too.
  110. <h2>Licensing and Copyright</h2>
  111. Any code submitted to the Apache project must be compatible with the
  112. Apache Software License, and the act of submission must be viewed as an
  113. implicit transfer of ownership of the submitted code to the Apache
  114. Software Foundation.
  115. <p>
  116. This is important.
  117. <p>
  118. The fairly laissez-faire license of Apache is not compabitible with
  119. either the GPL or the Lesser GPL of the Free Software Foundation -the
  120. Gnu project. Their license requires all changes to the source to be made
  121. public, and give the licensee of any software the right to distribute
  122. copies. It also requires derivative works to be made available under the
  123. same license terms. None of these requirements are in the Apache Software
  124. Foundation license, which permits people and organisations to build
  125. commercial and closed source applications atop the Apache libraries and
  126. source -but not use the Apache, Ant or Jakarta Project names without
  127. permission.
  128. <p>
  129. Because the Gnu GPL license immediately extends to cover any larger
  130. application (or library, in the case of GLPL) into which it is
  131. incorporated, the Ant team can not incorporate any task based upon GPL
  132. or LGPL source into the Ant codebase. You are free to submit it, but it
  133. will be politely and firmly rejected.
  134. <p>
  135. Once ant-2 adds better dynamic task incorporation, it may be possible to
  136. provide a framework for supporting [L]GPL code, but still no tasks
  137. direcely subject to the Gnu licenses will ever be included in the Ant
  138. CVS tree.
  139. <h3>Dont re-invent the wheel</h3>
  140. We've all done it: written and submitted a task only to discover it
  141. was already implemented in a small corner of another task, or it has
  142. been submitted by someone else and not committed. You can avoid this
  143. by being aware of what is in the latest CVS tree -keep getting the daily
  144. source updates, look at manual changes and subscribe to the ant-dev
  145. mailing list.
  146. <p>
  147. If you are thinking of writing a task, posting a note on your thoughts
  148. to the list can be informative -you well get other peoples insight and
  149. maybe some half written task to do the basics, all without writing a
  150. line of code.
  151. <h2>Submitting to Ant</h2>
  152. The process for submitting an ant task is documented on the
  153. <a href="http://jakarta.apache.org/site/guidelines.html">
  154. jakarta web site</a>.
  155. The basic mechanism is to mail it to the ant-dev mailing list.
  156. It helps to be on this list, as you will see other submissions, and
  157. any debate about your own submission.
  158. <p>
  159. Patches to existing files should be generated with <code>cvs diff -u filename</code>
  160. and save the output to a file. If you want to get
  161. the changes made to multiple files in a directory , just use <code>cvs
  162. diff -u</code>. The patches should be sent as an attachment to a message titled [PATCH]
  163. and distinctive one-line summary in the subject of the patch. The
  164. filename/task and the change usually suffices. It's important to include
  165. the changes as an attachment, as too many mailers reformat the text
  166. pasted in, which breaks the patch.
  167. <p>
  168. Then you wait for one of the committers to commit the patch, if it is
  169. felt appropriate to do so. Bug fixes go in quickly, other changes
  170. often spark a bit of discussion before a (perhaps revised) commit is
  171. made.
  172. <p>
  173. New submissions should be proceeded with [SUBMIT]. The mailer-daemon
  174. will reject any messages over 100KB, so any large update should be
  175. zipped up. If your submission is bigger than that, why not break it up
  176. into separate tasks.
  177. <h2>Checklists</h2>
  178. These are the things you should verify before submitting patches and new
  179. tasks. Things don't have to be perfect, it may take a couple of
  180. iterations before a patch or submission is committed, and these items
  181. can be addressed in the process. But by the time the code is committed,
  182. everything including the documentation and some test cases will have
  183. been done, so by getting them out the way up front can save time.
  184. The committers look more favourably on patches and submissions with test
  185. cases, while documentation helps sell the reason for a task.
  186. <h3>Checklist before submitting a patch</h3>
  187. <ul>
  188. <li>Added code complies with style guidelines
  189. <li>New member variables are private, and provide public accessor methods
  190. if access is actually needed.
  191. <li>Existing test cases succeed.
  192. <li>New test cases written and succeed.
  193. <li>Documentation page extended as appropriate.
  194. <li>Example task declarations in the documentation tested.
  195. <li>Diff files generated using cvs diff -u
  196. <li>Message to ant-dev contains [PATCH], task name and patch reason in
  197. subject.
  198. <li>Message body contains a rationale for the patch.
  199. <li>Message attachment contains the patch file(s).
  200. </ul>
  201. <h3>Checklist before submitting a new task</h3>
  202. <ul>
  203. <li>Java file begins with Apache copyright and license statement.
  204. <li>Task does not depend on GPL or LGPL code.
  205. <li>Source code complies with style guidelines
  206. <li>Member variables are private, and provide public accessor methods
  207. if access is actually needed.
  208. <li><i>Maybe</i> Task has failonerror attribute to control failure behaviour
  209. <li>New test cases written and succeed
  210. <li>Documentation page written
  211. <li>Example task declarations in the documentation tested.
  212. <li>Patch files generated using cvs diff -u
  213. <li>patch files include a patch to defaults.properties to register the
  214. tasks
  215. <li>patch files include a patch to coretasklist.html or
  216. optionaltasklist.html to link to the new task page
  217. <li>Message to ant-dev contains [SUBMIT] and task name in subject
  218. <li>Message body contains a rationale for the task
  219. <li>Message attachments contain the required files -source, documentation,
  220. test and patches
  221. </ul>
  222. <hr>
  223. <p align="center">Copyright &copy; 2001 Apache Software Foundation. All rights
  224. Reserved.</p>
  225. </body></html>