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.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. <code>&lt;script&gt;</code>-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 complicated 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. The same example in groovy is:
  88. </p>
  89. <blockquote><pre>
  90. &lt;script language="groovy"&gt;
  91. xmlfiles = new java.io.File(".").listFiles().findAll{ it =~ "\.xml$"}
  92. xmlfiles.sort().each { self.log(it.toString())}
  93. &lt;/script&gt;
  94. </pre>
  95. </blockquote>
  96. <p>
  97. The following script uses javascript to create a number of
  98. echo tasks and execute them.
  99. </p>
  100. <blockquote><pre>
  101. &lt;project name=&quot;squares&quot; default=&quot;main&quot; basedir=&quot;.&quot;&gt;
  102. &lt;target name=&quot;main&quot;&gt;
  103. &lt;script language=&quot;javascript&quot;&gt; &lt;![CDATA[
  104. for (i=1; i&lt;=10; i++) {
  105. echo = squares.createTask(&quot;echo&quot;);
  106. echo.setMessage(i*i);
  107. echo.perform();
  108. }
  109. ]]&gt; &lt;/script&gt;
  110. &lt;/target&gt;
  111. &lt;/project&gt;
  112. </pre></blockquote>
  113. <p>generates</p>
  114. <blockquote><pre>
  115. main:
  116. 1
  117. 4
  118. 9
  119. 16
  120. 25
  121. 36
  122. 49
  123. 64
  124. 81
  125. 100
  126. BUILD SUCCESSFUL
  127. </pre></blockquote>
  128. <p>Another example, using <a href="../using.html#references">references by id</a>
  129. and two different scripting languages:</p>
  130. <blockquote><pre>
  131. &lt;project name=&quot;testscript&quot; default=&quot;main&quot;&gt;
  132. &lt;target name=&quot;sub&quot;&gt;
  133. &lt;echo id=&quot;theEcho&quot;/&gt;
  134. &lt;/target&gt;
  135. &lt;target name=&quot;sub1&quot;&gt;
  136. &lt;script language=&quot;netrexx&quot;&gt;&lt;![CDATA[
  137. theEcho.setMessage(&quot;In sub1&quot;)
  138. sub.execute
  139. ]]&gt;&lt;/script&gt;
  140. &lt;/target&gt;
  141. &lt;target name=&quot;sub2&quot;&gt;
  142. &lt;script language=&quot;javascript&quot;&gt;&lt;![CDATA[
  143. theEcho.setMessage(&quot;In sub2&quot;);
  144. sub.execute();
  145. ]]&gt;&lt;/script&gt;
  146. &lt;/target&gt;
  147. &lt;target name=&quot;main&quot; depends=&quot;sub1,sub2&quot;/&gt;
  148. &lt;/project&gt;
  149. </pre></blockquote>
  150. <p>generates</p>
  151. <blockquote><pre>
  152. sub1:
  153. In sub1
  154. sub2:
  155. In sub2
  156. main:
  157. BUILD SUCCESSFUL
  158. </pre></blockquote>
  159. <p>Now a more complex example using the Java API and the Ant API. The goal is to list the
  160. filesizes of all files a <code>&lt;fileset/&gt;</code> caught.</p>
  161. <blockquote><pre>
  162. &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  163. &lt;project name="<font color=blue>MyProject</font>" basedir="." default="main"&gt;
  164. &lt;property name="fs.dir" value="src"/&gt;
  165. &lt;property name="fs.includes" value="**/*.txt"/&gt;
  166. &lt;property name="fs.excludes" value="**/*.tmp"/&gt;
  167. &lt;target name="main"&gt;
  168. &lt;script language="javascript"&gt; &lt;![CDATA[
  169. // import statements
  170. <font color=blue>// importPackage(java.io)</font>;
  171. <font color=blue>importClass(java.io.File)</font>;
  172. // Access to Ant-Properties by their names
  173. dir = <font color=blue>project</font>.getProperty("fs.dir");
  174. includes = <font color=blue>MyProject</font>.getProperty("fs.includes");
  175. excludes = <font color=blue>self.getProject()</font> .<font color=blue>getProperty("fs.excludes")</font>;
  176. // Create a &lt;fileset dir="" includes=""/&gt;
  177. fs = project.<font color=blue>createDataType("fileset")</font>;
  178. fs.setDir( new File(dir) );
  179. <font color=blue>fs.setIncludes(includes)</font>;
  180. fs.setExcludes(excludes);
  181. // Get the files (array) of that fileset
  182. ds = fs.getDirectoryScanner(project);
  183. srcFiles = ds.getIncludedFiles();
  184. // iterate over that array
  185. for (i=0; i&lt;srcFiles.length; i++) {
  186. // get the values via Java API
  187. var basedir = fs.getDir(project);
  188. var filename = srcFiles[i];
  189. var file = <font color=blue>new File(basedir, filename)</font>;
  190. var size = file.length();
  191. // create and use a Task via Ant API
  192. echo = MyProject.<font color=blue>createTask("echo")</font>;
  193. echo.setMessage(filename + ": " + size + " byte");
  194. echo.<font color=blue>perform()</font>;
  195. }
  196. ]]&gt;&lt;/script&gt;
  197. &lt;/target&gt;
  198. &lt;/project&gt;
  199. </pre></blockquote>
  200. <p>We want to use the Java API. Because we don't want always typing the package signature
  201. we do an import. Rhino knows two different methods for import statements: one for packages
  202. and one for a single class. By default only the <i>java</i> packages are available, so
  203. <i>java.lang.System</i> can be directly imported with <code>importClass/importPackage</code>.
  204. For other packages you have to prefix the full classified name with <i>Packages</i>.
  205. For example Ant's <i>FileUtils</i> class can be imported with
  206. <code>importClass(<b>Packages</b>.org.apache.tools.ant.util.FileUtils)</code>
  207. <br>
  208. The <code>&lt;script&gt;</code> task populates the Project instance under
  209. the name <i>project</i>, so we can use that reference. Another way is to use its given name
  210. or getting its reference from the task itself.<br>
  211. The Project provides methods for accessing and setting properties, creating DataTypes and
  212. Tasks and much more.<br>
  213. After creating a FileSet object we initialize that by calling its set-methods. Then we can
  214. use that object like a normal Ant task (<code>&lt;copy&gt;</code> for example).<br>
  215. For getting the size of a file we instantiate a <code>java.io.File</code>. So we are using
  216. normal Java API here.<br>
  217. Finally we use the <code>&lt;echo&gt;</code> task for producing the output. The task is not executed by
  218. its execute() method, because the perform() method (implemented in Task itself) does the
  219. appropriate logging before and after invoking execute().
  220. </p>
  221. <hr>
  222. <p align="center">Copyright &copy; 2000-2004 The Apache Software Foundation. All rights
  223. Reserved.</p>
  224. </body>
  225. </html>