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 | <?xml version="1.0"?>
|
---|
27 | <antlib>
|
---|
28 | <typedef name="if" classname="org.acme.ant.If"/>
|
---|
29 | <typedef name="scriptpathmapper"
|
---|
30 | classname="org.acme.ant.ScriptPathMapper"
|
---|
31 | onerror="ignore"/>
|
---|
32 | </antlib>
|
---|
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 | <typedef file="sample.xml"/>
|
---|
42 | </pre>
|
---|
43 | </blockquote>
|
---|
44 | <p>
|
---|
45 | The other attributes of <typedef> 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 | <typedef resource="org/acme/ant/sample.xml"
|
---|
54 | uri="samples:/acme.org"/>
|
---|
55 | </pre>
|
---|
56 | </blockquote>
|
---|
57 | <p>
|
---|
58 | The definitions may then be used as follows:
|
---|
59 | </p>
|
---|
60 | <blockquote>
|
---|
61 | <pre>
|
---|
62 | <sample:if valuetrue="${props}" xmlns:sample="samples:/acme.org">
|
---|
63 | <sample:scriptpathmapper language="beanshell">
|
---|
64 | some bean shell
|
---|
65 | </sample:scriptpathmapper>
|
---|
66 | </sample:if>
|
---|
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 | <project default="deletetest" xmlns:antcontrib="antlib:net.sf.antcontrib">
|
---|
89 | <macrodef name="showdir">
|
---|
90 | <attribute name="dir"/>
|
---|
91 | <sequential>
|
---|
92 | <antcontrib:shellscript shell="bash"> <!-- HERE -->
|
---|
93 | ls -Rl @{dir}
|
---|
94 | </antcontrib:shellscript>
|
---|
95 | </sequential>
|
---|
96 | </macrodef>
|
---|
97 |
|
---|
98 | <target name="deletetest">
|
---|
99 | <delete dir="a" quiet="yes"/>
|
---|
100 | <mkdir dir="a/b"/>
|
---|
101 | <touch file="a/a.txt"/>
|
---|
102 | <touch file="a/b/b.txt"/>
|
---|
103 | <delete>
|
---|
104 | <fileset dir="a"/>
|
---|
105 | </delete>
|
---|
106 | <showdir dir="a"/>
|
---|
107 | </target>
|
---|
108 | </project>
|
---|
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 <typedef> 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 <if>, the
|
---|
123 | type <isallowed> and a macro
|
---|
124 | <ifallowed> that makes use of the task and type:
|
---|
125 | </p>
|
---|
126 | <blockquote>
|
---|
127 | <pre>
|
---|
128 | <antlib xmlns:current="ant:current">
|
---|
129 | <taskdef name="if" classname="org.acme.ant.If"/>
|
---|
130 | <typedef name="isallowed" classname="org.acme.ant.Isallowed"/>
|
---|
131 | <macrodef name="ifallowed">
|
---|
132 | <attribute name="action"/>
|
---|
133 | <element name="do"/>
|
---|
134 | <sequential>
|
---|
135 | <current:if>
|
---|
136 | <current:isallowed test="@{action}"/>
|
---|
137 | <current:then>
|
---|
138 | <do/>
|
---|
139 | </current:then>
|
---|
140 | </current:if>
|
---|
141 | </sequential>
|
---|
142 | </macrodef>
|
---|
143 | </antlib>
|
---|
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 <typedef> 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 | <antlib xmlns:antcontrib="antlib:net.sf.antcontrib">
|
---|
161 | <presetdef name="javac">
|
---|
162 | <javac deprecation="${deprecation}"
|
---|
163 | debug="${debug}"/>
|
---|
164 | </presetdef>
|
---|
165 | <presetdef name="delete">
|
---|
166 | <delete quiet="yes"/>
|
---|
167 | </presetdef>
|
---|
168 | <presetdef name="shellscript">
|
---|
169 | <antcontrib:shellscript shell="bash"/>
|
---|
170 | </presetdef>
|
---|
171 | </antlib>
|
---|
172 | </pre>
|
---|
173 | </blockquote>
|
---|
174 | <p>
|
---|
175 | This may be used as follows:
|
---|
176 | </p>
|
---|
177 | <blockquote>
|
---|
178 | <pre>
|
---|
179 | <project xmlns:local="localpresets">
|
---|
180 | <typedef file="localpresets.xml" uri="localpresets"/>
|
---|
181 | <local:shellscript>
|
---|
182 | echo "hello world"
|
---|
183 | </local:shellscript>
|
---|
184 | </project>
|
---|
185 | </pre>
|
---|
186 | </blockquote>
|
---|
187 |
|
---|
188 | <hr>
|
---|
189 | <p align="center">Copyright © 2003-2004 The Apache Software
|
---|
190 | Foundation. All rights Reserved.</p>
|
---|
191 |
|
---|
192 | </body>
|
---|
193 | </html>
|
---|
194 |
|
---|