1 | <html>
|
---|
2 |
|
---|
3 | <head>
|
---|
4 | <meta http-equiv="Content-Language" content="en-us">
|
---|
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 | <script>-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>. Whith 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 | <blockquote><pre>
|
---|
60 | <project name="squares" default="main" basedir=".">
|
---|
61 |
|
---|
62 | <target name="setup">
|
---|
63 |
|
---|
64 | <script language="javascript"> <![CDATA[
|
---|
65 |
|
---|
66 | for (i=1; i<=10; i++) {
|
---|
67 | echo = squares.createTask("echo");
|
---|
68 | main.addTask(echo);
|
---|
69 | echo.setMessage(i*i);
|
---|
70 | }
|
---|
71 |
|
---|
72 | ]]> </script>
|
---|
73 |
|
---|
74 | </target>
|
---|
75 |
|
---|
76 | <target name="main" depends="setup"/>
|
---|
77 |
|
---|
78 | </project>
|
---|
79 | </pre></blockquote>
|
---|
80 | <p>generates</p>
|
---|
81 | <blockquote><pre>
|
---|
82 | setup:
|
---|
83 |
|
---|
84 | main:
|
---|
85 | 1
|
---|
86 | 4
|
---|
87 | 9
|
---|
88 | 16
|
---|
89 | 25
|
---|
90 | 36
|
---|
91 | 49
|
---|
92 | 64
|
---|
93 | 81
|
---|
94 | 100
|
---|
95 |
|
---|
96 | BUILD SUCCESSFUL
|
---|
97 | </pre></blockquote>
|
---|
98 | <p>Another example, using <a href="../using.html#references">references by id</a>
|
---|
99 | and two different scripting languages:</p>
|
---|
100 | <blockquote><pre>
|
---|
101 | <project name="testscript" default="main">
|
---|
102 | <target name="sub">
|
---|
103 | <echo id="theEcho"/>
|
---|
104 | </target>
|
---|
105 |
|
---|
106 | <target name="sub1">
|
---|
107 | <script language="netrexx"><![CDATA[
|
---|
108 | theEcho.setMessage("In sub1")
|
---|
109 | sub.execute
|
---|
110 | ]]></script>
|
---|
111 | </target>
|
---|
112 |
|
---|
113 | <target name="sub2">
|
---|
114 | <script language="javascript"><![CDATA[
|
---|
115 | theEcho.setMessage("In sub2");
|
---|
116 | sub.execute();
|
---|
117 | ]]></script>
|
---|
118 | </target>
|
---|
119 |
|
---|
120 | <target name="main" depends="sub1,sub2"/>
|
---|
121 | </project>
|
---|
122 | </pre></blockquote>
|
---|
123 | <p>generates</p>
|
---|
124 | <blockquote><pre>
|
---|
125 | sub1:
|
---|
126 | In sub1
|
---|
127 |
|
---|
128 | sub2:
|
---|
129 | In sub2
|
---|
130 |
|
---|
131 | main:
|
---|
132 |
|
---|
133 | BUILD SUCCESSFUL
|
---|
134 | </pre></blockquote>
|
---|
135 |
|
---|
136 | <p>Now a more complex example using the Java API and the Ant API. The goal is to list the
|
---|
137 | filesizes of all files a <fileset/> caught.</p>
|
---|
138 | <blockquote><pre>
|
---|
139 |
|
---|
140 | <?xml version="1.0" encoding="ISO-8859-1"?>
|
---|
141 | <project name="<font color=blue>MyProject</font>" basedir="." default="main">
|
---|
142 |
|
---|
143 | <property name="fs.dir" value="src"/>
|
---|
144 | <property name="fs.includes" value="**/*.txt"/>
|
---|
145 | <property name="fs.excludes" value="**/*.tmp"/>
|
---|
146 |
|
---|
147 | <target name="main">
|
---|
148 | <script language="javascript"> <![CDATA[
|
---|
149 |
|
---|
150 | // import statements
|
---|
151 | <font color=blue>// importPackage(java.io)</font>;
|
---|
152 | <font color=blue>importClass(java.io.File)</font>;
|
---|
153 |
|
---|
154 | // Access to Ant-Properties by their names
|
---|
155 | dir = <font color=blue>project</font>.getProperty("fs.dir");
|
---|
156 | includes = <font color=blue>MyProject</font>.getProperty("fs.includes");
|
---|
157 | excludes = <font color=blue>self.getProject()</font> .<font color=blue>getProperty("fs.excludes")</font>;
|
---|
158 |
|
---|
159 | // Create a <fileset dir="" includes="" />
|
---|
160 | fs = project.<font color=blue>createDataType("fileset")</font>;
|
---|
161 | fs.setDir( new File(dir) );
|
---|
162 | <font color=blue>fs.setIncludes(includes)</font>;
|
---|
163 | fs.setExcludes(excludes);
|
---|
164 |
|
---|
165 | // Get the files (array) of that fileset
|
---|
166 | ds = fs.getDirectoryScanner(project);
|
---|
167 | srcFiles = ds.getIncludedFiles();
|
---|
168 |
|
---|
169 | // iterate over that array
|
---|
170 | for (i=0; i<srcFiles.length; i++) {
|
---|
171 |
|
---|
172 | // get the values via Java API
|
---|
173 | var basedir = fs.getDir(project);
|
---|
174 | var filename = srcFiles[i];
|
---|
175 | var file = <font color=blue>new File(basedir, filename)</font>;
|
---|
176 | var size = file.length();
|
---|
177 |
|
---|
178 | // create and use a Task via Ant API
|
---|
179 | echo = MyProject.<font color=blue>createTask("echo")</font>;
|
---|
180 | echo.setMessage(filename + ": " + size + " byte");
|
---|
181 | echo.<font color=blue>perform()</font>;
|
---|
182 | }
|
---|
183 | ]]></script>
|
---|
184 | </target>
|
---|
185 | </project>
|
---|
186 | </pre></blockquote>
|
---|
187 | <p>We want to use the Java API. Because we donŽt want always typing the package signature
|
---|
188 | we do an import. Rhino knows two different methods for import statements: one for packages
|
---|
189 | and one for a single class. By default only the <i>java</i> packages are available, so
|
---|
190 | <i>java.lang.System</i> can be directly imported with <code>importClass/importPackage</code>.
|
---|
191 | For other packages you have to prefix the full classified name with <i>Package</i>.
|
---|
192 | For example antŽs <i>FileUtil</i> class can be imported with
|
---|
193 | <code>importClass(<b>Package</b>.org.apache.tools.ant.util.FileUtils)</code>
|
---|
194 | <br>
|
---|
195 | The <script> task populates the Project instance under
|
---|
196 | the name <i>project</i>, so we can use that reference. Another way is to use its given name
|
---|
197 | or getting its reference from the task itself.<br>
|
---|
198 | The Project provides methods for accessing and setting properties, creating DataTypes and
|
---|
199 | Tasks and much more.<br>
|
---|
200 | After creating a FileSet object we initialize that by calling its set-methods. Then we can
|
---|
201 | use that object like a normal Ant task (<copy> for example).<br>
|
---|
202 | For getting the size of a file we instantiate a <code>java.io.File</code>. So we are using
|
---|
203 | normal Java API here.<br>
|
---|
204 | Finally we use the <echo> task for producing the output. The task is not executed by
|
---|
205 | its execute() method, because the perform() method (implemented in Task itself) does the
|
---|
206 | apropriate logging before and after invoking execute().
|
---|
207 | </p>
|
---|
208 |
|
---|
209 |
|
---|
210 | <hr>
|
---|
211 | <p align="center">Copyright © 2000-2004 The Apache Software Foundation. All rights
|
---|
212 | Reserved.</p>
|
---|
213 |
|
---|
214 | </body>
|
---|
215 | </html>
|
---|