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

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