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

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