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.

typedef.html 9.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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">
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>Typedef Task</title>
  20. </head>
  21. <body>
  22. <h2 id="typedef">Typedef</h2>
  23. <h3>Description</h3>
  24. <p>Adds a task or a data type definition to the current project such that this new type or task can
  25. be used in the current project.</p>
  26. <p>A Task is any class that extends <code class="code">org.apache.tools.ant.Task</code> or can be adapted
  27. as a Task using an adapter class.</p>
  28. <p>Data types are things like <a href="../using.html#path">paths</a>
  29. or <a href="../Types/fileset.html">filesets</a> that can be defined at the project level and
  30. referenced via their <var>id</var> attribute. Custom data types usually need custom tasks to put
  31. them to good use.</p>
  32. <p>Two attributes are needed to make a definition: the name that identifies this data type uniquely,
  33. and the full name of the class (including its package name) that implements this type.</p>
  34. <p>You can also define a group of definitions at once using the file or resource attributes. These
  35. attributes point to files in the format of Java property files or an xml format.</p>
  36. <p>For property files each line defines a single data type in the format:</p>
  37. <pre>typename=fully.qualified.java.classname</pre>
  38. <p>The xml format is described in the <a href="../Types/antlib.html">Antlib</a> section.</p>
  39. <p>If you are defining tasks or types that share the same classpath with
  40. multiple <code>taskdef</code> or <code>typedef</code> tasks, the corresponding classes will be
  41. loaded by different
  42. Java <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html"
  43. target="_top">ClassLoaders</a>. Two classes with the same name loaded via different ClassLoaders
  44. are not the same class from the point of view of JVM, they don't share static variables and
  45. instances of these classes can't access private methods or attributes of instances defined by "the
  46. other class" of the same name. They don't even belong to the same Java package and can't access
  47. package private code, either.</p>
  48. <p>The best way to load several tasks/types that are supposed to cooperate with each other via
  49. shared Java code is to use the <var>resource</var> attribute and an <code>antlib</code> descriptor.
  50. If this is not possible, the second best option is to use the <var>loaderref</var> attribute and
  51. specify the same name for each and every <code>typedef</code>/<code>taskdef</code>&mdash;this way
  52. the classes will share the same <code>ClassLoader</code>. Note that
  53. the <code>typedef</code>/<code>taskdef</code> tasks must use identical classpath definitions (this
  54. includes the order of path components) for the <var>loaderref</var> attribute to work.</p>
  55. <h3>Parameters</h3>
  56. <table class="attr">
  57. <tr>
  58. <th scope="col">Attribute</th>
  59. <th scope="col">Description</th>
  60. <th scope="col">Required</th>
  61. </tr>
  62. <tr>
  63. <td>name</td>
  64. <td>the name of the data type</td>
  65. <td rowspan="2">Yes, unless <var>file</var> or <var>resource</var> attributes have been
  66. specified.</td>
  67. </tr>
  68. <tr>
  69. <td>classname</td>
  70. <td class="left">the full class name implementing the data type</td>
  71. </tr>
  72. <tr>
  73. <td>file</td>
  74. <td>Name of the file to load definitions from.</td>
  75. <td>No</td>
  76. </tr>
  77. <tr>
  78. <td>resource</td>
  79. <td>Name of the resource to load definitions from. If multiple resources by this name are found
  80. along the classpath, and <var>format</var> is <q>properties</q>, the first resource will be
  81. loaded; otherwise all such resources will be loaded.</td>
  82. <td>No</td>
  83. </tr>
  84. <tr>
  85. <td>format</td>
  86. <td>The format of the file or resource. The values are <q>properties</q>" or <q>xml</q>. If the
  87. value is <q>properties</q> the file/resource is a property file contains name-classname
  88. pairs. If the value is <q>xml</q>, the file/resource is an XML file/resource structured
  89. according to <a href="../Types/antlib.html">Antlib</a>. The default is <q>properties</q>
  90. unless the file/resource name ends with <samp>.xml</samp>, in which case the <var>format</var>
  91. attribute will have the value <q>xml</q>. <em>Since Ant 1.6</em></td>
  92. <td>No</td>
  93. </tr>
  94. <tr>
  95. <td>classpath</td>
  96. <td>the classpath to use when looking up <var>classname</var>.</td>
  97. <td>No</td>
  98. </tr>
  99. <tr>
  100. <td>classpathref</td>
  101. <td>a reference to a classpath to use when looking up <var>classname</var>.</td>
  102. <td>No</td>
  103. </tr>
  104. <tr>
  105. <td>loaderRef</td>
  106. <td>the name of the loader that is used to load the class, constructed from the specified
  107. classpath. Use this to allow multiple tasks/types to be loaded with the same loader, so they
  108. can call each other. <em>Since Ant 1.5</em></td>
  109. <td>No</td>
  110. </tr>
  111. <tr>
  112. <td>onerror</td>
  113. <td>The action to take if there was a failure in defining the type. The values are <q>fail</q>:
  114. cause a build exception; <q>report</q>: output a warning, but continue; <q>ignore</q>: do
  115. nothing. <em>Since Ant 1.6</em>, an additional value is <q>failall</q>: cause all behavior of
  116. fail, as well as a build exception for the resource or file attribute if the resource or file
  117. is not found.</td>
  118. <td>No; default is <q>fail</q> (<em>since Ant 1.7</em>)</td>
  119. </tr>
  120. <tr>
  121. <td>adapter</td>
  122. <td>A class that is used to adapt the defined class to another interface/class. The adapter
  123. class must implement the interface <code>org.apache.tools.ant.TypeAdapter</code>. The adapter
  124. class will be used to wrap the defined class unless the defined class implements/extends the
  125. class defined by the attribute <q>adaptto</q>. If <q>adaptto</q> is not set, the defined
  126. class will always be wrapped. <em>Since Ant 1.6</em></td>
  127. <td>No</td>
  128. </tr>
  129. <tr>
  130. <td>adaptto</td>
  131. <td>This attribute is used in conjunction with the adapter attribute. If the defined class does
  132. not implement/extend the interface/class specified by this attribute, the adaptor class will
  133. be used to wrap the class. <em>Since Ant 1.6</em></td>
  134. <td>No</td>
  135. </tr>
  136. <tr>
  137. <td>uri</td>
  138. <td>The uri that this definition should live in. <em>Since Ant 1.6</em></td>
  139. <td>No</td>
  140. </tr>
  141. </table>
  142. <h3>Parameters specified as nested elements</h3>
  143. <h4>classpath</h4>
  144. <p><code>Typedef</code>'s <var>classpath</var> attribute is a <a href="../using.html#path">path-like
  145. structure</a> and can also be set via a nested <code>classpath</code> element.</p>
  146. <h3>Examples</h3>
  147. <p>The following fragment defines define a type called <code>urlset</code>.</p>
  148. <pre>&lt;typedef name="urlset" classname="com.mydomain.URLSet"/&gt;</pre>
  149. <p>The data type is now available to Ant. The class <code>com.mydomain.URLSet</code> implements this
  150. type.</p>
  151. <p>Assuming a class <code>org.acme.ant.RunnableAdapter</code> that
  152. extends <code class="code">Task</code> and
  153. implements <code class="code">org.apache.tools.ant.TypeAdapter</code>, and in
  154. the <code class="code">execute()</code> method invokes <code class="code">run()</code> on the
  155. proxied object, one may use a <code class="code">Runnable</code> class as an Ant task. The following
  156. fragment defines a task called <code>runclock</code>.</p>
  157. <pre>
  158. &lt;typedef name="runclock"
  159. classname="com.acme.ant.RunClock"
  160. adapter="org.acme.ant.RunnableAdapter"/&gt;</pre>
  161. <p>The following fragment shows the use of the <var>classpathref</var> and <var>loaderref</var> to
  162. load up two definitions.</p>
  163. <pre>
  164. &lt;path id="lib.path"&gt;
  165. &lt;fileset dir="lib" includes="lib/*.jar"/&gt;
  166. &lt;/path&gt;
  167. &lt;typedef name="filter1"
  168. classname="org.acme.filters.Filter1"
  169. classpathref="lib.path"
  170. loaderref="lib.path.loader"/&gt;
  171. &lt;typedef name="filter2"
  172. classname="org.acme.filters.Filter2"
  173. loaderref="lib.path.loader"/&gt;</pre>
  174. <p>If you want to load an antlib into a special XML namespace, the <var>uri</var> attribute is
  175. important:</p>
  176. <pre>
  177. &lt;project xmlns:antcontrib="antlib:net.sf.antcontrib"&gt;
  178. &lt;taskdef uri="antlib:net.sf.antcontrib"
  179. resource="net/sf/antcontrib/antlib.xml"
  180. classpath="path/to/ant-contrib.jar"/&gt;</pre>
  181. <p>Here the namespace declaration <code>xmlns:antcontrib="antlib:net.sf.antcontrib"</code> allows
  182. tasks and types of the Ant-Contrib Antlib to be used with the <samp>antcontrib</samp> prefix
  183. like <code>&lt;antcontrib:if&gt;</code>. The normal rules of XML namespaces apply and you can
  184. declare the prefix at any element to make it usable for the element it is declared on as well as all
  185. its child elements.</p>
  186. </body>
  187. </html>