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 12 KiB

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