source: release-kits/lirk3/bin/ant-installer/web/manual1.6.2/manual/OptionalTasks/script.html@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 7.7 KB
Line 
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.
16See <a href="../install.html#librarydependencies">Library Dependencies</a> for more information.</p>
17<p>All items (tasks, targets, etc) of the running project are
18accessible from the script, using either their <code>name</code> or
19<code>id</code> attributes (as long as their names are considered
20valid Java identifiers, that is).
21The name "project" is a pre-defined reference to the Project, which can be
22used instead of the project name. The name "self" is a pre-defined reference to the actual
23&lt;script&gt;-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">
28http://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
31create an object which implements several interfaces, extends classes and for which you
32can overwrite methods. Because this is an undocumented feature (yet), here is the link
33to 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">
34Groups@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&lt;project name=&quot;squares&quot; default=&quot;main&quot; basedir=&quot;.&quot;&gt;
61
62 &lt;target name=&quot;setup&quot;&gt;
63
64 &lt;script language=&quot;javascript&quot;&gt; &lt;![CDATA[
65
66 for (i=1; i&lt;=10; i++) {
67 echo = squares.createTask(&quot;echo&quot;);
68 main.addTask(echo);
69 echo.setMessage(i*i);
70 }
71
72 ]]&gt; &lt;/script&gt;
73
74 &lt;/target&gt;
75
76 &lt;target name=&quot;main&quot; depends=&quot;setup&quot;/&gt;
77
78&lt;/project&gt;
79</pre></blockquote>
80<p>generates</p>
81<blockquote><pre>
82setup:
83
84main:
851
864
879
8816
8925
9036
9149
9264
9381
94100
95
96BUILD SUCCESSFUL
97</pre></blockquote>
98<p>Another example, using <a href="../using.html#references">references by id</a>
99and two different scripting languages:</p>
100<blockquote><pre>
101&lt;project name=&quot;testscript&quot; default=&quot;main&quot;&gt;
102 &lt;target name=&quot;sub&quot;&gt;
103 &lt;echo id=&quot;theEcho&quot;/&gt;
104 &lt;/target&gt;
105
106 &lt;target name=&quot;sub1&quot;&gt;
107 &lt;script language=&quot;netrexx&quot;&gt;&lt;![CDATA[
108 theEcho.setMessage(&quot;In sub1&quot;)
109 sub.execute
110 ]]&gt;&lt;/script&gt;
111 &lt;/target&gt;
112
113 &lt;target name=&quot;sub2&quot;&gt;
114 &lt;script language=&quot;javascript&quot;&gt;&lt;![CDATA[
115 theEcho.setMessage(&quot;In sub2&quot;);
116 sub.execute();
117 ]]&gt;&lt;/script&gt;
118 &lt;/target&gt;
119
120 &lt;target name=&quot;main&quot; depends=&quot;sub1,sub2&quot;/&gt;
121&lt;/project&gt;
122</pre></blockquote>
123<p>generates</p>
124<blockquote><pre>
125sub1:
126In sub1
127
128sub2:
129In sub2
130
131main:
132
133BUILD 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
137filesizes of all files a &lt;fileset/&gt; caught.</p>
138<blockquote><pre>
139
140&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
141&lt;project name="<font color=blue>MyProject</font>" basedir="." default="main"&gt;
142
143 &lt;property name="fs.dir" value="src"/&gt;
144 &lt;property name="fs.includes" value="**/*.txt"/&gt;
145 &lt;property name="fs.excludes" value="**/*.tmp"/&gt;
146
147 &lt;target name="main"&gt;
148 &lt;script language="javascript"&gt; &lt;![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 &lt;fileset dir="" includes="" /&gt;
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&lt;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 ]]&gt;&lt;/script&gt;
184 &lt;/target&gt;
185&lt;/project&gt;
186</pre></blockquote>
187<p>We want to use the Java API. Because we donŽt want always typing the package signature
188we do an import. Rhino knows two different methods for import statements: one for packages
189and 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>.
191For other packages you have to prefix the full classified name with <i>Package</i>.
192For 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>
195The &lt;script&gt; task populates the Project instance under
196the name <i>project</i>, so we can use that reference. Another way is to use its given name
197or getting its reference from the task itself.<br>
198The Project provides methods for accessing and setting properties, creating DataTypes and
199Tasks and much more.<br>
200After creating a FileSet object we initialize that by calling its set-methods. Then we can
201use that object like a normal Ant task (&lt;copy&gt; for example).<br>
202For getting the size of a file we instantiate a <code>java.io.File</code>. So we are using
203normal Java API here.<br>
204Finally we use the &lt;echo&gt; task for producing the output. The task is not executed by
205its execute() method, because the perform() method (implemented in Task itself) does the
206apropriate logging before and after invoking execute().
207</p>
208
209
210<hr>
211<p align="center">Copyright &copy; 2000-2004 The Apache Software Foundation. All rights
212Reserved.</p>
213
214</body>
215</html>
Note: See TracBrowser for help on using the repository browser.