[19253] | 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 | <?xml version="1.0"?>
|
---|
| 60 | <antlib>
|
---|
| 61 | <typedef name="if" classname="org.acme.ant.If"/>
|
---|
| 62 | <typedef name="scriptpathmapper"
|
---|
| 63 | classname="org.acme.ant.ScriptPathMapper"
|
---|
| 64 | onerror="ignore"/>
|
---|
| 65 | <macrodef name="print">
|
---|
| 66 | <attribute name="file"/>
|
---|
| 67 | <sequential>
|
---|
| 68 | <concat taskname="print">
|
---|
| 69 | <fileset dir="." includes="@{file}"/>
|
---|
| 70 | </concat>
|
---|
| 71 | </sequential>
|
---|
| 72 | </macrodef>
|
---|
| 73 | </antlib>
|
---|
| 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 | <typedef file="sample.xml"/>
|
---|
| 83 | </pre>
|
---|
| 84 | </blockquote>
|
---|
| 85 | <p>
|
---|
| 86 | The other attributes of <code><typedef></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 | <typedef resource="org/acme/ant/sample.xml"
|
---|
| 95 | uri="samples:/acme.org"/>
|
---|
| 96 | </pre>
|
---|
| 97 | </blockquote>
|
---|
| 98 | <p>
|
---|
| 99 | The definitions may then be used as follows:
|
---|
| 100 | </p>
|
---|
| 101 | <blockquote>
|
---|
| 102 | <pre>
|
---|
| 103 | <sample:if valuetrue="${props}" xmlns:sample="samples:/acme.org">
|
---|
| 104 | <sample:scriptpathmapper language="beanshell">
|
---|
| 105 | some bean shell
|
---|
| 106 | </sample:scriptpathmapper>
|
---|
| 107 | </sample:if>
|
---|
| 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 | <project default="deletetest" xmlns:antcontrib="antlib:net.sf.antcontrib">
|
---|
| 132 | <macrodef name="showdir">
|
---|
| 133 | <attribute name="dir"/>
|
---|
| 134 | <sequential>
|
---|
| 135 | <antcontrib:shellscript shell="bash"> <!-- HERE -->
|
---|
| 136 | ls -Rl @{dir}
|
---|
| 137 | </antcontrib:shellscript>
|
---|
| 138 | </sequential>
|
---|
| 139 | </macrodef>
|
---|
| 140 |
|
---|
| 141 | <target name="deletetest">
|
---|
| 142 | <delete dir="a" quiet="yes"/>
|
---|
| 143 | <mkdir dir="a/b"/>
|
---|
| 144 | <touch file="a/a.txt"/>
|
---|
| 145 | <touch file="a/b/b.txt"/>
|
---|
| 146 | <delete>
|
---|
| 147 | <fileset dir="a"/>
|
---|
| 148 | </delete>
|
---|
| 149 | <showdir dir="a"/>
|
---|
| 150 | </target>
|
---|
| 151 | </project>
|
---|
| 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><taskdef></tt>.
|
---|
| 165 | </p>
|
---|
| 166 | <blockquote>
|
---|
| 167 | <pre>
|
---|
| 168 | <project xmlns:<font color="green">antcontrib</font>="<font color="red">antlib:net.sf.antcontrib</font>">
|
---|
| 169 | <taskdef uri="<font color="red">antlib:net.sf.antcontrib</font>"
|
---|
| 170 | resource="net/sf/antcontrib/antlib.xml"
|
---|
| 171 | classpath="path/to/ant-contrib.jar"/>
|
---|
| 172 |
|
---|
| 173 | <target name="iterate">
|
---|
| 174 | <<font color="green">antcontrib</font>:for param="file">
|
---|
| 175 | <fileset dir="."/>
|
---|
| 176 | <sequential>
|
---|
| 177 | <echo message="- @{file}"/>
|
---|
| 178 | </sequential>
|
---|
| 179 | </antcontrib:for>
|
---|
| 180 | </target>
|
---|
| 181 | </project>
|
---|
| 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><typedef></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><if></code>, the
|
---|
| 196 | type <code><isallowed></code> and a macro
|
---|
| 197 | <code><ifallowed></code> that makes use of the task and type:
|
---|
| 198 | </p>
|
---|
| 199 | <blockquote>
|
---|
| 200 | <pre>
|
---|
| 201 | <antlib xmlns:current="ant:current">
|
---|
| 202 | <taskdef name="if" classname="org.acme.ant.If"/>
|
---|
| 203 | <typedef name="isallowed" classname="org.acme.ant.Isallowed"/>
|
---|
| 204 | <macrodef name="ifallowed">
|
---|
| 205 | <attribute name="action"/>
|
---|
| 206 | <element name="do"/>
|
---|
| 207 | <sequential>
|
---|
| 208 | <current:if>
|
---|
| 209 | <current:isallowed test="@{action}"/>
|
---|
| 210 | <current:then>
|
---|
| 211 | <do/>
|
---|
| 212 | </current:then>
|
---|
| 213 | </current:if>
|
---|
| 214 | </sequential>
|
---|
| 215 | </macrodef>
|
---|
| 216 | </antlib>
|
---|
| 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><typedef></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 | <antlib xmlns:antcontrib="antlib:net.sf.antcontrib">
|
---|
| 236 | <presetdef name="javac">
|
---|
| 237 | <javac deprecation="${deprecation}"
|
---|
| 238 | debug="${debug}"/>
|
---|
| 239 | </presetdef>
|
---|
| 240 | <presetdef name="delete">
|
---|
| 241 | <delete quiet="yes"/>
|
---|
| 242 | </presetdef>
|
---|
| 243 | <presetdef name="shellscript">
|
---|
| 244 | <antcontrib:shellscript shell="bash"/>
|
---|
| 245 | </presetdef>
|
---|
| 246 | </antlib>
|
---|
| 247 | </pre>
|
---|
| 248 | </blockquote>
|
---|
| 249 | <p>
|
---|
| 250 | This may be used as follows:
|
---|
| 251 | </p>
|
---|
| 252 | <blockquote>
|
---|
| 253 | <pre>
|
---|
| 254 | <project xmlns:local="localpresets">
|
---|
| 255 | <typedef file="localpresets.xml" uri="localpresets"/>
|
---|
| 256 | <local:shellscript>
|
---|
| 257 | echo "hello world"
|
---|
| 258 | </local:shellscript>
|
---|
| 259 | </project>
|
---|
| 260 | </pre>
|
---|
| 261 | </blockquote>
|
---|
| 262 |
|
---|
| 263 |
|
---|
| 264 |
|
---|
| 265 | </body>
|
---|
| 266 | </html>
|
---|
| 267 |
|
---|