1 | <html>
|
---|
2 |
|
---|
3 | <head>
|
---|
4 | <meta http-equiv="Content-Language" content="en-us"></meta>
|
---|
5 | <title>Script 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>Execute a script in a
|
---|
14 | <a href="http://jakarta.apache.org/bsf" target="_top">Apache BSF</a> supported language.</p>
|
---|
15 | <p><b>Note:</b> This task depends on external libraries not included in the Ant distribution.
|
---|
16 | See <a href="../install.html#librarydependencies">Library Dependencies</a> for more information.</p>
|
---|
17 | <p>All items (tasks, targets, etc) of the running project are
|
---|
18 | accessible from the script, using either their <code>name</code> or
|
---|
19 | <code>id</code> attributes (as long as their names are considered
|
---|
20 | valid Java identifiers, that is).
|
---|
21 | The name "project" is a pre-defined reference to the Project, which can be
|
---|
22 | used instead of the project name. The name "self" is a pre-defined reference to the actual
|
---|
23 | <code><script></code>-Task instance.<br/>From these objects you have access to the Ant Java API, see the
|
---|
24 | <a href="../api/index.html">JavaDoc</a> (especially for
|
---|
25 | <a href="../api/org/apache/tools/ant/Project.html">Project</a> and
|
---|
26 | <a href="../api/org/apache/tools/ant/taskdefs/optional/Script.html">Script</a>) for more information.</p>
|
---|
27 | <p>If you are using JavaScript a good resource is <a target="_blank" href="http://www.mozilla.org/rhino/doc.html">
|
---|
28 | http://www.mozilla.org/rhino/doc.html</a> as we are using their JavaScript interpreter.</p>
|
---|
29 | <p>Scripts can do almost anything a task written in Java could do.</p>
|
---|
30 | <p>Rhino provides a special construct - the <i>JavaAdapter</i>. With that you can
|
---|
31 | create an object which implements several interfaces, extends classes and for which you
|
---|
32 | can overwrite methods. Because this is an undocumented feature (yet), here is the link
|
---|
33 | 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">
|
---|
34 | Groups@Google: "Rhino, enum.js, JavaAdapter?"</a> by Norris Boyd in the newsgroup
|
---|
35 | <i>netscape.public.mozilla.jseng</i>.</p>
|
---|
36 |
|
---|
37 |
|
---|
38 |
|
---|
39 | <h3>Parameters</h3>
|
---|
40 | <table border="1" cellpadding="2" cellspacing="0">
|
---|
41 | <tr>
|
---|
42 | <td valign="top"><b>Attribute</b></td>
|
---|
43 | <td valign="top"><b>Description</b></td>
|
---|
44 | <td align="center" valign="top"><b>Required</b></td>
|
---|
45 | </tr>
|
---|
46 | <tr>
|
---|
47 | <td valign="top">language</td>
|
---|
48 | <td valign="top">The programming language the script is written in.
|
---|
49 | Must be a supported Apache BSF language</td>
|
---|
50 | <td valign="top" align="center">Yes</td>
|
---|
51 | </tr>
|
---|
52 | <tr>
|
---|
53 | <td valign="top">src</td>
|
---|
54 | <td valign="top">The location of the script as a file, if not inline</td>
|
---|
55 | <td valign="top" align="center">No</td>
|
---|
56 | </tr>
|
---|
57 | </table>
|
---|
58 | <h3>Examples</h3>
|
---|
59 | The following snippet shows use of five different languages:
|
---|
60 | <blockquote><pre>
|
---|
61 | <property name="message" value="Hello world"/>
|
---|
62 |
|
---|
63 | <script language="groovy">
|
---|
64 | println("message is " + message)
|
---|
65 | </script>
|
---|
66 |
|
---|
67 | <script language="beanshell">
|
---|
68 | System.out.println("message is " + message);
|
---|
69 | </script>
|
---|
70 |
|
---|
71 | <script language="judoscript">
|
---|
72 | println 'message is ', message
|
---|
73 | </script>
|
---|
74 |
|
---|
75 | <script language="ruby">
|
---|
76 | print 'message is ', $message, "\n"
|
---|
77 | </script>
|
---|
78 |
|
---|
79 | <script language="jython">
|
---|
80 | print "message is %s" % message
|
---|
81 | </script>
|
---|
82 | </pre>
|
---|
83 | </blockquote>
|
---|
84 | <p>
|
---|
85 | Note that for the <i>jython</i> example, the script contents <b>must</b>
|
---|
86 | start on the first column.
|
---|
87 | </p>
|
---|
88 | <p>
|
---|
89 | The following script shows a little more complicated jruby example:
|
---|
90 | </p>
|
---|
91 | <blockquote><pre>
|
---|
92 | <script language="ruby">
|
---|
93 | xmlfiles = Dir.new(".").entries.delete_if { |i| ! (i =~ /\.xml$/) }
|
---|
94 | xmlfiles.sort.each { |i| $self.log(i) }
|
---|
95 | </script>
|
---|
96 | </pre>
|
---|
97 | </blockquote>
|
---|
98 | <p>
|
---|
99 | The same example in groovy is:
|
---|
100 | </p>
|
---|
101 | <blockquote><pre>
|
---|
102 | <script language="groovy">
|
---|
103 | xmlfiles = new java.io.File(".").listFiles().findAll{ it =~ "\.xml$"}
|
---|
104 | xmlfiles.sort().each { self.log(it.toString())}
|
---|
105 | </script>
|
---|
106 | </pre>
|
---|
107 | </blockquote>
|
---|
108 | <p>
|
---|
109 | The following script uses javascript to create a number of
|
---|
110 | echo tasks and execute them.
|
---|
111 | </p>
|
---|
112 | <blockquote><pre>
|
---|
113 | <project name="squares" default="main" basedir=".">
|
---|
114 |
|
---|
115 | <target name="main">
|
---|
116 |
|
---|
117 | <script language="javascript"> <![CDATA[
|
---|
118 |
|
---|
119 | for (i=1; i<=10; i++) {
|
---|
120 | echo = squares.createTask("echo");
|
---|
121 | echo.setMessage(i*i);
|
---|
122 | echo.perform();
|
---|
123 | }
|
---|
124 |
|
---|
125 | ]]> </script>
|
---|
126 |
|
---|
127 | </target>
|
---|
128 |
|
---|
129 | </project>
|
---|
130 | </pre></blockquote>
|
---|
131 | <p>generates</p>
|
---|
132 | <blockquote><pre>
|
---|
133 | main:
|
---|
134 | 1
|
---|
135 | 4
|
---|
136 | 9
|
---|
137 | 16
|
---|
138 | 25
|
---|
139 | 36
|
---|
140 | 49
|
---|
141 | 64
|
---|
142 | 81
|
---|
143 | 100
|
---|
144 |
|
---|
145 | BUILD SUCCESSFUL
|
---|
146 | </pre></blockquote>
|
---|
147 | <p>Another example, using <a href="../using.html#references">references by id</a>
|
---|
148 | and two different scripting languages:</p>
|
---|
149 | <blockquote><pre>
|
---|
150 | <project name="testscript" default="main">
|
---|
151 | <target name="sub">
|
---|
152 | <echo id="theEcho"/>
|
---|
153 | </target>
|
---|
154 |
|
---|
155 | <target name="sub1">
|
---|
156 | <script language="netrexx"><![CDATA[
|
---|
157 | theEcho.setMessage("In sub1")
|
---|
158 | sub.execute
|
---|
159 | ]]></script>
|
---|
160 | </target>
|
---|
161 |
|
---|
162 | <target name="sub2">
|
---|
163 | <script language="javascript"><![CDATA[
|
---|
164 | theEcho.setMessage("In sub2");
|
---|
165 | sub.execute();
|
---|
166 | ]]></script>
|
---|
167 | </target>
|
---|
168 |
|
---|
169 | <target name="main" depends="sub1,sub2"/>
|
---|
170 | </project>
|
---|
171 | </pre></blockquote>
|
---|
172 | <p>generates</p>
|
---|
173 | <blockquote><pre>
|
---|
174 | sub1:
|
---|
175 | In sub1
|
---|
176 |
|
---|
177 | sub2:
|
---|
178 | In sub2
|
---|
179 |
|
---|
180 | main:
|
---|
181 |
|
---|
182 | BUILD SUCCESSFUL
|
---|
183 | </pre></blockquote>
|
---|
184 |
|
---|
185 | <p>Now a more complex example using the Java API and the Ant API. The goal is to list the
|
---|
186 | filesizes of all files a <code><fileset/></code> caught.</p>
|
---|
187 | <blockquote><pre>
|
---|
188 |
|
---|
189 | <?xml version="1.0" encoding="ISO-8859-1"?>
|
---|
190 | <project name="<font color=blue>MyProject</font>" basedir="." default="main">
|
---|
191 |
|
---|
192 | <property name="fs.dir" value="src"/>
|
---|
193 | <property name="fs.includes" value="**/*.txt"/>
|
---|
194 | <property name="fs.excludes" value="**/*.tmp"/>
|
---|
195 |
|
---|
196 | <target name="main">
|
---|
197 | <script language="javascript"> <![CDATA[
|
---|
198 |
|
---|
199 | // import statements
|
---|
200 | <font color=blue>// importPackage(java.io)</font>;
|
---|
201 | <font color=blue>importClass(java.io.File)</font>;
|
---|
202 |
|
---|
203 | // Access to Ant-Properties by their names
|
---|
204 | dir = <font color=blue>project</font>.getProperty("fs.dir");
|
---|
205 | includes = <font color=blue>MyProject</font>.getProperty("fs.includes");
|
---|
206 | excludes = <font color=blue>self.getProject()</font> .<font color=blue>getProperty("fs.excludes")</font>;
|
---|
207 |
|
---|
208 | // Create a <fileset dir="" includes=""/>
|
---|
209 | fs = project.<font color=blue>createDataType("fileset")</font>;
|
---|
210 | fs.setDir( new File(dir) );
|
---|
211 | <font color=blue>fs.setIncludes(includes)</font>;
|
---|
212 | fs.setExcludes(excludes);
|
---|
213 |
|
---|
214 | // Get the files (array) of that fileset
|
---|
215 | ds = fs.getDirectoryScanner(project);
|
---|
216 | srcFiles = ds.getIncludedFiles();
|
---|
217 |
|
---|
218 | // iterate over that array
|
---|
219 | for (i=0; i<srcFiles.length; i++) {
|
---|
220 |
|
---|
221 | // get the values via Java API
|
---|
222 | var basedir = fs.getDir(project);
|
---|
223 | var filename = srcFiles[i];
|
---|
224 | var file = <font color=blue>new File(basedir, filename)</font>;
|
---|
225 | var size = file.length();
|
---|
226 |
|
---|
227 | // create and use a Task via Ant API
|
---|
228 | echo = MyProject.<font color=blue>createTask("echo")</font>;
|
---|
229 | echo.setMessage(filename + ": " + size + " byte");
|
---|
230 | echo.<font color=blue>perform()</font>;
|
---|
231 | }
|
---|
232 | ]]></script>
|
---|
233 | </target>
|
---|
234 | </project>
|
---|
235 | </pre></blockquote>
|
---|
236 | <p>We want to use the Java API. Because we don't want always typing the package signature
|
---|
237 | we do an import. Rhino knows two different methods for import statements: one for packages
|
---|
238 | and one for a single class. By default only the <i>java</i> packages are available, so
|
---|
239 | <i>java.lang.System</i> can be directly imported with <code>importClass/importPackage</code>.
|
---|
240 | For other packages you have to prefix the full classified name with <i>Packages</i>.
|
---|
241 | For example Ant's <i>FileUtils</i> class can be imported with
|
---|
242 | <code>importClass(<b>Packages</b>.org.apache.tools.ant.util.FileUtils)</code>
|
---|
243 | <br>
|
---|
244 | The <code><script></code> task populates the Project instance under
|
---|
245 | the name <i>project</i>, so we can use that reference. Another way is to use its given name
|
---|
246 | or getting its reference from the task itself.<br>
|
---|
247 | The Project provides methods for accessing and setting properties, creating DataTypes and
|
---|
248 | Tasks and much more.<br>
|
---|
249 | After creating a FileSet object we initialize that by calling its set-methods. Then we can
|
---|
250 | use that object like a normal Ant task (<code><copy></code> for example).<br>
|
---|
251 | For getting the size of a file we instantiate a <code>java.io.File</code>. So we are using
|
---|
252 | normal Java API here.<br>
|
---|
253 | Finally we use the <code><echo></code> task for producing the output. The task is not executed by
|
---|
254 | its execute() method, because the perform() method (implemented in Task itself) does the
|
---|
255 | appropriate logging before and after invoking execute().
|
---|
256 | </p>
|
---|
257 |
|
---|
258 |
|
---|
259 | <hr>
|
---|
260 | <p align="center">Copyright © 2000-2004 The Apache Software Foundation. All rights
|
---|
261 | Reserved.</p>
|
---|
262 |
|
---|
263 | </body>
|
---|
264 | </html>
|
---|