source: other-projects/trunk/realistic-books/packages/AntInstaller/web/manual/manual/CoreTasks/presetdef.html@ 19253

Last change on this file since 19253 was 19253, checked in by davidb, 15 years ago

Establishing a source code repository for Veronica's Realistic Book's software

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