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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  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>. Whith 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. <blockquote><pre>
  53. &lt;project name=&quot;squares&quot; default=&quot;main&quot; basedir=&quot;.&quot;&gt;
  54. &lt;target name=&quot;setup&quot;&gt;
  55. &lt;script language=&quot;javascript&quot;&gt; &lt;![CDATA[
  56. for (i=1; i&lt;=10; i++) {
  57. echo = squares.createTask(&quot;echo&quot;);
  58. main.addTask(echo);
  59. echo.setMessage(i*i);
  60. }
  61. ]]&gt; &lt;/script&gt;
  62. &lt;/target&gt;
  63. &lt;target name=&quot;main&quot; depends=&quot;setup&quot;/&gt;
  64. &lt;/project&gt;
  65. </pre></blockquote>
  66. <p>generates</p>
  67. <blockquote><pre>
  68. setup:
  69. main:
  70. 1
  71. 4
  72. 9
  73. 16
  74. 25
  75. 36
  76. 49
  77. 64
  78. 81
  79. 100
  80. BUILD SUCCESSFUL
  81. </pre></blockquote>
  82. <p>Another example, using <a href="../using.html#references">references by id</a>
  83. and two different scripting languages:</p>
  84. <blockquote><pre>
  85. &lt;project name=&quot;testscript&quot; default=&quot;main&quot;&gt;
  86. &lt;target name=&quot;sub&quot;&gt;
  87. &lt;echo id=&quot;theEcho&quot;/&gt;
  88. &lt;/target&gt;
  89. &lt;target name=&quot;sub1&quot;&gt;
  90. &lt;script language=&quot;netrexx&quot;&gt;&lt;![CDATA[
  91. theEcho.setMessage(&quot;In sub1&quot;)
  92. sub.execute
  93. ]]&gt;&lt;/script&gt;
  94. &lt;/target&gt;
  95. &lt;target name=&quot;sub2&quot;&gt;
  96. &lt;script language=&quot;javascript&quot;&gt;&lt;![CDATA[
  97. theEcho.setMessage(&quot;In sub2&quot;);
  98. sub.execute();
  99. ]]&gt;&lt;/script&gt;
  100. &lt;/target&gt;
  101. &lt;target name=&quot;main&quot; depends=&quot;sub1,sub2&quot;/&gt;
  102. &lt;/project&gt;
  103. </pre></blockquote>
  104. <p>generates</p>
  105. <blockquote><pre>
  106. sub1:
  107. In sub1
  108. sub2:
  109. In sub2
  110. main:
  111. BUILD SUCCESSFUL
  112. </pre></blockquote>
  113. <p>Now a more complex example using the Java API and the Ant API. The goal is to list the
  114. filesizes of all files a &lt;fileset/&gt; caught.</p>
  115. <blockquote><pre>
  116. &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  117. &lt;project name="<font color=blue>MyProject</font>" basedir="." default="main"&gt;
  118. &lt;property name="fs.dir" value="src"/&gt;
  119. &lt;property name="fs.includes" value="**/*.txt"/&gt;
  120. &lt;property name="fs.excludes" value="**/*.tmp"/&gt;
  121. &lt;target name="main"&gt;
  122. &lt;script language="javascript"&gt; &lt;![CDATA[
  123. // import statements
  124. <font color=blue>// importPackage(java.io)</font>;
  125. <font color=blue>importClass(java.io.File)</font>;
  126. // Access to Ant-Properties by their names
  127. dir = <font color=blue>project</font>.getProperty("fs.dir");
  128. includes = <font color=blue>MyProject</font>.getProperty("fs.includes");
  129. excludes = <font color=blue>self.getProject()</font> .<font color=blue>getProperty("fs.excludes")</font>;
  130. // Create a &lt;fileset dir="" includes="" /&gt;
  131. fs = project.<font color=blue>createDataType("fileset")</font>;
  132. fs.setDir( new File(dir) );
  133. <font color=blue>fs.setIncludes(includes)</font>;
  134. fs.setExcludes(excludes);
  135. // Get the files (array) of that fileset
  136. ds = fs.getDirectoryScanner(project);
  137. srcFiles = ds.getIncludedFiles();
  138. // iterate over that array
  139. for (i=0; i&lt;srcFiles.length; i++) {
  140. // get the values via Java API
  141. var basedir = fs.getDir(project);
  142. var filename = srcFiles[i];
  143. var file = <font color=blue>new File(basedir, filename)</font>;
  144. var size = file.length();
  145. // create and use a Task via Ant API
  146. echo = MyProject.<font color=blue>createTask("echo")</font>;
  147. echo.setMessage(filename + ": " + size + " byte");
  148. echo.<font color=blue>perform()</font>;
  149. }
  150. ]]&gt;&lt;/script&gt;
  151. &lt;/target&gt;
  152. &lt;/project&gt;
  153. </pre></blockquote>
  154. <p>We want to use the Java API. Because we don�t want always typing the package signature
  155. we do an import. Rhino knows two different methods for import statements: one for packages
  156. and one for a single class. By default only the <i>java</i> packages are available, so
  157. <i>java.lang.System</i> can be directly imported with <code>importClass/importPackage</code>.
  158. For other packages you have to prefix the full classified name with <i>Package</i>.
  159. For example ant�s <i>FileUtil</i> class can be imported with
  160. <code>importClass(<b>Package</b>.org.apache.tools.ant.util.FileUtils)</code>
  161. <br>
  162. The &lt;script&gt; task populates the Project instance under
  163. the name <i>project</i>, so we can use that reference. Another way is to use its given name
  164. or getting its reference from the task itself.<br>
  165. The Project provides methods for accessing and setting properties, creating DataTypes and
  166. Tasks and much more.<br>
  167. After creating a FileSet object we initialize that by calling its set-methods. Then we can
  168. use that object like a normal Ant task (&lt;copy&gt; for example).<br>
  169. For getting the size of a file we instantiate a <code>java.io.File</code>. So we are using
  170. normal Java API here.<br>
  171. Finally we use the &lt;echo&gt; task for producing the output. The task is not executed by
  172. its execute() method, because the perform() method (implemented in Task itself) does the
  173. apropriate logging before and after invoking execute().
  174. </p>
  175. <hr>
  176. <p align="center">Copyright &copy; 2000-2004 The Apache Software Foundation. All rights
  177. Reserved.</p>
  178. </body>
  179. </html>