source: release-kits/lirk3/bin/ant-installer/web/manual1.7.0/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: 8.9 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<html>
18
19 <head>
20 <meta http-equiv="Content-Language" content="en-us"></meta>
21 <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
22<title>AntLib</title>
23 </head>
24
25 <body>
26 <h2><a name="antlib">Antlib</a></h2>
27
28
29 <h3>Description</h3>
30 <p>
31 An antlib file is an xml file with a root element of "antlib".
32 Antlib's elements are ant definition tasks - like
33
34 and <a href="../CoreTasks/taskdef.html">Taskdef</a>,
35 or any ant task that extends
36 <code>org.apache.tools.ant.taskdefs.AntlibDefinition</code>.
37 </p>
38 <p>
39 The current set of declarations bundled with Ant that do this are:
40 </p>
41 <ol>
42 <li><a href="../CoreTasks/typedef.html">Typedef</a>
43 </li>
44 <li><a href="../CoreTasks/taskdef.html">Taskdef</a>
45 </li>
46 <li><a href="../CoreTasks/macrodef.html">Macrodef</a>
47 </li>
48 <li><a href="../CoreTasks/presetdef.html">Presetdef</a>
49 </li>
50 <li><a href="../OptionalTasks/scriptdef.html">Scriptdef</a>
51 </li>
52 </ol>
53 <p>
54 A group of tasks and types may be defined together in an antlib
55 file. For example the file <i>sample.xml</i> contains the following:
56 </p>
57 <blockquote>
58 <pre>
59&lt;?xml version="1.0"?&gt;
60&lt;antlib&gt;
61 &lt;typedef name="if" classname="org.acme.ant.If"/&gt;
62 &lt;typedef name="scriptpathmapper"
63 classname="org.acme.ant.ScriptPathMapper"
64 onerror="ignore"/&gt;
65 &lt;macrodef name="print"&gt;
66 &lt;attribute name="file"/&gt;
67 &lt;sequential&gt;
68 &lt;concat taskname="print"&gt;
69 &lt;fileset dir="." includes="@{file}"/&gt;
70 &lt;/concat&gt;
71 &lt;/sequential&gt;
72 &lt;/macrodef&gt;
73&lt;/antlib&gt;
74 </pre>
75 </blockquote>
76 <p>
77 It defines two types or tasks, <i>if</i> and <i>scriptpathmapper</i>.
78 This antlib file may be used in a build script as follows:
79 </p>
80 <blockquote>
81 <pre>
82&lt;typedef file="sample.xml"/&gt;
83 </pre>
84 </blockquote>
85 <p>
86 The other attributes of <code>&lt;typedef&gt;</code> may be used as well.
87 For example, assuming that the <i>sample.xml</i> is in a jar
88 file <i>sample.jar</i> also containing the classes, the
89 following build fragment will define the <i>if</i> and <i>scriptpathmapper</i>
90 tasks/types and place them in the namespace uri <i>samples:/acme.org</i>.
91 </p>
92 <blockquote>
93 <pre>
94&lt;typedef resource="org/acme/ant/sample.xml"
95 uri="samples:/acme.org"/&gt;
96 </pre>
97 </blockquote>
98 <p>
99 The definitions may then be used as follows:
100 </p>
101 <blockquote>
102 <pre>
103&lt;sample:if valuetrue="${props}" xmlns:sample="samples:/acme.org"&gt;
104 &lt;sample:scriptpathmapper language="beanshell"&gt;
105 some bean shell
106 &lt;/sample:scriptpathmapper&gt;
107&lt;/sample:if&gt;
108 </pre>
109 </blockquote>
110
111
112 <h3><a name="antlibnamespace">Antlib namespace</a></h3>
113 <p>
114 The name space URIs with the pattern <b>antlib:<i>java package</i></b>
115 are given special treatment.
116 </p>
117 <p>
118 When ant encounters a element with a namespace URI with this pattern, it
119 will check to see if there is a resource of the name <i>antlib.xml</i> in
120 the package directory in the default classpath.
121 </p>
122 <p>
123 For example, assuming that the file <i>antcontrib.jar</i> has been placed
124 in the directory <i>${ant.home}/lib</i> and it contains the resource
125 <i>net/sf/antcontrib/antlib.xml</i> which has all antcontrib's definitions
126 defined, the following build file will automatically load the antcontrib
127 definitions at location <i>HERE</i>:
128 </p>
129 <blockquote>
130 <pre>
131&lt;project default="deletetest" xmlns:antcontrib="antlib:net.sf.antcontrib"&gt;
132 &lt;macrodef name="showdir"&gt;
133 &lt;attribute name="dir"/&gt;
134 &lt;sequential&gt;
135 &lt;antcontrib:shellscript shell="bash"&gt; &lt;!-- HERE --&gt;
136 ls -Rl @{dir}
137 &lt;/antcontrib:shellscript&gt;
138 &lt;/sequential&gt;
139 &lt;/macrodef&gt;
140
141 &lt;target name="deletetest"&gt;
142 &lt;delete dir="a" quiet="yes"/&gt;
143 &lt;mkdir dir="a/b"/&gt;
144 &lt;touch file="a/a.txt"/&gt;
145 &lt;touch file="a/b/b.txt"/&gt;
146 &lt;delete&gt;
147 &lt;fileset dir="a"/&gt;
148 &lt;/delete&gt;
149 &lt;showdir dir="a"/&gt;
150 &lt;/target&gt;
151&lt;/project&gt;
152 </pre>
153 </blockquote>
154 <p>
155 The requirement that the resource is in the default classpath
156 may be removed in future versions of Ant.</p>
157 </p>
158
159
160 <h3><a name="loadFromInside">Load antlib from inside of the buildfile</a></h3>
161 <p>
162 If you want to seperate the antlib from your local Ant installation, e.g. because you
163 want to hold that jar in your projects SCM system, you have to specify a classpath, so
164 that Ant could find that jar. The best solution is loading the antlib with <tt>&lt;taskdef&gt;</tt>.
165 </p>
166 <blockquote>
167 <pre>
168&lt;project xmlns:<font color="green">antcontrib</font>="<font color="red">antlib:net.sf.antcontrib</font>"&gt;
169 &lt;taskdef uri="<font color="red">antlib:net.sf.antcontrib</font>"
170 resource="net/sf/antcontrib/antlib.xml"
171 classpath="path/to/ant-contrib.jar"/&gt;
172
173 &lt;target name="iterate"&gt;
174 &lt;<font color="green">antcontrib</font>:for param="file"&gt;
175 &lt;fileset dir="."/&gt;
176 &lt;sequential&gt;
177 &lt;echo message="- @{file}"/&gt;
178 &lt;/sequential&gt;
179 &lt;/antcontrib:for&gt;
180 &lt;/target&gt;
181&lt;/project&gt;
182 </pre>
183 </blockquote>
184
185
186
187
188 <h3><a name="currentnamespace">Current namespace</a></h3>
189 <p>
190 Definitions defined in antlibs may be used in antlibs. However
191 the namespace that definitions are placed in are dependent on
192 the <code>&lt;typedef&gt;</code> that uses the antlib. To deal with this
193 problem, the definitions are placed in the namepace URI <i>ant:current</i>
194 for the duration of the antlib execution.
195 For example the following antlib defines the task <code>&lt;if&gt;</code>, the
196 type <code>&lt;isallowed&gt;</code> and a macro
197 <code>&lt;ifallowed&gt;</code> that makes use of the task and type:
198 </p>
199 <blockquote>
200 <pre>
201&lt;antlib xmlns:current="ant:current"&gt;
202 &lt;taskdef name="if" classname="org.acme.ant.If"/&gt;
203 &lt;typedef name="isallowed" classname="org.acme.ant.Isallowed"/&gt;
204 &lt;macrodef name="ifallowed"&gt;
205 &lt;attribute name="action"/&gt;
206 &lt;element name="do"/&gt;
207 &lt;sequential&gt;
208 &lt;current:if&gt;
209 &lt;current:isallowed test="@{action}"/&gt;
210 &lt;current:then&gt;
211 &lt;do/&gt;
212 &lt;/current:then&gt;
213 &lt;/current:if&gt;
214 &lt;/sequential&gt;
215 &lt;/macrodef&gt;
216&lt;/antlib&gt;
217 </pre>
218 </blockquote>
219
220
221 <h3>Other examples and comments</h3>
222 <p>
223 Antlibs may make use of other antlibs.
224 </p>
225 <p>
226 As the names defined in the antlib are in the namespace uri as
227 specified by the calling <code>&lt;typedef&gt;</code> or by automatic element
228 resolution, one may reuse names from core ant types and tasks,
229 provided the caller uses a namespace uri. For example, the
230 following antlib may be used to define defaults for various
231 tasks:
232 </p>
233 <blockquote>
234 <pre>
235&lt;antlib xmlns:antcontrib="antlib:net.sf.antcontrib"&gt;
236 &lt;presetdef name="javac"&gt;
237 &lt;javac deprecation="${deprecation}"
238 debug="${debug}"/&gt;
239 &lt;/presetdef&gt;
240 &lt;presetdef name="delete"&gt;
241 &lt;delete quiet="yes"/&gt;
242 &lt;/presetdef&gt;
243 &lt;presetdef name="shellscript"&gt;
244 &lt;antcontrib:shellscript shell="bash"/&gt;
245 &lt;/presetdef&gt;
246&lt;/antlib&gt;
247 </pre>
248 </blockquote>
249 <p>
250 This may be used as follows:
251 </p>
252 <blockquote>
253 <pre>
254&lt;project xmlns:local="localpresets"&gt;
255 &lt;typedef file="localpresets.xml" uri="localpresets"/&gt;
256 &lt;local:shellscript&gt;
257 echo "hello world"
258 &lt;/local:shellscript&gt;
259&lt;/project&gt;
260 </pre>
261 </blockquote>
262
263
264
265</body>
266</html>
267
Note: See TracBrowser for help on using the repository browser.