source: release-kits/lirk3/bin/ant-installer/web/manual/manual/antexternal.html@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 5.1 KB
Line 
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
31administrators. But what about programmers? Can the functionality
32provided by Ant tasks be used in java programs?</p>
33
34<p>Yes, and its quite easy. Before getting into the details, however,
35we 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>
43Ant tasks are very robust. They have been banged on by many people.
44Ant tasks have been used in many different contexts, and have
45therefore been instrumented to take care of a great many boundary
46conditions and potentially obscure errors.
47</td>
48</tr>
49<tr>
50<td><b>Cross Platform</b></td>
51<td>
52Ant tasks are cross platform. They have been tested on all of the
53volume platforms, and several rather unusual ones (Netware and OS/390, to
54name a few).
55</td>
56</tr>
57<tr>
58<td><b>Community Support</b></td>
59<td>
60Using Ant tasks means you have less of your own code to support. Ant
61code 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>
72Obviously, 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
74remove the unnecessary classes, but you will still probably require a
75chunk of the Ant core.
76</td>
77</tr>
78<tr>
79<td><b>Loss of Flexibility</b></td>
80<td>
81At some point, if you find yourself having to modify the Ant code, it
82probably makes more sense to "roll your own." Of course, you can
83still steal some code snippets and good ideas. This is the beauty of
84open 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
93into a certain directory. Of course you could write your own routine
94to 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
97XSLT Transformation. XSLT Transformers can be extended by plugging in
98static methods in java. I therefore need a function something like
99this:</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>
112The Ant task to perform this function is
113<code>org.apache.tools.ant.taskdefs.Expand</code>. All we have to do
114is create a dummy Ant <code>Project</code> and <code>Target</code>,
115set the <code>Task</code> parameters that would normally be set in a
116buildfile, and call <code>execute()</code>.</p>
117
118<p>First, let's make sure we have the proper includes:</p>
119
120<pre>
121import org.apache.tools.ant.Project;
122import org.apache.tools.ant.Target;
123import org.apache.tools.ant.taskdefs.Expand;
124import java.io.File;
125</pre>
126
127<p>The function call is actually quite simple:</p>
128
129<pre>
130static 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
148handling code and you may not want to use a local inner class.
149However, the point of the example is to show how an Ant task can be
150called 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
154order to set up a dummy Project and Target?</i> The answer is: you
155don't. Ultimately, you have to be willing to get your feet wet and
156read the source code. The above example is merely designed to whet
157your appetite and get you started. Go for it!</p>
158
159
160</html>
Note: See TracBrowser for help on using the repository browser.