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