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><presetdef></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 | <presetdef name="my.javac">
|
---|
82 | <javac debug="${debug}" deprecation="${deprecation}"
|
---|
83 | srcdir="${src.dir}" destdir="${classes.dir}">
|
---|
84 | <src path="${gen.dir}"/>
|
---|
85 | </javac>
|
---|
86 | </presetdef>
|
---|
87 | </pre>
|
---|
88 | </blockquote>
|
---|
89 | This can be used as a normal javac task - example:
|
---|
90 | <blockquote>
|
---|
91 | <pre class="code">
|
---|
92 | <my.javac/>
|
---|
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 | <my.javac srcdir="${test.src}" deprecation="no"/>
|
---|
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 | <antlib>
|
---|
108 | <taskdef resource="com/acme/antgoodies/tasks.properties"/>
|
---|
109 | <!-- Implement the common use of the javac command -->
|
---|
110 | <presetdef name="javac">
|
---|
111 | <javac deprecation="${deprecation}" debug="${debug}"
|
---|
112 | srcdir="src" destdir="classes"/>
|
---|
113 | </presetdef>
|
---|
114 | </antlib>
|
---|
115 | </pre>
|
---|
116 | </blockquote>
|
---|
117 | One may then use this in a build file as follows:
|
---|
118 | <blockquote>
|
---|
119 | <pre class="code">
|
---|
120 | <project default="example" xmlns:antgoodies="antlib:com.acme.antgoodies">
|
---|
121 | <target name="example">
|
---|
122 | <!-- Compile source -->
|
---|
123 | <antgoodies:javac srcdir="src/main"/>
|
---|
124 | <!-- Compile test code -->
|
---|
125 | <antgoodies:javac srcdir="src/test"/>
|
---|
126 | </target>
|
---|
127 | </project>
|
---|
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 | <target name="defineandcall">
|
---|
137 | <presetdef name="showmessage">
|
---|
138 | <echo>message is '${message}'</echo>
|
---|
139 | </presetdef>
|
---|
140 | <showmessage/>
|
---|
141 | <property name="message" value="Message 1"/>
|
---|
142 | <showmessage/>
|
---|
143 | <antcall target="called">
|
---|
144 | <param name="message" value="Message 2"/>
|
---|
145 | </antcall>
|
---|
146 | </target>
|
---|
147 | <target name="called">
|
---|
148 | <showmessage/>
|
---|
149 | </target>
|
---|
150 | </pre>
|
---|
151 | </blockquote>
|
---|
152 | <p>
|
---|
153 | The command ant defineandcall results in the output:
|
---|
154 | </p>
|
---|
155 | <blockquote>
|
---|
156 | <pre class="code">
|
---|
157 | defineandcall:
|
---|
158 | [showmessage] message is '${message}'
|
---|
159 | [showmessage] message is 'Message 1'
|
---|
160 |
|
---|
161 | called:
|
---|
162 | [showmessage] message is 'Message 2'
|
---|
163 | </pre>
|
---|
164 | </blockquote>
|
---|
165 | <p>
|
---|
166 | It 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
|
---|
168 | properties to be available in child builds run with
|
---|
169 | <code><ant ... inheritall="false"></code>:
|
---|
170 | </p>
|
---|
171 | <blockquote><pre class="code">
|
---|
172 | <macrodef name="showmessage-presetdef">
|
---|
173 | <attribute name="messageval"/>
|
---|
174 | <presetdef name="showmessage">
|
---|
175 | <echo>message is '@{messageval}'</echo>
|
---|
176 | </presetdef>
|
---|
177 | </macrodef>
|
---|
178 | <showmessage-presetdef messageval="${message}"/>
|
---|
179 | </pre></blockquote>
|
---|
180 | <hr></hr>
|
---|
181 |
|
---|
182 | </body>
|
---|
183 | </html>
|
---|
184 |
|
---|