You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

antlib.html 8.8 KiB

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