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>Script Task</title>
|
---|
23 | </head>
|
---|
24 |
|
---|
25 | <body>
|
---|
26 |
|
---|
27 | <h2><a name="script">Script</a></h2>
|
---|
28 | <h3>Description</h3>
|
---|
29 | <p>Execute a script in a
|
---|
30 | <a href="http://jakarta.apache.org/bsf" target="_top">Apache BSF</a>
|
---|
31 | or
|
---|
32 | <a href="https://scripting.dev.java.net">JSR 223</a> supported language.
|
---|
33 | </p>
|
---|
34 | <p><b>Note:</b>
|
---|
35 | This task depends on external libraries not included in the Ant distribution.
|
---|
36 | See <a href="../install.html#librarydependencies">Library Dependencies</a>
|
---|
37 | for more information.
|
---|
38 | </p>
|
---|
39 | <p>
|
---|
40 | The task may use the BSF scripting manager or the JSR 223 manager that
|
---|
41 | is included in JDK6 and higher. This is controlled by the <code>manager</code>
|
---|
42 | attribute. The JSR 223 scripting manager is indicated by "javax".
|
---|
43 | </p>
|
---|
44 | <p>All items (tasks, targets, etc) of the running project are
|
---|
45 | accessible from the script, using either their <code>name</code> or
|
---|
46 | <code>id</code> attributes (as long as their names are considered
|
---|
47 | valid Java identifiers, that is).
|
---|
48 | This is controlled by the "setbeans" attribute of the task.
|
---|
49 | The name "project" is a pre-defined reference to the Project, which can be
|
---|
50 | used instead of the project name. The name "self" is a pre-defined reference to the actual
|
---|
51 | <code><script></code>-Task instance.<br>From these objects you have access to the Ant Java API, see the
|
---|
52 | <a href="../api/index.html">JavaDoc</a> (especially for
|
---|
53 | <a href="../api/org/apache/tools/ant/Project.html">Project</a> and
|
---|
54 | <a href="../api/org/apache/tools/ant/taskdefs/optional/Script.html">Script</a>) for more information.</p>
|
---|
55 | <p>If you are using JavaScript a good resource is <a target="_blank" href="http://www.mozilla.org/rhino/doc.html">
|
---|
56 | http://www.mozilla.org/rhino/doc.html</a> as we are using their JavaScript interpreter.</p>
|
---|
57 | <p>Scripts can do almost anything a task written in Java could do.</p>
|
---|
58 | <p>Rhino provides a special construct - the <i>JavaAdapter</i>. With that you can
|
---|
59 | create an object which implements several interfaces, extends classes and for which you
|
---|
60 | can overwrite methods. Because this is an undocumented feature (yet), here is the link
|
---|
61 | to an explanation: <a href="http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&newwindow=1&frame=right&th=610d2db45c0756bd&seekm=391EEC3C.5236D929%40yahoo.com#link2">
|
---|
62 | Groups@Google: "Rhino, enum.js, JavaAdapter?"</a> by Norris Boyd in the newsgroup
|
---|
63 | <i>netscape.public.mozilla.jseng</i>.</p>
|
---|
64 |
|
---|
65 |
|
---|
66 |
|
---|
67 | <h3>Parameters</h3>
|
---|
68 | <table border="1" cellpadding="2" cellspacing="0">
|
---|
69 | <tr>
|
---|
70 | <td valign="top"><b>Attribute</b></td>
|
---|
71 | <td valign="top"><b>Description</b></td>
|
---|
72 | <td align="center" valign="top"><b>Required</b></td>
|
---|
73 | </tr>
|
---|
74 | <tr>
|
---|
75 | <td valign="top">language</td>
|
---|
76 | <td valign="top">The programming language the script is written in.
|
---|
77 | Must be a supported Apache BSF or JSR 223 language</td>
|
---|
78 | <td valign="top" align="center">Yes</td>
|
---|
79 | </tr>
|
---|
80 | <tr>
|
---|
81 | <td valign="top">manager</td>
|
---|
82 | <td valign="top">
|
---|
83 | <em>Since: Ant 1.7. </em>
|
---|
84 | The script engine manager to use. This can have
|
---|
85 | one of three values ("auto", "bsf" or "javax").
|
---|
86 | The default value is "auto".
|
---|
87 | <dl>
|
---|
88 | <li>"bsf" use the BSF scripting manager to run
|
---|
89 | the language.</li>
|
---|
90 | <li>"javax" use the <em>javax.scripting</em> manager
|
---|
91 | to run the language. (This will only work for JDK6 and higher).</li>
|
---|
92 | <li>"auto" use the BSF engine if it exists,
|
---|
93 | otherwise use the <em>javax.scripting</em> manager.</li>
|
---|
94 | </dl>
|
---|
95 | </td>
|
---|
96 | <td valign="top" align="center">No</td>
|
---|
97 | </tr>
|
---|
98 | <tr>
|
---|
99 | <td valign="top">src</td>
|
---|
100 | <td valign="top">The location of the script as a file, if not inline</td>
|
---|
101 | <td valign="top" align="center">No</td>
|
---|
102 | </tr>
|
---|
103 | <tr>
|
---|
104 | <td valign="top">setbeans</td>
|
---|
105 | <td valign="top">
|
---|
106 | This attribute controls whether to set variables for
|
---|
107 | all properties, references and targets in the running script.
|
---|
108 | If this attribute is false, only the the "project" and "self" variables are set.
|
---|
109 | If this attribute is true all the variables are set. The default value of this
|
---|
110 | attribute is "true". <em>Since Ant 1.7</em>
|
---|
111 | </td>
|
---|
112 | <td valign="top" align="center">No</td>
|
---|
113 | </tr>
|
---|
114 | <tr>
|
---|
115 | <td valign="top">classpath</td>
|
---|
116 | <td valign="top">
|
---|
117 | The classpath to pass into the script. <em>Since Ant 1.7</em>
|
---|
118 | </td>
|
---|
119 | <td align="center" valign="top">No</td>
|
---|
120 | </tr>
|
---|
121 | <tr>
|
---|
122 | <td valign="top">classpathref</td>
|
---|
123 | <td valign="top">The classpath to use, given as a
|
---|
124 | <a href="../using.html#references">reference</a> to a path defined elsewhere.
|
---|
125 | <em>Since Ant 1.7</em></td>
|
---|
126 | <td align="center" valign="top">No</td>
|
---|
127 | </tr>
|
---|
128 | </table>
|
---|
129 | <h3>Parameters specified as nested elements</h3>
|
---|
130 | <h4>classpath</h4>
|
---|
131 | <p><em>Since Ant 1.7</em></p>
|
---|
132 | <p>
|
---|
133 | <code>Script</code>'s <code>classpath</code> attribute is a
|
---|
134 | <a href="../using.html#path">path-like structure</a> and can also be set via a nested
|
---|
135 | <code><classpath></code> element.
|
---|
136 | <p>
|
---|
137 | If a classpath is set, it will be used as the current thread
|
---|
138 | context classloader, and
|
---|
139 | as the classloader given to the BSF manager.
|
---|
140 | This means that it can be used to specify
|
---|
141 | the classpath containing the language implementation for BSF
|
---|
142 | or for JSR 223 managers.
|
---|
143 | This can be usefull if one wants
|
---|
144 | to keep ${user.home}/.ant/lib free of lots of scripting language
|
---|
145 | specific jar files.
|
---|
146 | </p>
|
---|
147 | <p>
|
---|
148 | <b>NB:</b> This classpath cannot (currently) be used to
|
---|
149 | specify the location of
|
---|
150 | the BSF jar file. Also, due to classloading issues, languages
|
---|
151 | that have engines in the BSF jar file cannot have the
|
---|
152 | implementation in the classpath element. This includes the
|
---|
153 | javascript, jython, netrexx and jacl languages.
|
---|
154 | </p>
|
---|
155 | </p>
|
---|
156 | <h3>Examples</h3>
|
---|
157 | The following snippet shows use of five different languages:
|
---|
158 | <blockquote><pre>
|
---|
159 | <property name="message" value="Hello world"/>
|
---|
160 |
|
---|
161 | <script language="groovy">
|
---|
162 | println("message is " + message)
|
---|
163 | </script>
|
---|
164 |
|
---|
165 | <script language="beanshell">
|
---|
166 | System.out.println("message is " + message);
|
---|
167 | </script>
|
---|
168 |
|
---|
169 | <script language="judoscript">
|
---|
170 | println 'message is ', message
|
---|
171 | </script>
|
---|
172 |
|
---|
173 | <script language="ruby">
|
---|
174 | print 'message is ', $message, "\n"
|
---|
175 | </script>
|
---|
176 |
|
---|
177 | <script language="jython">
|
---|
178 | print "message is %s" % message
|
---|
179 | </script>
|
---|
180 | </pre>
|
---|
181 | </blockquote>
|
---|
182 | <p>
|
---|
183 | Note that for the <i>jython</i> example, the script contents <b>must</b>
|
---|
184 | start on the first column.
|
---|
185 | </p>
|
---|
186 | <p>
|
---|
187 | Note also that for the <i>ruby</i> example, the names of the set variables are prefixed
|
---|
188 | by a '$'.
|
---|
189 | <p>
|
---|
190 | The following script shows a little more complicated jruby example:
|
---|
191 | </p>
|
---|
192 | <blockquote><pre>
|
---|
193 | <script language="ruby">
|
---|
194 | xmlfiles = Dir.new(".").entries.delete_if { |i| ! (i =~ /\.xml$/) }
|
---|
195 | xmlfiles.sort.each { |i| $self.log(i) }
|
---|
196 | </script>
|
---|
197 | </pre>
|
---|
198 | </blockquote>
|
---|
199 | <p>
|
---|
200 | The same example in groovy is:
|
---|
201 | </p>
|
---|
202 | <blockquote><pre>
|
---|
203 | <script language="groovy">
|
---|
204 | xmlfiles = new java.io.File(".").listFiles().findAll{ it =~ "\.xml$"}
|
---|
205 | xmlfiles.sort().each { self.log(it.toString())}
|
---|
206 | </script>
|
---|
207 | </pre>
|
---|
208 | </blockquote>
|
---|
209 | <p>
|
---|
210 | The following example shows the use of classpath to specify the location
|
---|
211 | of the beanshell jar file.
|
---|
212 | </p>
|
---|
213 | <blockquote><pre>
|
---|
214 | <script language="beanshell" setbeans="true">
|
---|
215 | <classpath>
|
---|
216 | <fileset dir="${user.home}/lang/beanshell" includes="*.jar" />
|
---|
217 | </classpath>
|
---|
218 | System.out.println("Hello world");
|
---|
219 | </script>
|
---|
220 | </pre>
|
---|
221 | </blockquote>
|
---|
222 | <p>
|
---|
223 | The following script uses javascript to create a number of
|
---|
224 | echo tasks and execute them.
|
---|
225 | </p>
|
---|
226 | <blockquote><pre>
|
---|
227 | <project name="squares" default="main" basedir=".">
|
---|
228 |
|
---|
229 | <target name="main">
|
---|
230 |
|
---|
231 | <script language="javascript"> <![CDATA[
|
---|
232 |
|
---|
233 | for (i=1; i<=10; i++) {
|
---|
234 | echo = squares.createTask("echo");
|
---|
235 | echo.setMessage(i*i);
|
---|
236 | echo.perform();
|
---|
237 | }
|
---|
238 |
|
---|
239 | ]]> </script>
|
---|
240 |
|
---|
241 | </target>
|
---|
242 |
|
---|
243 | </project>
|
---|
244 | </pre></blockquote>
|
---|
245 | <p>generates</p>
|
---|
246 | <blockquote><pre>
|
---|
247 | main:
|
---|
248 | 1
|
---|
249 | 4
|
---|
250 | 9
|
---|
251 | 16
|
---|
252 | 25
|
---|
253 | 36
|
---|
254 | 49
|
---|
255 | 64
|
---|
256 | 81
|
---|
257 | 100
|
---|
258 |
|
---|
259 | BUILD SUCCESSFUL
|
---|
260 | </pre></blockquote>
|
---|
261 |
|
---|
262 | <p>Now a more complex example using the Java API and the Ant API. The goal is to list the
|
---|
263 | filesizes of all files a <code><fileset/></code> caught.</p>
|
---|
264 | <blockquote><pre>
|
---|
265 |
|
---|
266 | <?xml version="1.0" encoding="ISO-8859-1"?>
|
---|
267 | <project name="<font color=blue>MyProject</font>" basedir="." default="main">
|
---|
268 |
|
---|
269 | <property name="fs.dir" value="src"/>
|
---|
270 | <property name="fs.includes" value="**/*.txt"/>
|
---|
271 | <property name="fs.excludes" value="**/*.tmp"/>
|
---|
272 |
|
---|
273 | <target name="main">
|
---|
274 | <script language="javascript"> <![CDATA[
|
---|
275 |
|
---|
276 | // import statements
|
---|
277 | <font color=blue>// importPackage(java.io)</font>;
|
---|
278 | <font color=blue>importClass(java.io.File)</font>;
|
---|
279 |
|
---|
280 | // Access to Ant-Properties by their names
|
---|
281 | dir = <font color=blue>project</font>.getProperty("fs.dir");
|
---|
282 | includes = <font color=blue>MyProject</font>.getProperty("fs.includes");
|
---|
283 | excludes = <font color=blue>self.getProject()</font> .<font color=blue>getProperty("fs.excludes")</font>;
|
---|
284 |
|
---|
285 | // Create a <fileset dir="" includes=""/>
|
---|
286 | fs = project.<font color=blue>createDataType("fileset")</font>;
|
---|
287 | fs.setDir( new File(dir) );
|
---|
288 | <font color=blue>fs.setIncludes(includes)</font>;
|
---|
289 | fs.setExcludes(excludes);
|
---|
290 |
|
---|
291 | // Get the files (array) of that fileset
|
---|
292 | ds = fs.getDirectoryScanner(project);
|
---|
293 | srcFiles = ds.getIncludedFiles();
|
---|
294 |
|
---|
295 | // iterate over that array
|
---|
296 | for (i=0; i<srcFiles.length; i++) {
|
---|
297 |
|
---|
298 | // get the values via Java API
|
---|
299 | var basedir = fs.getDir(project);
|
---|
300 | var filename = srcFiles[i];
|
---|
301 | var file = <font color=blue>new File(basedir, filename)</font>;
|
---|
302 | var size = file.length();
|
---|
303 |
|
---|
304 | // create and use a Task via Ant API
|
---|
305 | echo = MyProject.<font color=blue>createTask("echo")</font>;
|
---|
306 | echo.setMessage(filename + ": " + size + " byte");
|
---|
307 | echo.<font color=blue>perform()</font>;
|
---|
308 | }
|
---|
309 | ]]></script>
|
---|
310 | </target>
|
---|
311 | </project>
|
---|
312 | </pre></blockquote>
|
---|
313 | <p>We want to use the Java API. Because we don't want always typing the package signature
|
---|
314 | we do an import. Rhino knows two different methods for import statements: one for packages
|
---|
315 | and one for a single class. By default only the <i>java</i> packages are available, so
|
---|
316 | <i>java.lang.System</i> can be directly imported with <code>importClass/importPackage</code>.
|
---|
317 | For other packages you have to prefix the full classified name with <i>Packages</i>.
|
---|
318 | For example Ant's <i>FileUtils</i> class can be imported with
|
---|
319 | <code>importClass(<b>Packages</b>.org.apache.tools.ant.util.FileUtils)</code>
|
---|
320 | <br>
|
---|
321 | The <code><script></code> task populates the Project instance under
|
---|
322 | the name <i>project</i>, so we can use that reference. Another way is to use its given name
|
---|
323 | or getting its reference from the task itself.<br>
|
---|
324 | The Project provides methods for accessing and setting properties, creating DataTypes and
|
---|
325 | Tasks and much more.<br>
|
---|
326 | After creating a FileSet object we initialize that by calling its set-methods. Then we can
|
---|
327 | use that object like a normal Ant task (<code><copy></code> for example).<br>
|
---|
328 | For getting the size of a file we instantiate a <code>java.io.File</code>. So we are using
|
---|
329 | normal Java API here.<br>
|
---|
330 | Finally we use the <code><echo></code> task for producing the output. The task is not executed by
|
---|
331 | its execute() method, because the perform() method (implemented in Task itself) does the
|
---|
332 | appropriate logging before and after invoking execute().
|
---|
333 | </p>
|
---|
334 | </body>
|
---|
335 | </html>
|
---|