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 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 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>Support Java 1.1 through Java 1.4</h2>
  87. Ant is designed to support Java1.1: to build on it, to run on it. Sometimes
  88. functionality of tasks have to degrade in that environment -&lt;touch&gt;
  89. is a case in point- this is usually due to library limitations;
  90. such behaviour change must always be noted in the documentation.
  91. <p>
  92. What is problematic is code which is dependent on Java1.2 features
  93. -Collections, Reader and Writer classes, extra methods in older classes.
  94. These can not be used directly by any code and still be able to compile
  95. and run on a Java 1.1 system. So please stick to the older collection
  96. classes, and the older IO classes. If a new method in an existing class
  97. is to be used, it must be used via reflection and the
  98. <tt>NoSuchMethodException</tt> handled somehow.
  99. <p>
  100. What if code simply does not work on Java1.1? It can happen. It will
  101. probably be OK to have the task as an optional task, with compilation
  102. restricted to Java1.2 or later through build.xml modifications.
  103. Better still, use reflection to link to the classes at run time.
  104. <p>
  105. Java 1.4 adds a new optional change to the language itself, the
  106. <tt>assert</tt> keyword, which is only enabled if the compiler is told
  107. to compile 1.4 version source. Clearly with the 1.1 compatibility requirement,
  108. Ant tasks can not use this keyword. They also need to move away from
  109. using the JUnit <tt>assert()</tt> method and start calling <tt>assertTrue()</tt>
  110. instead.
  111. <h2>Refactor</h2>
  112. If the changes made to a task are making it too unwieldy, split it up
  113. into a cleaner design, refactor the code and submit not just feature
  114. creep but cleaner tasks. A common design pattern which tends to occur in
  115. the ant process is the adoption of the adapter pattern, in which a base
  116. class (say Javac or Rmi) starts off simple, then gets convoluted with
  117. support for multiple back ends -javac, jikes, jvc. A refactoring to
  118. split the programmable front end from the classes which provide the back
  119. end cleans up the design and makes it much easier to add new back ends.
  120. But to carry this off one needs to keep the interface and behaviour of
  121. the front end identical, and to be sure that no subclasses have been
  122. accessing data members directly -because these data members may not
  123. exist in the refactored design. Which is why having private data members
  124. is so important.
  125. <h2>Test</h2>
  126. Look in <tt>jakarta-ant/src/testcases</tt> and you will find JUnit tests for the
  127. shipping ant tasks, to see how it is done and what is expected of a new
  128. task. Most of them are rudimentary, and no doubt you could do better for
  129. your task -feel free to do so!
  130. <p>
  131. A well written set of test cases will break the Ant task while it is in
  132. development, until the code is actually complete. And every bug which
  133. surfaces later should have a test case added to demonstrate the problem,
  134. and to fix it.
  135. <p>
  136. The test cases are a great way of testing your task during development.
  137. A simple call to 'ant run-test' in the ant source tree will run all ant
  138. tests, to verify that your changes don't break anything.
  139. To test a single task, use the one shot <code>ant run-single-test
  140. -Dtestcase=${testname}</code> where ${testname} is the name of your test class.
  141. <p>
  142. The test cases are also used by the committers to verify that changes
  143. and patches do what they say. If you've got test cases it increases your
  144. credibility significantly.
  145. <p>
  146. Remember also that Ant 1.x is designed to compile and run on Java1.1, so
  147. you should test on Java 1.1 as well as any later version which you use.
  148. You can download an old SDK from Sun for this purpose.
  149. <h2>Document</h2>
  150. Without documentation, the task can't be used. So remember to provide a
  151. succint and clear html (soon, xml) page describing the task in a similar
  152. style to that of existing tasks. It should include a list of attributes
  153. and elements, and at least one working example of the task. Many users
  154. cut and paste the examples into their build files as a starting point,
  155. so make the examples practical and test them too.
  156. <h2>Licensing and Copyright</h2>
  157. Any code submitted to the Apache project must be compatible with the
  158. Apache Software License, and the act of submission must be viewed as an
  159. implicit transfer of ownership of the submitted code to the Apache
  160. Software Foundation.
  161. <p>
  162. This is important.
  163. <p>
  164. The fairly laissez-faire license of Apache is not compabitible with
  165. either the GPL or the Lesser GPL of the Free Software Foundation -the
  166. Gnu project. Their license requires all changes to the source to be made
  167. public, and give the licensee of any software the right to distribute
  168. copies. It also requires derivative works to be made available under the
  169. same license terms. None of these requirements are in the Apache Software
  170. Foundation license, which permits people and organisations to build
  171. commercial and closed source applications atop the Apache libraries and
  172. source -but not use the Apache, Ant or Jakarta Project names without
  173. permission.
  174. <p>
  175. Because the Gnu GPL license immediately extends to cover any larger
  176. application (or library, in the case of GLPL) into which it is
  177. incorporated, the Ant team can not incorporate any task based upon GPL
  178. or LGPL source into the Ant codebase. You are free to submit it, but it
  179. will be politely and firmly rejected.
  180. <p>
  181. Once ant-2 adds better dynamic task incorporation, it may be possible to
  182. provide a framework for supporting [L]GPL code, but still no tasks
  183. direcely subject to the Gnu licenses will ever be included in the Ant
  184. CVS tree.
  185. <h3>Dont re-invent the wheel</h3>
  186. We've all done it: written and submitted a task only to discover it
  187. was already implemented in a small corner of another task, or it has
  188. been submitted by someone else and not committed. You can avoid this
  189. by being aware of what is in the latest CVS tree -keep getting the daily
  190. source updates, look at manual changes and subscribe to the ant-dev
  191. mailing list.
  192. <p>
  193. If you are thinking of writing a task, posting a note on your thoughts
  194. to the list can be informative -you well get other peoples insight and
  195. maybe some half written task to do the basics, all without writing a
  196. line of code.
  197. <h2>Submitting to Ant</h2>
  198. The process for submitting an Ant task is documented on the
  199. <a href="http://jakarta.apache.org/site/guidelines.html">
  200. jakarta web site</a>.
  201. The basic mechanism is to mail it to the ant-dev mailing list.
  202. It helps to be on this list, as you will see other submissions, and
  203. any debate about your own submission.
  204. <p>
  205. Patches to existing files should be generated with
  206. <code>cvs diff -u filename</code>
  207. and save the output to a file. If you want to get
  208. the changes made to multiple files in a directory , just use <code>cvs
  209. diff -u</code>. The patches should be sent as an attachment to a message titled [PATCH]
  210. and distinctive one-line summary in the subject of the patch. The
  211. filename/task and the change usually suffices. It's important to include
  212. the changes as an attachment, as too many mailers reformat the text
  213. pasted in, which breaks the patch.
  214. <p>
  215. Then you wait for one of the committers to commit the patch, if it is
  216. felt appropriate to do so. Bug fixes go in quickly, other changes
  217. often spark a bit of discussion before a (perhaps revised) commit is
  218. made.
  219. <p>
  220. New submissions should be proceeded with [SUBMIT]. The mailer-daemon
  221. will reject any messages over 100KB, so any large update should be
  222. zipped up. If your submission is bigger than that, why not break it up
  223. into separate tasks.
  224. <p>
  225. If you hear nothing after a couple of weeks, remind the mailing list.
  226. Sometimes really good submissions get lost in the noise of other issues.
  227. This is particularly the case just prior to a new point release of
  228. the product. At that time anything other than bug fixes will tend
  229. to be neglected.
  230. <h2>Checklists</h2>
  231. These are the things you should verify before submitting patches and new
  232. tasks. Things don't have to be perfect, it may take a couple of
  233. iterations before a patch or submission is committed, and these items
  234. can be addressed in the process. But by the time the code is committed,
  235. everything including the documentation and some test cases will have
  236. been done, so by getting them out the way up front can save time.
  237. The committers look more favourably on patches and submissions with test
  238. cases, while documentation helps sell the reason for a task.
  239. <h3>Checklist before submitting a patch</h3>
  240. <ul>
  241. <li>Added code complies with style guidelines
  242. <li>Code compiles and runs on Java1.1
  243. <li>New member variables are private, and provide public accessor methods
  244. if access is actually needed.
  245. <li>Existing test cases succeed.
  246. <li>New test cases written and succeed.
  247. <li>Documentation page extended as appropriate.
  248. <li>Example task declarations in the documentation tested.
  249. <li>Diff files generated using cvs diff -u
  250. <li>Message to ant-dev contains [PATCH], task name and patch reason in
  251. subject.
  252. <li>Message body contains a rationale for the patch.
  253. <li>Message attachment contains the patch file(s).
  254. </ul>
  255. <h3>Checklist before submitting a new task</h3>
  256. <ul>
  257. <li>Java file begins with Apache copyright and license statement.
  258. <li>Task does not depend on GPL or LGPL code.
  259. <li>Source code complies with style guidelines
  260. <li>Code compiles and runs on Java1.1
  261. <li>Member variables are private, and provide public accessor methods
  262. if access is actually needed.
  263. <li><i>Maybe</i> Task has failonerror attribute to control failure behaviour
  264. <li>New test cases written and succeed
  265. <li>Documentation page written
  266. <li>Example task declarations in the documentation tested.
  267. <li>Patch files generated using cvs diff -u
  268. <li>patch files include a patch to defaults.properties to register the
  269. tasks
  270. <li>patch files include a patch to coretasklist.html or
  271. optionaltasklist.html to link to the new task page
  272. <li>Message to ant-dev contains [SUBMIT] and task name in subject
  273. <li>Message body contains a rationale for the task
  274. <li>Message attachments contain the required files -source, documentation,
  275. test and patches
  276. </ul>
  277. <hr>
  278. <p align="center">Copyright &copy; 2001 Apache Software Foundation. All rights
  279. Reserved.</p>
  280. </body></html>