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