Debian Codegraph @

manual/Types/antlib.html

raw
1 <!DOCTYPE html>
2 <!--
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
9
10 https://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 -->
18 <html lang="en">
19
20 <head>
21 <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
22 <title>AntLib</title>
23 </head>
24
25 <body>
26 <h2 id="antlib">Antlib</h2>
27
28 <h3>Description</h3>
29 <p>
30 An antlib file is an xml file with a root element of <code>antlib</code>. Antlib's elements
31 are Apache Ant definition tasks&mdash;like <a href="../Tasks/taskdef.html">Taskdef</a> or any
32 Ant task that
33 extends <code class="code">org.apache.tools.ant.taskdefs.AntlibDefinition</code>.
34 </p>
35 <p>
36 The current set of declarations bundled with Ant that do this are:
37 </p>
38 <ol>
39 <li><a href="../Tasks/typedef.html">Typedef</a></li>
40 <li><a href="../Tasks/taskdef.html">Taskdef</a></li>
41 <li><a href="../Tasks/macrodef.html">Macrodef</a></li>
42 <li><a href="../Tasks/presetdef.html">Presetdef</a></li>
43 <li><a href="../Tasks/scriptdef.html">Scriptdef</a></li>
44 </ol>
45 <p>
46 A group of tasks and types may be defined together in an antlib file. For example the
47 file <samp>sample.xml</samp> contains the following:
48 </p>
49 <pre>
50 &lt;?xml version="1.0"?&gt;
51 &lt;antlib&gt;
52 &lt;typedef name="if" classname="org.acme.ant.If"/&gt;
53 &lt;typedef name="scriptpathmapper"
54 classname="org.acme.ant.ScriptPathMapper"
55 onerror="ignore"/&gt;
56 &lt;macrodef name="print"&gt;
57 &lt;attribute name="file"/&gt;
58 &lt;sequential&gt;
59 &lt;concat taskname="print"&gt;
60 &lt;fileset dir="." includes="@{file}"/&gt;
61 &lt;/concat&gt;
62 &lt;/sequential&gt;
63 &lt;/macrodef&gt;
64 &lt;/antlib&gt;</pre>
65 <p>
66 It defines two types or tasks, <code>if</code> and <code>scriptpathmapper</code>. This
67 antlib file may be used in a build script as follows:
68 </p>
69 <pre>&lt;typedef file="sample.xml"/&gt;</pre>
70 <p>
71 The other attributes of <code>&lt;typedef&gt;</code> may be used as well. For example,
72 assuming that the <samp>sample.xml</samp> is in a jar file <samp>sample.jar</samp> also
73 containing the classes, the following build fragment will define the <code>if</code>
74 and <code>scriptpathmapper</code> tasks/types and place them in the namespace
75 uri <code>samples:/acme.org</code>.
76 </p>
77 <pre>
78 &lt;typedef resource="org/acme/ant/sample.xml"
79 uri="samples:/acme.org"/&gt;</pre>
80 <p>
81 The definitions may then be used as follows:
82 </p>
83 <pre>
84 &lt;sample:if valuetrue="${props}" xmlns:sample="samples:/acme.org"&gt;
85 &lt;sample:scriptpathmapper language="beanshell"&gt;
86 some bean shell
87 &lt;/sample:scriptpathmapper&gt;
88 &lt;/sample:if&gt;</pre>
89
90 <h3 id="antlibnamespace">Antlib namespace</h3>
91 <p>
92 The name space URIs with the pattern <code>antlib:<i>java.package</i></code> are given
93 special treatment.
94 </p>
95 <p>
96 When Ant encounters a element with a namespace URI with this pattern, it will check to see
97 if there is a resource of the name <samp>antlib.xml</samp> in the package directory in the
98 default classpath.
99 </p>
100 <p>
101 For example, assuming that the file <samp>antcontrib.jar</samp> has been placed in the
102 directory <samp>${ant.home}/lib</samp> and it contains the
103 resource <samp>net/sf/antcontrib/antlib.xml</samp> which has all antcontrib's definitions
104 defined, the following build file will automatically load the antcontrib definitions at
105 location <code>HERE</code>:
106 </p>
107 <pre>
108 &lt;project default="deletetest" xmlns:antcontrib="antlib:net.sf.antcontrib"&gt;
109 &lt;macrodef name="showdir"&gt;
110 &lt;attribute name="dir"/&gt;
111 &lt;sequential&gt;
112 &lt;antcontrib:shellscript shell="bash"&gt; &lt;!-- HERE --&gt;
113 ls -Rl @{dir}
114 &lt;/antcontrib:shellscript&gt;
115 &lt;/sequential&gt;
116 &lt;/macrodef&gt;
117
118 &lt;target name="deletetest"&gt;
119 &lt;delete dir="a" quiet="yes"/&gt;
120 &lt;mkdir dir="a/b"/&gt;
121 &lt;touch file="a/a.txt"/&gt;
122 &lt;touch file="a/b/b.txt"/&gt;
123 &lt;delete&gt;
124 &lt;fileset dir="a"/&gt;
125 &lt;/delete&gt;
126 &lt;showdir dir="a"/&gt;
127 &lt;/target&gt;
128 &lt;/project&gt;</pre>
129 <p>
130 The requirement that the resource is in the default classpath may be removed in future
131 versions of Ant.
132 </p>
133 <h3 id="loadFromInside">Load antlib from inside of the buildfile</h3>
134 <p>
135 If you want to separate the antlib from your local Ant installation, e.g. because you want
136 to hold that jar in your project's SCM system, you have to specify a classpath, so that
137 Ant could find that jar. The best solution is loading the antlib
138 with <code>&lt;taskdef&gt;</code>.
139 </p>
140 <pre>
141 &lt;project xmlns:<span style="color:green">antcontrib</span>="<span style="color:red">antlib:net.sf.antcontrib</span>"&gt;
142 &lt;taskdef uri="<span style="color:red">antlib:net.sf.antcontrib</span>"
143 resource="net/sf/antcontrib/antlib.xml"
144 classpath="path/to/ant-contrib.jar"/&gt;
145
146 &lt;target name="iterate"&gt;
147 &lt;<span style="color:green">antcontrib</span>:for param="file"&gt;
148 &lt;fileset dir="."/&gt;
149 &lt;sequential&gt;
150 &lt;echo message="- @{file}"/&gt;
151 &lt;/sequential&gt;
152 &lt;/antcontrib:for&gt;
153 &lt;/target&gt;
154 &lt;/project&gt;</pre>
155 <h3 id="currentnamespace">Current namespace</h3>
156 <p>
157 Definitions defined in antlibs may be used in antlibs. However, the namespace that
158 definitions are placed in are dependent on the <code>&lt;typedef&gt;</code> that uses the
159 antlib. To deal with this problem, the definitions are placed in the namespace
160 URI <code>ant:current</code> for the duration of the antlib execution. For example, the
161 following antlib defines the task <code>&lt;if&gt;</code>, the
162 type <code>&lt;isallowed&gt;</code> and a macro <code>&lt;ifallowed&gt;</code> that makes
163 use of the task and type:
164 </p>
165 <pre>
166 &lt;antlib xmlns:current="ant:current"&gt;
167 &lt;taskdef name="if" classname="org.acme.ant.If"/&gt;
168 &lt;typedef name="isallowed" classname="org.acme.ant.Isallowed"/&gt;
169 &lt;macrodef name="ifallowed"&gt;
170 &lt;attribute name="action"/&gt;
171 &lt;element name="do"/&gt;
172 &lt;sequential&gt;
173 &lt;current:if&gt;
174 &lt;current:isallowed test="@{action}"/&gt;
175 &lt;current:then&gt;
176 &lt;do/&gt;
177 &lt;/current:then&gt;
178 &lt;/current:if&gt;
179 &lt;/sequential&gt;
180 &lt;/macrodef&gt;
181 &lt;/antlib&gt;</pre>
182 <h3>Other examples and comments</h3>
183 <p>
184 Antlibs may make use of other antlibs.
185 </p>
186 <p>
187 As the names defined in the antlib are in the namespace URI as specified by the
188 calling <code>&lt;typedef&gt;</code> or by automatic element resolution, one may reuse
189 names from core Ant types and tasks, provided the caller uses a namespace URI. For
190 example, the following antlib may be used to define defaults for various tasks:
191 </p>
192 <pre>
193 &lt;antlib xmlns:antcontrib="antlib:net.sf.antcontrib"&gt;
194 &lt;presetdef name="javac"&gt;
195 &lt;javac deprecation="${deprecation}"
196 debug="${debug}"/&gt;
197 &lt;/presetdef&gt;
198 &lt;presetdef name="delete"&gt;
199 &lt;delete quiet="yes"/&gt;
200 &lt;/presetdef&gt;
201 &lt;presetdef name="shellscript"&gt;
202 &lt;antcontrib:shellscript shell="bash"/&gt;
203 &lt;/presetdef&gt;
204 &lt;/antlib&gt;</pre>
205 <p>
206 This may be used as follows:
207 </p>
208 <pre>
209 &lt;project xmlns:local="localpresets"&gt;
210 &lt;typedef file="localpresets.xml" uri="localpresets"/&gt;
211 &lt;local:shellscript&gt;
212 echo "hello world"
213 &lt;/local:shellscript&gt;
214 &lt;/project&gt;</pre>
215
216 </body>
217 </html>