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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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>PreSetDef Task</title>
  20. </head>
  21. <body>
  22. <h2 id="presetdef">PreSetDef</h2>
  23. <p><em>Since Apache Ant 1.6</em></p>
  24. <h3>Description</h3>
  25. <p>The preset definition generates a new definition based on a current definition with some
  26. attributes or elements preset.</p>
  27. <p>The resolution of properties in any of the attributes or nested text takes place with the
  28. definition is used and <em>not</em> when the preset definition is defined.</p>
  29. <h3>Parameters</h3>
  30. <table class="attr">
  31. <tr>
  32. <th scope="col">Attribute</th>
  33. <th scope="col">Description</th>
  34. <th scope="col">Required</th>
  35. </tr>
  36. <tr>
  37. <td>name</td>
  38. <td>the name of the new definition</td>
  39. <td>Yes</td>
  40. </tr>
  41. <tr>
  42. <td>uri</td>
  43. <td>The URI that this definition should live in.</td>
  44. <td>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. This nested
  50. element can be any other type or task. The attributes and elements that need to be preset are
  51. placed here.</p>
  52. <h3>Examples</h3>
  53. <p>The following fragment defines a <code>javac</code> task with
  54. the <var>debug</var>, <var>deprecation</var>, <var>srcdir</var> and <var>destdir</var>
  55. attributes set. It also has a <code>src</code> element to source files from a generated
  56. directory.</p>
  57. <pre>
  58. &lt;presetdef name="my.javac"&gt;
  59. &lt;javac debug="${debug}" deprecation="${deprecation}"
  60. srcdir="${src.dir}" destdir="${classes.dir}"&gt;
  61. &lt;src path="${gen.dir}"/&gt;
  62. &lt;/javac&gt;
  63. &lt;/presetdef&gt;</pre>
  64. <p>This can be used as a normal <code>javac</code> task&mdash;for example:</p>
  65. <pre>&lt;my.javac/&gt;</pre>
  66. <p>The attributes specified in the preset task may be overridden&mdash;i.e. they may be seen as
  67. optional attributes&mdash;for example:</p>
  68. <pre>&lt;my.javac srcdir="${test.src}" deprecation="no"/&gt;</pre>
  69. <p>One may put a <code>presetdef</code> definition in an antlib. For example suppose the jar
  70. file <samp>antgoodies.jar</samp> has the <samp>antlib.xml</samp> as follows:</p>
  71. <pre>
  72. &lt;antlib&gt;
  73. &lt;taskdef resource="com/acme/antgoodies/tasks.properties"/&gt;
  74. &lt;!-- Implement the common use of the javac command --&gt;
  75. &lt;presetdef name="javac"&gt;
  76. &lt;javac deprecation="${deprecation}" debug="${debug}"
  77. srcdir="src" destdir="classes"/&gt;
  78. &lt;/presetdef&gt;
  79. &lt;/antlib&gt;</pre>
  80. <p>One may then use this in a build file as follows:</p>
  81. <pre>
  82. &lt;project default="example" xmlns:antgoodies="antlib:com.acme.antgoodies"&gt;
  83. &lt;target name="example"&gt;
  84. &lt;!-- Compile source --&gt;
  85. &lt;antgoodies:javac srcdir="src/main"/&gt;
  86. &lt;!-- Compile test code --&gt;
  87. &lt;antgoodies:javac srcdir="src/test"/&gt;
  88. &lt;/target&gt;
  89. &lt;/project&gt;</pre>
  90. <p>The following is an example of evaluation of properties when the definition is used:</p>
  91. <pre>
  92. &lt;target name="defineandcall"&gt;
  93. &lt;presetdef name="showmessage"&gt;
  94. &lt;echo&gt;message is '${message}'&lt;/echo&gt;
  95. &lt;/presetdef&gt;
  96. &lt;showmessage/&gt;
  97. &lt;property name="message" value="Message 1"/&gt;
  98. &lt;showmessage/&gt;
  99. &lt;antcall target="called"&gt;
  100. &lt;param name="message" value="Message 2"/&gt;
  101. &lt;/antcall&gt;
  102. &lt;/target&gt;
  103. &lt;target name="called"&gt;
  104. &lt;showmessage/&gt;
  105. &lt;/target&gt;</pre>
  106. <p>The command <kbd>ant defineandcall</kbd> results in the output:</p>
  107. <pre class="output">
  108. defineandcall:
  109. [showmessage] message is '${message}'
  110. [showmessage] message is 'Message 1'
  111. called:
  112. [showmessage] message is 'Message 2'</pre>
  113. <p>It is possible to use a trick to evaluate properties when the definition is <em>made</em>
  114. rather than used. This can be useful if you do not expect some properties to be available in
  115. child builds run with <code>&lt;ant ... inheritall="false"&gt;</code>:</p>
  116. <pre>
  117. &lt;macrodef name="showmessage-presetdef"&gt;
  118. &lt;attribute name="messageval"/&gt;
  119. &lt;presetdef name="showmessage"&gt;
  120. &lt;echo&gt;message is '@{messageval}'&lt;/echo&gt;
  121. &lt;/presetdef&gt;
  122. &lt;/macrodef&gt;
  123. &lt;showmessage-presetdef messageval="${message}"/&gt;</pre>
  124. </body>
  125. </html>