You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

script.html 11 KiB

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