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.5 KiB

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