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.

presetdef.html 5.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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>PreSetDef Task</title>
  20. <style type="text/css">
  21. <!--
  22. .code { background: #EFEFEF; margin-top: }
  23. -->
  24. </style>
  25. </head>
  26. <body>
  27. <h2><a name="presetdef">PreSetDef</a></h2>
  28. <h3>Description</h3>
  29. <p>
  30. The preset definition generates a new definition
  31. based on a current definition with some attributes
  32. or elements preset.
  33. </p>
  34. <p>
  35. <em>since Ant 1.6</em>
  36. </p>
  37. <p>
  38. The resolution of properties in any of the attributes or
  39. nested text takes place with the definition is used and <em>not</em>
  40. when the preset definition is defined.
  41. </p>
  42. <h3>Parameters</h3>
  43. <table border="1" cellpadding="2" cellspacing="0">
  44. <tr>
  45. <td valign="top"><b>Attribute</b></td>
  46. <td valign="top"><b>Description</b></td>
  47. <td align="center" valign="top"><b>Required</b></td>
  48. </tr>
  49. <tr>
  50. <td valign="top">name</td>
  51. <td valign="top">the name of the new definition</td>
  52. <td valign="top" align="center">Yes</td>
  53. </tr>
  54. <tr>
  55. <td valign="top">uri</td>
  56. <td valign="top">
  57. The uri that this definition should live in.
  58. </td>
  59. <td valign="top" align="center">No</td>
  60. </tr>
  61. </table>
  62. <h3>Parameters specified as nested elements</h3>
  63. <h4>another type with attributes or elements set</h4>
  64. <p>The <code>&lt;presetdef&gt;</code> task takes one nested element as a parameter.
  65. This nested element can be any other type or task. The attributes
  66. and elements that need to be preset are placed here.
  67. </p>
  68. <h3>Examples</h3>
  69. The following fragment defines a javac task with the debug, deprecation
  70. srcdir and destdir
  71. attributes set. It also has a src element to source files from a generated
  72. directory.
  73. <blockquote>
  74. <pre class="code">
  75. &lt;presetdef name="my.javac"&gt;
  76. &lt;javac debug="${debug}" deprecation="${deprecation}"
  77. srcdir="${src.dir}" destdir="${classes.dir}"&gt;
  78. &lt;src path="${gen.dir}"/&gt;
  79. &lt;/javac&gt;
  80. &lt;/presetdef&gt;
  81. </pre>
  82. </blockquote>
  83. This can be used as a normal javac task - example:
  84. <blockquote>
  85. <pre class="code">
  86. &lt;my.javac/&gt;
  87. </pre>
  88. </blockquote>
  89. The attributes specified in the preset task may be overridden - i.e.
  90. they may be seen as optional attributes - example:
  91. <blockquote>
  92. <pre class="code">
  93. &lt;my.javac srcdir="${test.src}" deprecation="no"/&gt;
  94. </pre>
  95. </blockquote>
  96. One may put a presetdef definition in an antlib.
  97. For example suppose the jar file antgoodies.jar has
  98. the antlib.xml as follows:
  99. <blockquote>
  100. <pre class="code">
  101. &lt;antlib&gt;
  102. &lt;taskdef resource="com/acme/antgoodies/tasks.properties"/&gt;
  103. &lt;!-- Implement the common use of the javac command --&gt;
  104. &lt;presetdef name="javac"&gt;
  105. &lt;javac deprecation="${deprecation}" debug="${debug}"
  106. srcdir="src" destdir="classes"/&gt;
  107. &lt;/presetdef&gt;
  108. &lt;/antlib&gt;
  109. </pre>
  110. </blockquote>
  111. One may then use this in a build file as follows:
  112. <blockquote>
  113. <pre class="code">
  114. &lt;project default="example" xmlns:antgoodies="antlib:com.acme.antgoodies"&gt;
  115. &lt;target name="example"&gt;
  116. &lt;!-- Compile source --&gt;
  117. &lt;antgoodies:javac srcdir="src/main"/&gt;
  118. &lt;!-- Compile test code --&gt;
  119. &lt;antgoodies:javac srcdir="src/test"/&gt;
  120. &lt;/target&gt;
  121. &lt;/project&gt;
  122. </pre>
  123. </blockquote>
  124. <p>
  125. The following is an example of evaluation of properties when the
  126. definition is used:
  127. </p>
  128. <blockquote>
  129. <pre class="code">
  130. &lt;target name="defineandcall"&gt;
  131. &lt;presetdef name="showmessage"&gt;
  132. &lt;echo&gt;message is '${message}'&lt;/echo&gt;
  133. &lt;/presetdef&gt;
  134. &lt;showmessage/&gt;
  135. &lt;property name="message" value="Message 1"/&gt;
  136. &lt;showmessage/&gt;
  137. &lt;antcall target="called"&gt;
  138. &lt;param name="message" value="Message 2"/&gt;
  139. &lt;/antcall&gt;
  140. &lt;/target&gt;
  141. &lt;target name="called"&gt;
  142. &lt;showmessage/&gt;
  143. &lt;/target&gt;
  144. </pre>
  145. </blockquote>
  146. <p>
  147. The command ant defineandcall results in the output:
  148. </p>
  149. <blockquote>
  150. <pre class="code">
  151. defineandcall:
  152. [showmessage] message is '${message}'
  153. [showmessage] message is 'Message 1'
  154. called:
  155. [showmessage] message is 'Message 2'
  156. </pre>
  157. </blockquote>
  158. <p>
  159. It is possible to use a trick to evaluate properties when the definition is
  160. <em>made</em> rather than used. This can be useful if you do not expect some
  161. properties to be available in child builds run with
  162. <code>&lt;ant ... inheritall="false"&gt;</code>:
  163. </p>
  164. <blockquote><pre class="code">
  165. &lt;macrodef name="showmessage-presetdef"&gt;
  166. &lt;attribute name="messageval"/&gt;
  167. &lt;presetdef name="showmessage"&gt;
  168. &lt;echo&gt;message is '@{messageval}'&lt;/echo&gt;
  169. &lt;/presetdef&gt;
  170. &lt;/macrodef&gt;
  171. &lt;showmessage-presetdef messageval="${message}"/&gt;
  172. </pre></blockquote>
  173. <hr></hr>
  174. </body>
  175. </html>