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

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