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

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