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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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>Don't break existing builds</h2>
  12. Even if you find some really hideous problem with ant, one that is easy
  13. to fix, if your fix breaks an existing build file then we have problems.
  14. Making sure that every build file out there still works, is one of the
  15. goals of all changes. As an example of this, Ant1.5 passes the single
  16. dollar sign "$" through in strings; Ant1.4 and before would strip it. To
  17. get this fix in we first had to write the test suite to expose current
  18. behaviour, then change something so that singe $ was passed through, but
  19. double "$$" got mapped to "$" for backwards compatibility.
  20. <h2>Use built in helper classes</h2>
  21. Ant includes helper tasks to simplify mauch of your work. Be warned that
  22. these helper classes will look very different in ant2.0 from these 1.x
  23. versions. However it is still better to use them than roll your own, for
  24. development, maintenance and code size reasons.
  25. <h4>Execute</h4>
  26. Execute will spawn off separate programs under all the platforms which
  27. ant supports, dealing with Java version issues as well as platform
  28. issues. Always use this task to invoke other programs.
  29. <h4>Java, ExecuteJava</h4>
  30. These classes can be used to spawn Java programs in a separate VM (they
  31. use execute) or in the same VM -with or without a different classloader.
  32. When deriving tasks from this, it often benefits users to permit the
  33. classpath to be specified, and for forking to be an optional attribute.
  34. <h4>Project and related classes</h4>
  35. Project, FileUtils, JavaEnvUtils all have helper functions
  36. to do things like touch a file, to
  37. copy a file and the like. Use these instead of trying to code them
  38. yourself -or trying to use tasks which may be less stable and fiddlier
  39. to use.
  40. <h2>Obey the Sun/Java style guidelines</h2>
  41. The Ant codebase aims to have a single unified coding standard, and that
  42. standard is the
  43. <a href="http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html">
  44. Sun Java coding guidelines
  45. </a>
  46. <p>
  47. It's not that they are better than any alternatives, but they are a
  48. standard and they are what is consistently used in the rest of the
  49. tasks. Code will not be incorporated into the database until it complies
  50. with these.
  51. <p>
  52. If you are writing a task for your personal or organisational use, you
  53. are free to use whatever style you like. But using the Sun Java style
  54. will help you to become comfortable with the rest of the Ant source,
  55. which may be important.
  56. <p>
  57. One important rule is 'no tabs'. Use four spaces instead. Not two,
  58. not eight, four. Even if your editor is configured to have a tab of four
  59. spaces, lots of others aren't -spaces have more consistency across
  60. editors and platforms. Some IDEs (JEdit) can highlight tabs, to stop you
  61. accidentally inserting them
  62. <h2>Attributes and elements</h2>
  63. Use the Ant introspection based mapping of attributes into Java datatypes,
  64. rather than implementing all your attributes as setFoo(String) and doing
  65. the mapping to Int, bool or file yourself. This saves work on your part,
  66. lets Java callers use you in a typesafe manner, and will let the Xdocs
  67. documentation generator work out what the parameters are.
  68. <p>
  69. The ant1.x tasks are very inconsistent regarding naming of attributes
  70. -some tasks use <tt>source</tt>, others <tt>src</tt>.
  71. Here is a list of preferred attribute names.
  72. <table>
  73. <tr>
  74. <td>
  75. failonerror
  76. </td>
  77. <td>
  78. boolean to control whether failure to execute should throw a
  79. <tt>BuildException</tt> or just print an error.
  80. Parameter validation failures should always throw an error, regardless
  81. of this flag
  82. </td>
  83. </tr>
  84. <tr>
  85. <td>
  86. destdir
  87. </td>
  88. <td>
  89. destination directory for output
  90. </td>
  91. </tr>
  92. <td>
  93. destfile
  94. </td>
  95. <td>
  96. destination file for output
  97. </td>
  98. </tr>
  99. <tr>
  100. <td>
  101. srcdir
  102. </td>
  103. <td>
  104. source directory
  105. </td>
  106. </tr>
  107. <tr>
  108. <td>
  109. srcfile
  110. </td>
  111. <td>
  112. source file
  113. </td>
  114. </tr>
  115. </table>
  116. Yes, this is a very short list. Try and be vaguely consistent with the core
  117. tasks, at the very least.
  118. <h2>Support classpaths</h2>
  119. Try and make it possible for people to supply a classpath to your task,
  120. if you need external libraries, rather than make them add everything to
  121. the ANT_HOME\lib directory. This lets people keep the external libraries
  122. in their ant-based project, rather than force all users to make changes
  123. to their ant system configuration.
  124. <h2>Design for controlled re-use</h2>
  125. Keep member variables private. If read access by subclasses is required.
  126. add accessor methods rather than change the accessiblity of the member.
  127. This enables subclasses to access the contents, yet
  128. still be decoupled from the actual implementation.
  129. <p>
  130. The other common re-use mechanism in ant is for one task to create and
  131. configure another. This is fairly simple.
  132. <h2>Do your own Dependency Checking</h2>
  133. Make has the edge over Ant in its integrated dependency checking: the
  134. command line apps make invokes dont need to do their own work. Ant tasks
  135. do have to do their own dependency work, but if this can be done then
  136. it can be done well. A good dependency aware task can work out the dependencies
  137. without explicit dependency information in the build file, and be smart
  138. enough to work out the real dependencies, perhaps through a bit of file parsing.
  139. The <tt>depends</tt> task is the best example of this. Some of the zip/jar
  140. tasks are pretty good too, as they can update the archive when needed.
  141. Most tasks just compare source and destination timestamps and work from there.
  142. Tasks which don't do any dependency checking do not help users as much as
  143. they can, because their needless work can trickle through the entire build, test
  144. and deploy process.
  145. <h2>Support Java 1.1 through Java 1.4</h2>
  146. Ant is designed to support Java1.1: to build on it, to run on it. Sometimes
  147. functionality of tasks have to degrade in that environment -&lt;touch&gt;
  148. is a case in point- this is usually due to library limitations;
  149. such behaviour change must always be noted in the documentation.
  150. <p>
  151. What is problematic is code which is dependent on Java1.2 features
  152. -Collections, Reader and Writer classes, extra methods in older classes.
  153. These can not be used directly by any code and still be able to compile
  154. and run on a Java 1.1 system. So please stick to the older collection
  155. classes, and the older IO classes. If a new method in an existing class
  156. is to be used, it must be used via reflection and the
  157. <tt>NoSuchMethodException</tt> handled somehow.
  158. <p>
  159. What if code simply does not work on Java1.1? It can happen. It will
  160. probably be OK to have the task as an optional task, with compilation
  161. restricted to Java1.2 or later through build.xml modifications.
  162. Better still, use reflection to link to the classes at run time.
  163. <p>
  164. Java 1.4 adds a new optional change to the language itself, the
  165. <tt>assert</tt> keyword, which is only enabled if the compiler is told
  166. to compile 1.4 version source. Clearly with the 1.1 compatibility requirement,
  167. Ant tasks can not use this keyword. They also need to move away from
  168. using the JUnit <tt>assert()</tt> method and call <tt>assertTrue()</tt>
  169. instead.
  170. <h2>Refactor</h2>
  171. If the changes made to a task are making it too unwieldy, split it up
  172. into a cleaner design, refactor the code and submit not just feature
  173. creep but cleaner tasks. A common design pattern which tends to occur in
  174. the ant process is the adoption of the adapter pattern, in which a base
  175. class (say Javac or Rmi) starts off simple, then gets convoluted with
  176. support for multiple back ends -javac, jikes, jvc. A refactoring to
  177. split the programmable front end from the classes which provide the back
  178. end cleans up the design and makes it much easier to add new back ends.
  179. But to carry this off one needs to keep the interface and behaviour of
  180. the front end identical, and to be sure that no subclasses have been
  181. accessing data members directly -because these data members may not
  182. exist in the refactored design. Which is why having private data members
  183. is so important.
  184. <h2>Test</h2>
  185. Look in <tt>ant/src/testcases</tt> and you will find JUnit tests for the
  186. shipping ant tasks, to see how it is done and what is expected of a new
  187. task. Most of them are rudimentary, and no doubt you could do better for
  188. your task -feel free to do so!
  189. <p>
  190. A well written set of test cases will break the Ant task while it is in
  191. development, until the code is actually complete. And every bug which
  192. surfaces later should have a test case added to demonstrate the problem,
  193. and to fix it.
  194. <p>
  195. The test cases are a great way of testing your task during development.
  196. A simple call to 'build run-test' in the ant source tree will run all ant
  197. tests, to verify that your changes don't break anything.
  198. To test a single task, use the one shot <code>ant run-single-test
  199. -Dtestcase=${testname}</code> where <code>${testname}</code> is the name of your test class.
  200. <p>
  201. The test cases are also used by the committers to verify that changes
  202. and patches do what they say. If you've got test cases it increases your
  203. credibility significantly. To be precise, we hate submissions without
  204. test cases, as it means we have to write them ourselves. This is
  205. something that only gets done if we need the task or it is perceived as
  206. utterly essential to many users.
  207. <p>
  208. Remember also that Ant 1.x is designed to compile and run on Java1.1, so
  209. you should test on Java 1.1 as well as any later version which you use.
  210. You can download an old SDK from Sun for this purpose.
  211. <p>
  212. Finally, run a full <code>build test</code> before and after you start
  213. developing your project, to make sure you havent broken anything else by
  214. accident.
  215. <h2>Document</h2>
  216. Without documentation, the task can't be used. So remember to provide a
  217. succint and clear html (soon, xml) page describing the task in a similar
  218. style to that of existing tasks. It should include a list of attributes
  219. and elements, and at least one working example of the task. Many users
  220. cut and paste the examples into their build files as a starting point,
  221. so make the examples practical and test them too.
  222. <p>
  223. You can use the xdocs stuff in proposal/xdocs to autogenerate your
  224. documentation page from the javadocs of the source; this makes life
  225. easier and will make the transition to a full xdoclet generated
  226. documentation build process trivial.
  227. <h2>Licensing and Copyright</h2>
  228. Any code submitted to the Apache project must be compatible with the
  229. Apache Software License, and the act of submission must be viewed as an
  230. implicit transfer of ownership of the submitted code to the Apache
  231. Software Foundation.
  232. <p>
  233. This is important.
  234. <p>
  235. The fairly laissez-faire license of Apache is not compabitible with
  236. either the GPL or the Lesser GPL of the Free Software Foundation -the
  237. Gnu project. These licenses have stricter terms, "copyleft", which are
  238. not in the Apache Software Foundation license.
  239. This permits people and organisations to build
  240. commercial and closed source applications atop the Apache libraries and
  241. source -but not use the Apache, Ant or Jakarta Project names without
  242. permission.
  243. <p>
  244. Because the Gnu GPL license immediately extends to cover any larger
  245. application (or library, in the case of GLPL) into which it is
  246. incorporated, the Ant team can not incorporate any task based upon GPL
  247. or LGPL source into the Ant codebase. You are free to submit it, but it
  248. will be politely and firmly rejected.
  249. <p>
  250. Once ant-2 adds better dynamic task incorporation, it may be possible to
  251. provide a framework for indirectly supporting [L]GPL code, but still no tasks
  252. directly subject to the Gnu licenses can be included in the Ant
  253. CVS tree.
  254. <p>
  255. If you link to a GPL or LGPL library, by <code>import</code> or
  256. reflection, your task must be licensed under the same terms. So tasks
  257. linking to (L)GPL code can't go into the Apache managed codebase.
  258. Tasks calling such code can use the 'exec' or 'java' tasks to run the
  259. programs, as you are just executing them at this point, not linking to
  260. them.
  261. <p>
  262. Even if we cannot include your task into the Apache codebase, we can
  263. still point to where you host it -just submit a diff to
  264. xdocs/external.html pointing to your task.
  265. If your task links directly to proprietary code, we have a differnt
  266. problem: it is really hard to build the tasks. Please use reflection.
  267. <h3>Dont re-invent the wheel</h3>
  268. We've all done it: written and submitted a task only to discover it
  269. was already implemented in a small corner of another task, or it has
  270. been submitted by someone else and not committed. You can avoid this
  271. by being aware of what is in the latest CVS tree -keep getting the daily
  272. source updates, look at manual changes and subscribe to the dev
  273. mailing list.
  274. <p>
  275. If you are thinking of writing a task, posting a note on your thoughts
  276. to the list can be informative -you well get other peoples insight and
  277. maybe some half written task to do the basics, all without writing a
  278. line of code.
  279. <h2>Submitting to Ant</h2>
  280. The process for submitting an Ant task is documented on the
  281. <a href="http://jakarta.apache.org/site/guidelines.html">
  282. jakarta web site</a>.
  283. The basic mechanism is to mail it to the dev mailing list.
  284. It helps to be on this list, as you will see other submissions, and
  285. any debate about your own submission.
  286. <p>
  287. You may create your patch file using either of the following approaches.
  288. The committers recommend you to take the first approach.
  289. <p>
  290. <ul>
  291. <li> <h3>Approach 1 - The Ant Way</h3>
  292. <p>
  293. Use Ant to generate a patch file to Ant:
  294. <pre class="code">
  295. ant -f patch.xml
  296. </pre>
  297. This will create a file named patch.tar.gz that will contain a unified
  298. diff of files that have been modified and also include files that have
  299. been added. Review the file for completeness and correctness. This approach
  300. is recommended because it standardizes the way in which patch files are
  301. constructed. It also eliminates the chance of you missing to submit new files
  302. that constitute part of the patch.
  303. <p>
  304. <li><h3>Approach 2 - The Manual Way</h3>
  305. <p>
  306. Patches to existing files should be generated with
  307. <code>cvs diff -u filename</code>
  308. and save the output to a file. If you want to get
  309. the changes made to multiple files in a directory , just use <code>cvs
  310. diff -u</code>. Then, Tar and GZip the patch file as well as any new files
  311. that you have added.
  312. </ul>
  313. <p>
  314. The patches should be sent as an attachment to a message titled [PATCH]
  315. and distinctive one-line summary in the subject of the patch. The
  316. filename/task and the change usually suffices. It's important to include
  317. the changes as an attachment, as too many mailers reformat the text
  318. pasted in, which breaks the patch.
  319. <p>
  320. Then you wait for one of the committers to commit the patch, if it is
  321. felt appropriate to do so. Bug fixes go in quickly, other changes
  322. often spark a bit of discussion before a (perhaps revised) commit is
  323. made.
  324. <p>
  325. New submissions should be proceeded with [SUBMIT]. The mailer-daemon
  326. will reject any messages over 100KB, so any large update should be
  327. zipped up. If your submission is bigger than that, why not break it up
  328. into separate tasks.
  329. <p>
  330. We also like submissions to be added to
  331. <a href="http://nagoya.apache.org/bugzilla/">bugzilla</a>, so that they
  332. dont get lost. Please submit them by first filing the report with a
  333. meaningful name, then adding files as attachments. Use CVS diff files
  334. please!
  335. <p>
  336. If you hear nothing after a couple of weeks, remind the mailing list.
  337. Sometimes really good submissions get lost in the noise of other issues.
  338. This is particularly the case just prior to a new point release of
  339. the product. At that time anything other than bug fixes will tend
  340. to be neglected.
  341. <h2>Checklists</h2>
  342. These are the things you should verify before submitting patches and new
  343. tasks. Things don't have to be perfect, it may take a couple of
  344. iterations before a patch or submission is committed, and these items
  345. can be addressed in the process. But by the time the code is committed,
  346. everything including the documentation and some test cases will have
  347. been done, so by getting them out the way up front can save time.
  348. The committers look more favourably on patches and submissions with test
  349. cases, while documentation helps sell the reason for a task.
  350. <h3>Checklist before submitting a patch</h3>
  351. <ul>
  352. <li>Added code complies with style guidelines
  353. <li>Code compiles and runs on Java1.1
  354. <li>New member variables are private, and provide public accessor methods
  355. if access is actually needed.
  356. <li>Existing test cases succeed.
  357. <li>New test cases written and succeed.
  358. <li>Documentation page extended as appropriate.
  359. <li>Example task declarations in the documentation tested.
  360. <li>Diff files generated using cvs diff -u
  361. <li>Message to dev contains [PATCH], task name and patch reason in
  362. subject.
  363. <li>Message body contains a rationale for the patch.
  364. <li>Message attachment contains the patch file(s).
  365. </ul>
  366. <h3>Checklist before submitting a new task</h3>
  367. <ul>
  368. <li>Java file begins with Apache copyright and license statement.
  369. <li>Task does not depend on GPL or LGPL code.
  370. <li>Source code complies with style guidelines
  371. <li>Code compiles and runs on Java1.1
  372. <li>Member variables are private, and provide public accessor methods
  373. if access is actually needed.
  374. <li><i>Maybe</i> Task has failonerror attribute to control failure behaviour
  375. <li>New test cases written and succeed
  376. <li>Documentation page written
  377. <li>Example task declarations in the documentation tested.
  378. <li>Patch files generated using cvs diff -u
  379. <li>patch files include a patch to defaults.properties to register the
  380. tasks
  381. <li>patch files include a patch to coretasklist.html or
  382. optionaltasklist.html to link to the new task page
  383. <li>Message to dev contains [SUBMIT] and task name in subject
  384. <li>Message body contains a rationale for the task
  385. <li>Message attachments contain the required files -source, documentation,
  386. test and patches zipped up to escape the HTML filter.
  387. </ul>
  388. <hr>
  389. <p align="center">Copyright &copy; 2001-2003 Apache Software Foundation. All rights
  390. Reserved.</p>
  391. </body></html>