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

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