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.4 KiB

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