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 4.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us"></meta>
  4. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css"/>
  5. <title>PreSetDef Task</title>
  6. <style type="text/css">
  7. <!--
  8. .code { background: #EFEFEF; margin-top: }
  9. -->
  10. </style>
  11. </head>
  12. <body>
  13. <h2><a name="presetdef">PreSetDef</a></h2>
  14. <h3>Description</h3>
  15. <p>
  16. The preset definition generates a new definition
  17. based on a current definition with some attributes
  18. or elements preset.
  19. </p>
  20. <p>
  21. <em>since Ant 1.6</em>
  22. </p>
  23. <p>
  24. The resolution of properties in any of the attributes or
  25. nested text takes place with the definition is used and <em>not</em>
  26. when the preset definition is defined.
  27. </p>
  28. <h3>Parameters</h3>
  29. <table border="1" cellpadding="2" cellspacing="0">
  30. <tr>
  31. <td valign="top"><b>Attribute</b></td>
  32. <td valign="top"><b>Description</b></td>
  33. <td align="center" valign="top"><b>Required</b></td>
  34. </tr>
  35. <tr>
  36. <td valign="top">name</td>
  37. <td valign="top">the name of the new definition</td>
  38. <td valign="top" align="center">Yes</td>
  39. </tr>
  40. <tr>
  41. <td valign="top">uri</td>
  42. <td valign="top">
  43. The uri that this definition should live in.
  44. </td>
  45. <td valign="top" align="center">No</td>
  46. </tr>
  47. </table>
  48. <h3>Parameters specified as nested elements</h3>
  49. <h4>another type with attributes or elements set</h4>
  50. <p>The <code>&lt;presetdef&gt;</code> task takes one nested element as a parameter.
  51. This nested element can be any other type or task. The attributes
  52. and elements that need to be preset are placed here.
  53. </p>
  54. <h3>Examples</h3>
  55. The following fragment defines a javac task with the debug, deprecation
  56. srcdir and destdir
  57. attributes set. It also has a src element to source files from a generated
  58. directory.
  59. <blockquote>
  60. <pre class="code">
  61. &lt;presetdef name="my.javac"&gt;
  62. &lt;javac debug="${debug}" deprecation="${deprecation}"
  63. srcdir="${src.dir}" destdir="${classes.dir}"&gt;
  64. &lt;src path="${gen.dir}"/&gt;
  65. &lt;/javac&gt;
  66. &lt;/presetdef&gt;
  67. </pre>
  68. </blockquote>
  69. This can be used as a normal javac task - example:
  70. <blockquote>
  71. <pre class="code">
  72. &lt;my.javac/&gt;
  73. </pre>
  74. </blockquote>
  75. The attributes specified in the preset task may be overridden - i.e.
  76. they may be seen as optional attributes - example:
  77. <blockquote>
  78. <pre class="code">
  79. &lt;my.javac srcdir="${test.src}" deprecation="no"/&gt;
  80. </pre>
  81. </blockquote>
  82. One may put a presetdef definition in an antlib.
  83. For example suppose the jar file antgoodies.jar has
  84. the antlib.xml as follows:
  85. <blockquote>
  86. <pre class="code">
  87. &lt;antlib&gt;
  88. &lt;taskdef resource="com/acme/antgoodies/tasks.properties"/&gt;
  89. &lt;!-- Implement the common use of the javac command --&gt;
  90. &lt;presetdef name="javac"&gt;
  91. &lt;javac deprecation="${deprecation}" debug="${debug}"
  92. srcdir="src" destdir="classes"/&gt;
  93. &lt;/presetdef&gt;
  94. &lt;/antlib&gt;
  95. </pre>
  96. </blockquote>
  97. One may then use this in a build file as follows:
  98. <blockquote>
  99. <pre class="code">
  100. &lt;project default="example" xmlns:antgoodies="antlib:com.acme.antgoodies"&gt;
  101. &lt;target name="example"&gt;
  102. &lt;!-- Compile source --&gt;
  103. &lt;antgoodies:javac srcdir="src/main"/&gt;
  104. &lt;!-- Compile test code --&gt;
  105. &lt;antgoodies:javac srcdir="src/test"/&gt;
  106. &lt;/target&gt;
  107. &lt;/project&gt;
  108. </pre>
  109. </blockquote>
  110. <p>
  111. The following is an example of evaluation of properties when the
  112. definition is used:
  113. </p>
  114. <blockquote>
  115. <pre class="code">
  116. &lt;target name="defineandcall"&gt;
  117. &lt;presetdef name="showmessage"&gt;
  118. &lt;echo&gt;message is '${message}'&lt;/echo&gt;
  119. &lt;/presetdef&gt;
  120. &lt;showmessage/&gt;
  121. &lt;property name="message" value="Message 1"/&gt;
  122. &lt;showmessage/&gt;
  123. &lt;antcall target="called"&gt;
  124. &lt;param name="message" value="Message 2"/&gt;
  125. &lt;/antcall&gt;
  126. &lt;/target&gt;
  127. &lt;target name="called"&gt;
  128. &lt;showmessage/&gt;
  129. &lt;/target&gt;
  130. </pre>
  131. </blockquote>
  132. <p>
  133. The command ant defineandcall results in the output:
  134. </p>
  135. <blockquote>
  136. <pre class="code">
  137. defineandcall:
  138. [showmessage] message is '${message}'
  139. [showmessage] message is 'Message 1'
  140. called:
  141. [showmessage] message is 'Message 2'
  142. </pre>
  143. </blockquote>
  144. <hr></hr>
  145. <p align="center">Copyright &copy; 2003-2005 Apache Software
  146. Foundation. All rights Reserved.</p>
  147. </body>
  148. </html>