1 | <html>
|
---|
2 |
|
---|
3 | <head>
|
---|
4 | <meta http-equiv="Content-Language" content="en-us">
|
---|
5 | <title>Scriptdef Task</title>
|
---|
6 | <link rel="stylesheet" type="text/css" href="../stylesheets/antmanual.css">
|
---|
7 | </head>
|
---|
8 |
|
---|
9 | <body>
|
---|
10 |
|
---|
11 | <h2><a name="script">Script</a></h2>
|
---|
12 | <h3>Description</h3>
|
---|
13 | <p>Scriptdef can be used to define an Ant task using a scripting language. Ant
|
---|
14 | scripting languages supported by
|
---|
15 | <a href="http://jakarta.apache.org/bsf" target="_top">Apache BSF</a> may be
|
---|
16 | used to define the script. Scriptdef provides a mechanism to encapsulate
|
---|
17 | control logic from a build within an Ant task minimizing the need for
|
---|
18 | providing control style tasks in Ant itself. Complex logic can be made
|
---|
19 | available while retaining the simple structure of an Ant build file. Scriptdef
|
---|
20 | is also useful for prototyping new custom tasks. Certainly as the complexity
|
---|
21 | of the script increases it would be better to migrate the task definition
|
---|
22 | into a Java based custom task.
|
---|
23 | </p>
|
---|
24 |
|
---|
25 | <p><b>Note:</b> This task depends on external libraries not included in the
|
---|
26 | Ant distribution. See
|
---|
27 | <a href="../install.html#librarydependencies">Library Dependencies</a>
|
---|
28 | for more information.</p>
|
---|
29 |
|
---|
30 | <p>The attributes and nested elements supported by the task may be defined
|
---|
31 | using <code><attribute></code> and <code><element></code> nested elements. These are
|
---|
32 | available to the script that implements the task as two collection style
|
---|
33 | script variables <code>attributes</code> and <code>elements</code>. The
|
---|
34 | elements in the <code>attributes</code> collection may be accessed by the
|
---|
35 | attribute name. The <code>elements</code> collection is accessed by the nested
|
---|
36 | element name. This will return a list of all instances of the nested element.
|
---|
37 | The instances in this list may be accessed by an integer index.
|
---|
38 | </p>
|
---|
39 |
|
---|
40 | <p><b>Note:</b> Ant will turn all attribute and element names into all
|
---|
41 | lowercase names, so even if you use name="SomeAttribute", you'll have
|
---|
42 | to use "someattribute" to retrieve the attribute's value from the
|
---|
43 | <code>attributes</code> collection.</p>
|
---|
44 |
|
---|
45 | <p>The name "project" is a pre-defined reference to the Ant Project. For
|
---|
46 | more information on writing scripts, please refer to the
|
---|
47 | <a href="script.html"><code><script></code></a> task
|
---|
48 | </p>
|
---|
49 |
|
---|
50 | <h3>Parameters</h3>
|
---|
51 | <table border="1" cellpadding="2" cellspacing="0">
|
---|
52 | <tr>
|
---|
53 | <td valign="top"><b>Attribute</b></td>
|
---|
54 | <td valign="top"><b>Description</b></td>
|
---|
55 | <td align="center" valign="top"><b>Required</b></td>
|
---|
56 | </tr>
|
---|
57 | <tr>
|
---|
58 | <td valign="top">name</td>
|
---|
59 | <td valign="top">the name of the task to be created using the script</td>
|
---|
60 | <td valign="top" align="center">Yes</td>
|
---|
61 | </tr>
|
---|
62 | <tr>
|
---|
63 | <td valign="top">language</td>
|
---|
64 | <td valign="top">The programming language the script is written in.
|
---|
65 | Must be a supported Apache BSF language</td>
|
---|
66 | <td valign="top" align="center">Yes</td>
|
---|
67 | </tr>
|
---|
68 | <tr>
|
---|
69 | <td valign="top">src</td>
|
---|
70 | <td valign="top">The location of the script as a file, if not inline</td>
|
---|
71 | <td valign="top" align="center">No</td>
|
---|
72 | </tr>
|
---|
73 | <tr>
|
---|
74 | <td valign="top">uri</td>
|
---|
75 | <td valign="top">
|
---|
76 | The XML namespace uri that this definition should live in.
|
---|
77 | </td>
|
---|
78 | <td valign="top" align="center">No</td>
|
---|
79 | </tr>
|
---|
80 | </table>
|
---|
81 |
|
---|
82 | <h3>Nested elements</h3>
|
---|
83 | <h4>attribute</h4>
|
---|
84 | <table border="1" cellpadding="2" cellspacing="0">
|
---|
85 | <tr>
|
---|
86 | <td valign="top"><b>Attribute</b></td>
|
---|
87 | <td valign="top"><b>Description</b></td>
|
---|
88 | <td align="center" valign="top"><b>Required</b></td>
|
---|
89 | </tr>
|
---|
90 | <tr>
|
---|
91 | <td valign="top">name</td>
|
---|
92 | <td valign="top">the name of the attribute</td>
|
---|
93 | <td valign="top" align="center">Yes</td>
|
---|
94 | </tr>
|
---|
95 | </table>
|
---|
96 |
|
---|
97 | <h4>element</h4>
|
---|
98 | <table border="1" cellpadding="2" cellspacing="0">
|
---|
99 | <tr>
|
---|
100 | <td valign="top"><b>Attribute</b></td>
|
---|
101 | <td valign="top"><b>Description</b></td>
|
---|
102 | <td align="center" valign="top"><b>Required</b></td>
|
---|
103 | </tr>
|
---|
104 | <tr>
|
---|
105 | <td valign="top">name</td>
|
---|
106 | <td valign="top">the name of the nested element to be supported by the
|
---|
107 | task defined by the script</td>
|
---|
108 | <td valign="top" align="center">Yes</td>
|
---|
109 | </tr>
|
---|
110 | <tr>
|
---|
111 | <td valign="top">classname</td>
|
---|
112 | <td valign="top">the classname of the class to be used for the nested element.
|
---|
113 | This specifies the class directly and is an alternative to specifying
|
---|
114 | the Ant type name.</td>
|
---|
115 | <td valign="top" align="center">No</td>
|
---|
116 | </tr>
|
---|
117 | <tr>
|
---|
118 | <td valign="top">type</td>
|
---|
119 | <td valign="top">This is the name of an Ant task or type which is to
|
---|
120 | be used when this element is to be created. This is an alternative
|
---|
121 | to specifying the class name directly</td>
|
---|
122 | <td valign="top" align="center">No</td>
|
---|
123 | </tr>
|
---|
124 | </table>
|
---|
125 |
|
---|
126 |
|
---|
127 |
|
---|
128 | <h3>Examples</h3>
|
---|
129 |
|
---|
130 | <p>
|
---|
131 | The following definition creates a task which supports an attribute called
|
---|
132 | attr and two nested elements, one being a fileset and the other a path. When
|
---|
133 | executed, the resulting task logs the value of the attribute and the basedir
|
---|
134 | of the first fileset.
|
---|
135 | </p>
|
---|
136 |
|
---|
137 | <pre>
|
---|
138 | <scriptdef name="scripttest" language="javascript">
|
---|
139 | <attribute name="attr1"/>
|
---|
140 | <element name="fileset" type="fileset"/>
|
---|
141 | <element name="path" type="path"/>
|
---|
142 | <![CDATA[
|
---|
143 |
|
---|
144 | project.log("Hello from script");
|
---|
145 | project.log("Attribute attr1 = " + attributes.get("attr1"));
|
---|
146 | project.log("First fileset basedir = "
|
---|
147 | + elements.get("fileset").get(0).getDir(project));
|
---|
148 |
|
---|
149 | ]]>
|
---|
150 | </scriptdef>
|
---|
151 |
|
---|
152 | <scripttest attr1="test">
|
---|
153 | <path>
|
---|
154 | <pathelement location="src"/>
|
---|
155 | </path>
|
---|
156 | <fileset dir="src"/>
|
---|
157 | <fileset dir="main"/>
|
---|
158 | </scripttest>
|
---|
159 | </pre>
|
---|
160 |
|
---|
161 | <p>
|
---|
162 | The following variation on the above script lists the number of fileset elements
|
---|
163 | and iterates through them
|
---|
164 | </p>
|
---|
165 | <pre>
|
---|
166 | <scriptdef name="scripttest2" language="javascript">
|
---|
167 | <element name="fileset" type="fileset"/>
|
---|
168 | <![CDATA[
|
---|
169 | filesets = elements.get("fileset");
|
---|
170 | project.log("Number of filesets = " + filesets.size());
|
---|
171 | for (i = 0; i < filesets.size(); ++i) {
|
---|
172 | project.log("fileset " + i + " basedir = "
|
---|
173 | + filesets.get(i).getDir(project));
|
---|
174 | }
|
---|
175 | ]]>
|
---|
176 | </scriptdef>
|
---|
177 |
|
---|
178 | <scripttest2>
|
---|
179 | <fileset dir="src"/>
|
---|
180 | <fileset dir="main"/>
|
---|
181 | </scripttest2>
|
---|
182 | </pre>
|
---|
183 |
|
---|
184 | <p>
|
---|
185 | When a script has a syntax error, the scriptdef name will be listed in the
|
---|
186 | error. For example in the above script, removing the closing curly bracket
|
---|
187 | would result in this error
|
---|
188 | </p>
|
---|
189 |
|
---|
190 | <p><code>build.xml:15: SyntaxError: missing } in compound
|
---|
191 | statement (scriptdef <code><scripttest2></code>; line 10)</code></p>
|
---|
192 |
|
---|
193 | <p>
|
---|
194 | Script errors are only detected when a script task is actually executed.
|
---|
195 | </p>
|
---|
196 |
|
---|
197 |
|
---|
198 | <hr>
|
---|
199 | <p align="center">Copyright © 2000-2005 The Apache Software Foundation. All rights
|
---|
200 | Reserved.</p>
|
---|
201 |
|
---|
202 | </body>
|
---|
203 | </html>
|
---|