source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/docs/manual/CoreTypes/antlib.html@ 14982

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

initial import of LiRK3

File size: 6.4 KB
Line 
1<html>
2
3 <head>
4 <meta http-equiv="Content-Language" content="en-us"></meta>
5 <title>AntLib</title>
6<link rel="stylesheet" type="text/css" href="../stylesheets/antmanual.css">
7 </head>
8
9 <body>
10 <h2><a name="antlib">Antlib</a></h2>
11 <h3>Description</h3>
12 <p>
13 An antlib file is an xml file with a root element of "antlib".
14 Antlib's elements are ant definition tasks - like
15 <a href="../CoreTasks/typedef.html">Typedef</a>
16 and <a href="../CoreTasks/taskdef.html">Taskdef</a>,
17 or any ant task that extends
18 <code>org.apache.tools.ant.taskdefs.AntlibDefinition</code>.
19 </p>
20 <p>
21 A group of tasks and types may be defined together in an antlib
22 file. For example the file <i>sample.xml</i> contains the following:
23 </p>
24 <blockquote>
25 <pre>
26&lt;?xml version="1.0"?&gt;
27&lt;antlib&gt;
28 &lt;typedef name="if" classname="org.acme.ant.If"/&gt;
29 &lt;typedef name="scriptpathmapper"
30 classname="org.acme.ant.ScriptPathMapper"
31 onerror="ignore"/&gt;
32&lt;/antlib&gt;
33 </pre>
34 </blockquote>
35 <p>
36 It defines two types or tasks, <i>if</i> and <i>scriptpathmapper</i>.
37 This antlib file may be used in a build script as follows:
38 </p>
39 <blockquote>
40 <pre>
41&lt;typedef file="sample.xml"/&gt;
42 </pre>
43 </blockquote>
44 <p>
45 The other attributes of <code>&lt;typedef&gt;</code> may be used as well.
46 For example, assuming that the <i>sample.xml</i> is in a jar
47 file <i>sample.jar</i> also containing the classes, the
48 following build fragment will define the <i>if</i> and <i>scriptpathmapper</i>
49 tasks/types and place them in the namespace uri <i>samples:/acme.org</i>.
50 </p>
51 <blockquote>
52 <pre>
53&lt;typedef resource="org/acme/ant/sample.xml"
54 uri="samples:/acme.org"/&gt;
55 </pre>
56 </blockquote>
57 <p>
58 The definitions may then be used as follows:
59 </p>
60 <blockquote>
61 <pre>
62&lt;sample:if valuetrue="${props}" xmlns:sample="samples:/acme.org"&gt;
63 &lt;sample:scriptpathmapper language="beanshell"&gt;
64 some bean shell
65 &lt;/sample:scriptpathmapper&gt;
66&lt;/sample:if&gt;
67 </pre>
68 </blockquote>
69 <h3><a name="antlibnamespace">Antlib namespace</a></h3>
70 <p>
71 The name space URIs with the pattern <b>antlib:<i>java package</i></b>
72 are given special treatment.
73 </p>
74 <p>
75 When ant encounters a element with a namespace URI with this pattern, it
76 will check to see if there is a resource of the name <i>antlib.xml</i> in
77 the package directory in the default classpath.
78 </p>
79 <p>
80 For example, assuming that the file <i>antcontrib.jar</i> has been placed
81 in the directory <i>${ant.home}/lib</i> and it contains the resource
82 <i>net/sf/antcontrib/antlib.xml</i> which has all antcontrib's definitions
83 defined, the following build file will automatically load the antcontrib
84 definitions at location <i>HERE</i>:
85 </p>
86 <blockquote>
87 <pre>
88&lt;project default="deletetest" xmlns:antcontrib="antlib:net.sf.antcontrib"&gt;
89 &lt;macrodef name="showdir"&gt;
90 &lt;attribute name="dir"/&gt;
91 &lt;sequential&gt;
92 &lt;antcontrib:shellscript shell="bash"&gt; &lt;!-- HERE --&gt;
93 ls -Rl @{dir}
94 &lt;/antcontrib:shellscript&gt;
95 &lt;/sequential&gt;
96 &lt;/macrodef&gt;
97
98 &lt;target name="deletetest"&gt;
99 &lt;delete dir="a" quiet="yes"/&gt;
100 &lt;mkdir dir="a/b"/&gt;
101 &lt;touch file="a/a.txt"/&gt;
102 &lt;touch file="a/b/b.txt"/&gt;
103 &lt;delete&gt;
104 &lt;fileset dir="a"/&gt;
105 &lt;/delete&gt;
106 &lt;showdir dir="a"/&gt;
107 &lt;/target&gt;
108&lt;/project&gt;
109 </pre>
110 </blockquote>
111 <p>
112 The requirement that the resource is in the default classpath
113 may be removed in future versions of Ant.</p>
114 </p>
115 <h3><a name="currentnamespace">Current namespace</a></h3>
116 <p>
117 Definitions defined in antlibs may be used in antlibs. However
118 the namespace that definitions are placed in are dependent on
119 the <code>&lt;typedef&gt;</code> that uses the antlib. To deal with this
120 problem, the definitions are placed in the namepace URI <i>ant:current</i>
121 for the duration of the antlib execution.
122 For example the following antlib defines the task <code>&lt;if&gt;</code>, the
123 type <code>&lt;isallowed&gt;</code> and a macro
124 <code>&lt;ifallowed&gt;</code> that makes use of the task and type:
125 </p>
126 <blockquote>
127 <pre>
128&lt;antlib xmlns:current="ant:current"&gt;
129 &lt;taskdef name="if" classname="org.acme.ant.If"/&gt;
130 &lt;typedef name="isallowed" classname="org.acme.ant.Isallowed"/&gt;
131 &lt;macrodef name="ifallowed"&gt;
132 &lt;attribute name="action"/&gt;
133 &lt;element name="do"/&gt;
134 &lt;sequential&gt;
135 &lt;current:if&gt;
136 &lt;current:isallowed test="@{action}"/&gt;
137 &lt;current:then&gt;
138 &lt;do/&gt;
139 &lt;/current:then&gt;
140 &lt;/current:if&gt;
141 &lt;/sequential&gt;
142 &lt;/macrodef&gt;
143&lt;/antlib&gt;
144 </pre>
145 </blockquote>
146 <h3>Other examples and comments</h3>
147 <p>
148 Antlibs may make use of other antlibs.
149 </p>
150 <p>
151 As the names defined in the antlib are in the namespace uri as
152 specified by the calling <code>&lt;typedef&gt;</code> or by automatic element
153 resolution, one may reuse names from core ant types and tasks,
154 provided the caller uses a namespace uri. For example, the
155 following antlib may be used to define defaults for various
156 tasks:
157 </p>
158 <blockquote>
159 <pre>
160&lt;antlib xmlns:antcontrib="antlib:net.sf.antcontrib"&gt;
161 &lt;presetdef name="javac"&gt;
162 &lt;javac deprecation="${deprecation}"
163 debug="${debug}"/&gt;
164 &lt;/presetdef&gt;
165 &lt;presetdef name="delete"&gt;
166 &lt;delete quiet="yes"/&gt;
167 &lt;/presetdef&gt;
168 &lt;presetdef name="shellscript"&gt;
169 &lt;antcontrib:shellscript shell="bash"/&gt;
170 &lt;/presetdef&gt;
171&lt;/antlib&gt;
172 </pre>
173 </blockquote>
174 <p>
175 This may be used as follows:
176 </p>
177 <blockquote>
178 <pre>
179&lt;project xmlns:local="localpresets"&gt;
180 &lt;typedef file="localpresets.xml" uri="localpresets"/&gt;
181 &lt;local:shellscript&gt;
182 echo "hello world"
183 &lt;/local:shellscript&gt;
184&lt;/project&gt;
185 </pre>
186 </blockquote>
187
188<hr>
189<p align="center">Copyright &copy; 2003-2004 The Apache Software
190Foundation. All rights Reserved.</p>
191
192</body>
193</html>
194
Note: See TracBrowser for help on using the repository browser.