source: other-projects/trunk/realistic-books/packages/AntInstaller/web/manual1.6.2/manual/antexternal.html@ 19253

Last change on this file since 19253 was 19253, checked in by davidb, 15 years ago

Establishing a source code repository for Veronica's Realistic Book's software

File size: 4.4 KB
Line 
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
15administrators. But what about programmers? Can the functionality
16provided by Ant tasks be used in java programs?</p>
17
18<p>Yes, and its quite easy. Before getting into the details, however,
19we should mention the pros and cons of this approach:
20
21<h3>Pros</h3>
22
23<table border="1" cellpadding="2" cellspacing="0">
24<tr>
25<td><b>Robust</b></td>
26<td>
27Ant tasks are very robust. They have been banged on by many people.
28Ant tasks have been used in many different contexts, and have
29therefore been instrumented to take care of a great many boundary
30conditions and potentially obscure errors.
31</td>
32</tr>
33<tr>
34<td><b>Cross Platform</b></td>
35<td>
36Ant tasks are cross platform. They have been tested on all of the
37volume platforms, and several rather unusual ones (Netware and OS/390, to
38name a few).
39</td>
40</tr>
41<tr>
42<td><b>Community Support</b></td>
43<td>
44Using Ant tasks means you have less of your own code to support. Ant
45code is supported by the entire Apache Ant community.
46</td>
47</tr>
48</table>
49
50<h3>Cons</h3>
51
52<table border="1" cellpadding="2" cellspacing="0">
53<tr>
54<td><b>Dependency on Ant Libraries</b></td>
55<td>
56Obviously, 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
58remove the unnecessary classes, but you will still probably require a
59chunk of the Ant core.
60</td>
61</tr>
62<tr>
63<td><b>Loss of Flexibility</b></td>
64<td>
65At some point, if you find yourself having to modify the Ant code, it
66probably makes more sense to "roll your own." Of course, you can
67still steal some code snippets and good ideas. This is the beauty of
68open 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
77into a certain directory. Of course you could write your own routine
78to 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
81XSLT Transformation. XSLT Transformers can be extended by plugging in
82static methods in java. I therefore need a function something like
83this:</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>
96The Ant task to perform this function is
97<code>org.apache.tools.ant.taskdefs.Expand</code>. All we have to do
98is create a dummy Ant <code>Project</code> and <code>Target</code>,
99set the <code>Task</code> parameters that would normally be set in a
100buildfile, and call <code>execute()</code>.</p>
101
102<p>First, let's make sure we have the proper includes:</p>
103
104<pre>
105import org.apache.tools.ant.Project;
106import org.apache.tools.ant.Target;
107import org.apache.tools.ant.taskdefs.Expand;
108import java.io.File;
109</pre>
110
111<p>The function call is actually quite simple:</p>
112
113<pre>
114static 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
132handling code and you may not want to use a local inner class.
133However, the point of the example is to show how an Ant task can be
134called 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
138order to set up a dummy Project and Target?</i> The answer is: you
139don't. Ultimately, you have to be willing to get your feet wet and
140read the source code. The above example is merely designed to whet
141your appetite and get you started. Go for it!</p>
142
143<hr>
144<p align="center">Copyright &copy; 2002-2004 The Apache Software Foundation. All rights
145Reserved.</p>
146</html>
Note: See TracBrowser for help on using the repository browser.