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

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