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

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