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.

antexternal.html 4.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Language" content="en-us">
  5. <title>InputHandler</title>
  6. </head>
  7. <body>
  8. <h1>Using Ant Tasks Outside of Ant</h1>
  9. <h2>Rationale</h2>
  10. <p>Ant provides a rich set of tasks for buildfile creators and
  11. administrators. But what about programmers? Can the functionality
  12. provided by Ant tasks be used in java programs?</p>
  13. <p>Yes, and its quite easy. Before getting into the details, however,
  14. we should mention the pros and cons of this approach:
  15. <h3>Pros</h3>
  16. <table cellpadding="0" margin="0" border="1">
  17. <tr>
  18. <td><b>Robust</b></td>
  19. <td>
  20. Ant tasks are very robust. They have been banged on by many people.
  21. Ant tasks have been used in many different contexts, and have
  22. therefore been instrumented to take care of a great many boundary
  23. conditions and potentially obscure errors.
  24. </td>
  25. </tr>
  26. <tr>
  27. <td><b>Cross Platform</b></td>
  28. <td>
  29. Ant tasks are cross platform. They have been tested on all of the
  30. volume platforms, and several rather unusual ones (Netware and OS/390, to
  31. name a few).
  32. </td>
  33. </tr>
  34. <tr>
  35. <td><b>Community Support</b></td>
  36. <td>
  37. Using Ant tasks means you have less of your own code to support. Ant
  38. code is supported by the entire jakarta-ant community.
  39. </td>
  40. </tr>
  41. </table>
  42. <h3>Cons</h3>
  43. <table cellpadding="0" margin="0" border="1">
  44. <tr>
  45. <td><b>Dependency on Ant Libraries</b></td>
  46. <td>
  47. Obviously, if you use an Ant task in your code, you will have to add
  48. "ant.jar" to your path. Of course, you could use a code optimizer to
  49. remove the unnecessary classes, but you will still probably require a
  50. chunk of the Ant core.
  51. </td>
  52. </tr>
  53. <tr>
  54. <td><b>Loss of Flexibility</b></td>
  55. <td>
  56. At some point, if you find yourself having to modify the Ant code, it
  57. probably makes more sense to "roll your own." Of course, you can
  58. still steal some code snippets and good ideas. This is the beauty of
  59. open source!
  60. </td>
  61. </tr>
  62. </table>
  63. <h2>Example</h2>
  64. <p>Let's say you want to unzip a zip file programmatically from java
  65. into a certain directory. Of course you could write your own routine
  66. to do this, but why not use the Ant task that has already been written?</p>
  67. <p>In my example, I wanted to be able to unzip a file from within an
  68. XSLT Transformation. XSLT Transformers can be extended by plugging in
  69. static methods in java. I therefore need a function something like
  70. this:</p>
  71. <pre>
  72. /**
  73. * Unzip a zip file into a given directory.
  74. *
  75. * @param zipFilepath A pathname representing a local zip file
  76. * @param destinationDir where to unzip the archive to
  77. */
  78. static public void unzip(String zipFilepath, String destinationDir)
  79. </pre>
  80. <p>
  81. The Ant task to perform this function is
  82. <code>org.apache.tools.ant.taskdefs.Expand</code>. All we have to do
  83. is create a dummy Ant <code>Project</code> and <code>Target</code>,
  84. set the <code>Task</code> parameters that would normally be set in a
  85. buildfile, and call <code>execute()</code>.</p>
  86. <p>First, let's make sure we have the proper includes:</p>
  87. <pre>
  88. import org.apache.tools.ant.Project;
  89. import org.apache.tools.ant.Target;
  90. import org.apache.tools.ant.taskdefs.Expand;
  91. import java.io.File;
  92. </pre>
  93. <p>The function call is actually quite simple:</p>
  94. <pre>
  95. static public void unzip(String zipFilepath, String destinationDir) {
  96. final class Expander extends Expand {
  97. public Expander() {
  98. project = new Project();
  99. project.init();
  100. taskType = "unzip";
  101. taskName = "unzip";
  102. target = new Target();
  103. }
  104. }
  105. Expander expander = new Expander();
  106. expander.setSrc(new File(zipfile));
  107. expander.setDest(new File(destdir));
  108. expander.execute();
  109. </pre>
  110. <p>In actual practice, you will probably want to add your own error
  111. handling code and you may not want to use a local inner class.
  112. However, the point of the example is to show how an Ant task can be
  113. called programmatically in relatively few lines of code.</p>
  114. <p>The question you are probably asking yourself at this point is:
  115. <i>How would I know which classes and methods have to be called in
  116. order to set up a dummy Project and Target?</i> The answer is: you
  117. don't. Ultimately, you have to be willing to get your feet wet and
  118. read the source code. The above example is merely designed to whet
  119. your appetite and get you started. Go for it!</p>
  120. <hr>
  121. <p align="center">Copyright &copy; 2002 Apache Software Foundation. All rights
  122. Reserved.</p>
  123. </html>