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">
|
---|
21 | <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
|
---|
22 | <title>Scriptdef Task</title>
|
---|
23 | </head>
|
---|
24 |
|
---|
25 | <body>
|
---|
26 |
|
---|
27 | <h2><a name="script">Scriptdef</a></h2>
|
---|
28 | <h3>Description</h3>
|
---|
29 | <p>Scriptdef can be used to define an Ant task using a scripting language. Ant
|
---|
30 | scripting languages supported by
|
---|
31 | <a href="http://jakarta.apache.org/bsf" target="_top">Apache BSF</a>
|
---|
32 | or
|
---|
33 | <a href="https://scripting.dev.java.net">JSR 223</a>
|
---|
34 | may be
|
---|
35 | used to define the script. Scriptdef provides a mechanism to encapsulate
|
---|
36 | control logic from a build within an Ant task minimizing the need for
|
---|
37 | providing control style tasks in Ant itself. Complex logic can be made
|
---|
38 | available while retaining the simple structure of an Ant build file. Scriptdef
|
---|
39 | is also useful for prototyping new custom tasks. Certainly as the complexity
|
---|
40 | of the script increases it would be better to migrate the task definition
|
---|
41 | into a Java based custom task.
|
---|
42 | </p>
|
---|
43 |
|
---|
44 | <p><b>Note:</b> This task depends on external libraries not included in the
|
---|
45 | Ant distribution. See
|
---|
46 | <a href="../install.html#librarydependencies">Library Dependencies</a>
|
---|
47 | for more information.</p>
|
---|
48 |
|
---|
49 |
|
---|
50 |
|
---|
51 | <p>The attributes and nested elements supported by the task may be defined
|
---|
52 | using <code><attribute></code> and <code><element></code> nested elements. These are
|
---|
53 | available to the script that implements the task as two collection style
|
---|
54 | script variables <code>attributes</code> and <code>elements</code>. The
|
---|
55 | elements in the <code>attributes</code> collection may be accessed by the
|
---|
56 | attribute name. The <code>elements</code> collection is accessed by the nested
|
---|
57 | element name. This will return a list of all instances of the nested element.
|
---|
58 | The instances in this list may be accessed by an integer index.
|
---|
59 | </p>
|
---|
60 |
|
---|
61 | <p><b>Note:</b> Ant will turn all attribute and element names into all
|
---|
62 | lowercase names, so even if you use name="SomeAttribute", you'll have
|
---|
63 | to use "someattribute" to retrieve the attribute's value from the
|
---|
64 | <code>attributes</code> collection.</p>
|
---|
65 |
|
---|
66 | <p>The name "self" (<i>since Ant 1.6.3</i>) is a pre-defined reference to the
|
---|
67 | script def task instance.
|
---|
68 | It can be used for logging, or for integration with the rest of
|
---|
69 | ant. the <code>self.text attribute</code> contains
|
---|
70 | any nested text passed to the script</p>
|
---|
71 |
|
---|
72 | <p>If an attribute or element is not passed in,
|
---|
73 | then <code>attributes.get()</code> or <code>elements.get()</code> will
|
---|
74 | return null. It is up to the script to perform any checks and validation.
|
---|
75 | <code>self.fail(String message)</code>can be used to raise a
|
---|
76 | <code>BuildException</code>.
|
---|
77 | </p>
|
---|
78 |
|
---|
79 |
|
---|
80 | <p>The name "project" is a pre-defined reference to the Ant Project. For
|
---|
81 | more information on writing scripts, please refer to the
|
---|
82 | <a href="script.html"><code><script></code></a> task
|
---|
83 | </p>
|
---|
84 |
|
---|
85 |
|
---|
86 | <h3>Parameters</h3>
|
---|
87 | <table border="1" cellpadding="2" cellspacing="0">
|
---|
88 | <tr>
|
---|
89 | <td valign="top"><b>Attribute</b></td>
|
---|
90 | <td valign="top"><b>Description</b></td>
|
---|
91 | <td align="center" valign="top"><b>Required</b></td>
|
---|
92 | </tr>
|
---|
93 | <tr>
|
---|
94 | <td valign="top">name</td>
|
---|
95 | <td valign="top">the name of the task to be created using the script</td>
|
---|
96 | <td valign="top" align="center">Yes</td>
|
---|
97 | </tr>
|
---|
98 | <tr>
|
---|
99 | <td valign="top">language</td>
|
---|
100 | <td valign="top">The programming language the script is written in.
|
---|
101 | Must be a supported Apache BSF or JSR 223 language</td>
|
---|
102 | <td valign="top" align="center">Yes</td>
|
---|
103 | </tr>
|
---|
104 | <tr>
|
---|
105 | <td valign="top">manager</td>
|
---|
106 | <td valign="top">
|
---|
107 | The script engine manager to use.
|
---|
108 | See the <a href="../OptionalTasks/script.html">script</a> task
|
---|
109 | for using this attribute.
|
---|
110 | </td>
|
---|
111 | <td valign="top" align="center">No - default is "auto"</td>
|
---|
112 | </tr>
|
---|
113 | <tr>
|
---|
114 | <td valign="top">src</td>
|
---|
115 | <td valign="top">The location of the script as a file, if not inline</td>
|
---|
116 | <td valign="top" align="center">No</td>
|
---|
117 | </tr>
|
---|
118 | <tr>
|
---|
119 | <td valign="top">uri</td>
|
---|
120 | <td valign="top">
|
---|
121 | The XML namespace uri that this definition should live in.
|
---|
122 | </td>
|
---|
123 | <td valign="top" align="center">No</td>
|
---|
124 | </tr>
|
---|
125 | <tr>
|
---|
126 | <td valign="top">classpath</td>
|
---|
127 | <td valign="top">
|
---|
128 | The classpath to pass into the script.
|
---|
129 | </td>
|
---|
130 | <td align="center" valign="top">No</td>
|
---|
131 | </tr>
|
---|
132 | <tr>
|
---|
133 | <td valign="top">classpathref</td>
|
---|
134 | <td valign="top">The classpath to use, given as a
|
---|
135 | <a href="../using.html#references">reference</a> to a path defined elsewhere.
|
---|
136 | <td align="center" valign="top">No</td>
|
---|
137 | </tr>
|
---|
138 | <tr>
|
---|
139 | <td valign="top">loaderRef</td>
|
---|
140 | <td valign="top">the name of the loader that is
|
---|
141 | used to load the script, constructed from the specified
|
---|
142 | classpath. This allows multiple script defintions
|
---|
143 | to reuse the same class loader.
|
---|
144 | </td>
|
---|
145 | <td align="center" valign="top">No</td>
|
---|
146 | </tr>
|
---|
147 | </table>
|
---|
148 |
|
---|
149 | <h3>Nested elements</h3>
|
---|
150 | <h4>attribute</h4>
|
---|
151 | <table border="1" cellpadding="2" cellspacing="0">
|
---|
152 | <tr>
|
---|
153 | <td valign="top"><b>Attribute</b></td>
|
---|
154 | <td valign="top"><b>Description</b></td>
|
---|
155 | <td align="center" valign="top"><b>Required</b></td>
|
---|
156 | </tr>
|
---|
157 | <tr>
|
---|
158 | <td valign="top">name</td>
|
---|
159 | <td valign="top">the name of the attribute</td>
|
---|
160 | <td valign="top" align="center">Yes</td>
|
---|
161 | </tr>
|
---|
162 | </table>
|
---|
163 |
|
---|
164 | <h4>element</h4>
|
---|
165 | <table border="1" cellpadding="2" cellspacing="0">
|
---|
166 | <tr>
|
---|
167 | <td valign="top"><b>Attribute</b></td>
|
---|
168 | <td valign="top"><b>Description</b></td>
|
---|
169 | <td align="center" valign="top"><b>Required</b></td>
|
---|
170 | </tr>
|
---|
171 | <tr>
|
---|
172 | <td valign="top">name</td>
|
---|
173 | <td valign="top">the name of the nested element to be supported by the
|
---|
174 | task defined by the script</td>
|
---|
175 | <td valign="top" align="center">Yes</td>
|
---|
176 | </tr>
|
---|
177 | <tr>
|
---|
178 | <td valign="top">classname</td>
|
---|
179 | <td valign="top">the classname of the class to be used for the nested element.
|
---|
180 | This specifies the class directly and is an alternative to specifying
|
---|
181 | the Ant type name.</td>
|
---|
182 | <td valign="top" align="center">No</td>
|
---|
183 | </tr>
|
---|
184 | <tr>
|
---|
185 | <td valign="top">type</td>
|
---|
186 | <td valign="top">This is the name of an Ant task or type which is to
|
---|
187 | be used when this element is to be created. This is an alternative
|
---|
188 | to specifying the class name directly. If the type is in a namespace,
|
---|
189 | the URI and a : must be prefixed to the type. For example
|
---|
190 | <code>type="antlib:example.org:newtype"</code></td>
|
---|
191 | <td valign="top" align="center">No</td>
|
---|
192 | </tr>
|
---|
193 | </table>
|
---|
194 |
|
---|
195 | <h4>classpath</h4>
|
---|
196 | <p>
|
---|
197 | See the <a href="../OptionalTasks/script.html">script</a> task
|
---|
198 | for using this nested element.
|
---|
199 | </p>
|
---|
200 |
|
---|
201 | <h3>Examples</h3>
|
---|
202 |
|
---|
203 | <p>
|
---|
204 | The following definition creates a task which supports an attribute called
|
---|
205 | attr and two nested elements, one being a fileset and the other a path. When
|
---|
206 | executed, the resulting task logs the value of the attribute and the basedir
|
---|
207 | of the first fileset.
|
---|
208 | </p>
|
---|
209 |
|
---|
210 | <pre>
|
---|
211 | <scriptdef name="scripttest" language="javascript">
|
---|
212 | <attribute name="attr1"/>
|
---|
213 | <element name="fileset" type="fileset"/>
|
---|
214 | <element name="path" type="path"/>
|
---|
215 | <![CDATA[
|
---|
216 |
|
---|
217 | self.log("Hello from script");
|
---|
218 | self.log("Attribute attr1 = " + attributes.get("attr1"));
|
---|
219 | self.log("First fileset basedir = "
|
---|
220 | + elements.get("fileset").get(0).getDir(project));
|
---|
221 |
|
---|
222 | ]]>
|
---|
223 | </scriptdef>
|
---|
224 |
|
---|
225 | <scripttest attr1="test">
|
---|
226 | <path>
|
---|
227 | <pathelement location="src"/>
|
---|
228 | </path>
|
---|
229 | <fileset dir="src"/>
|
---|
230 | <fileset dir="main"/>
|
---|
231 | </scripttest>
|
---|
232 | </pre>
|
---|
233 |
|
---|
234 | <p>
|
---|
235 | The following variation on the above script lists the number of fileset elements
|
---|
236 | and iterates through them
|
---|
237 | </p>
|
---|
238 | <pre>
|
---|
239 | <scriptdef name="scripttest2" language="javascript">
|
---|
240 | <element name="fileset" type="fileset"/>
|
---|
241 | <![CDATA[
|
---|
242 | filesets = elements.get("fileset");
|
---|
243 | self.log("Number of filesets = " + filesets.size());
|
---|
244 | for (i = 0; i < filesets.size(); ++i) {
|
---|
245 | self.log("fileset " + i + " basedir = "
|
---|
246 | + filesets.get(i).getDir(project));
|
---|
247 | }
|
---|
248 | ]]>
|
---|
249 | </scriptdef>
|
---|
250 |
|
---|
251 | <scripttest2>
|
---|
252 | <fileset dir="src"/>
|
---|
253 | <fileset dir="main"/>
|
---|
254 | </scripttest2>
|
---|
255 | </pre>
|
---|
256 |
|
---|
257 | <p>
|
---|
258 | When a script has a syntax error, the scriptdef name will be listed in the
|
---|
259 | error. For example in the above script, removing the closing curly bracket
|
---|
260 | would result in this error
|
---|
261 | </p>
|
---|
262 |
|
---|
263 | <p><code>build.xml:15: SyntaxError: missing } in compound
|
---|
264 | statement (scriptdef <code><scripttest2></code>; line 10)</code></p>
|
---|
265 |
|
---|
266 | <p>
|
---|
267 | Script errors are only detected when a script task is actually executed.
|
---|
268 | </p>
|
---|
269 | <p>
|
---|
270 | The next example does uses nested text in Jython. It also declares
|
---|
271 | the script in a new xml namespace, which must be used to refer to
|
---|
272 | the task. Declaring scripts in a new namespace guarantees that Ant will
|
---|
273 | not create a task of the same (namespace,localname) name pair.
|
---|
274 | </p>
|
---|
275 |
|
---|
276 | <pre>
|
---|
277 | <target name="echo-task-jython">
|
---|
278 | <scriptdef language="jython"
|
---|
279 | name="echo"
|
---|
280 | uri="http://example.org/script">
|
---|
281 | <![CDATA[
|
---|
282 | self.log("text: " +self.text)
|
---|
283 | ]]>
|
---|
284 | </scriptdef>
|
---|
285 | </target>
|
---|
286 |
|
---|
287 | <target name="testEcho" depends="echo-task-jython"
|
---|
288 | xmlns:s="http://example.org/script">
|
---|
289 | <s:echo>nested text</s:echo>
|
---|
290 | </target>
|
---|
291 | </pre>
|
---|
292 |
|
---|
293 | The next example shows the use of <classpath> and
|
---|
294 | "loaderref" to get access to the beanshell jar.
|
---|
295 | <pre>
|
---|
296 | <scriptdef name="b1" language="beanshell"
|
---|
297 | loaderref="beanshell-ref">
|
---|
298 | <attribute name="a"/>
|
---|
299 | <classpath
|
---|
300 | path="${user.home}/scripting/beanshell/bsh-1.3b1.jar"/>
|
---|
301 | self.log("attribute a is " + attributes.get("a"));
|
---|
302 | </scriptdef>
|
---|
303 |
|
---|
304 | <scriptdef name="b2" language="beanshell"
|
---|
305 | loaderref="beanshell-ref">
|
---|
306 | <attribute name="a2"/>
|
---|
307 | self.log("attribute a2 is " + attributes.get("a2"));
|
---|
308 | </scriptdef>
|
---|
309 |
|
---|
310 | <b1 a="this is an 'a'"/>
|
---|
311 | <b2 a2="this is an 'a2' for b2"/>
|
---|
312 | </pre>
|
---|
313 | <h3>Testing Scripts</h3>
|
---|
314 |
|
---|
315 | <p>
|
---|
316 | The easiest way to test scripts is to use the
|
---|
317 | <a href="http://ant.apache.org/antlibs/antunit/">AntUnit</a> ant library.
|
---|
318 | This will run all targets in a script that begin with "test" (and their dependencies). </p>
|
---|
319 |
|
---|
320 |
|
---|
321 |
|
---|
322 |
|
---|
323 | </body>
|
---|
324 | </html>
|
---|