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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. <h3>Parameters</h3>
  27. <table border="1" cellpadding="2" cellspacing="0">
  28. <tr>
  29. <td valign="top"><b>Attribute</b></td>
  30. <td valign="top"><b>Description</b></td>
  31. <td align="center" valign="top"><b>Required</b></td>
  32. </tr>
  33. <tr>
  34. <td valign="top">language</td>
  35. <td valign="top">The programming language the script is written in.
  36. Must be a supported Apache BSF language</td>
  37. <td valign="top" align="center">Yes</td>
  38. </tr>
  39. <tr>
  40. <td valign="top">src</td>
  41. <td valign="top">The location of the script as a file, if not inline</td>
  42. <td valign="top" align="center">No</td>
  43. </tr>
  44. </table>
  45. <h3>Examples</h3>
  46. <blockquote><pre>
  47. &lt;project name=&quot;squares&quot; default=&quot;main&quot; basedir=&quot;.&quot;&gt;
  48. &lt;target name=&quot;setup&quot;&gt;
  49. &lt;script language=&quot;javascript&quot;&gt; &lt;![CDATA[
  50. for (i=1; i&lt;=10; i++) {
  51. echo = squares.createTask(&quot;echo&quot;);
  52. main.addTask(echo);
  53. echo.setMessage(i*i);
  54. }
  55. ]]&gt; &lt;/script&gt;
  56. &lt;/target&gt;
  57. &lt;target name=&quot;main&quot; depends=&quot;setup&quot;/&gt;
  58. &lt;/project&gt;
  59. </pre></blockquote>
  60. <p>generates</p>
  61. <blockquote><pre>
  62. setup:
  63. main:
  64. 1
  65. 4
  66. 9
  67. 16
  68. 25
  69. 36
  70. 49
  71. 64
  72. 81
  73. 100
  74. BUILD SUCCESSFUL
  75. </pre></blockquote>
  76. <p>Another example, using <a href="../using.html#references">references by id</a>
  77. and two different scripting languages:</p>
  78. <blockquote><pre>
  79. &lt;project name=&quot;testscript&quot; default=&quot;main&quot;&gt;
  80. &lt;target name=&quot;sub&quot;&gt;
  81. &lt;echo id=&quot;theEcho&quot;/&gt;
  82. &lt;/target&gt;
  83. &lt;target name=&quot;sub1&quot;&gt;
  84. &lt;script language=&quot;netrexx&quot;&gt;&lt;![CDATA[
  85. theEcho.setMessage(&quot;In sub1&quot;)
  86. sub.execute
  87. ]]&gt;&lt;/script&gt;
  88. &lt;/target&gt;
  89. &lt;target name=&quot;sub2&quot;&gt;
  90. &lt;script language=&quot;javascript&quot;&gt;&lt;![CDATA[
  91. theEcho.setMessage(&quot;In sub2&quot;);
  92. sub.execute();
  93. ]]&gt;&lt;/script&gt;
  94. &lt;/target&gt;
  95. &lt;target name=&quot;main&quot; depends=&quot;sub1,sub2&quot;/&gt;
  96. &lt;/project&gt;
  97. </pre></blockquote>
  98. <p>generates</p>
  99. <blockquote><pre>
  100. sub1:
  101. In sub1
  102. sub2:
  103. In sub2
  104. main:
  105. BUILD SUCCESSFUL
  106. </pre></blockquote>
  107. <p>Now a more complex example using the Java API and the Ant API. The goal is to list the
  108. filesizes of all files a &lt;fileset/&gt; caught.</p>
  109. <blockquote><pre>
  110. &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  111. &lt;project name="<font color=blue>MyProject</font>" basedir="." default="main"&gt;
  112. &lt;property name="fs.dir" value="src"/&gt;
  113. &lt;property name="fs.includes" value="**/*.txt"/&gt;
  114. &lt;property name="fs.excludes" value="**/*.tmp"/&gt;
  115. &lt;target name="main"&gt;
  116. &lt;script language="javascript"&gt; &lt;![CDATA[
  117. // import statements
  118. <font color=blue>// importPackage(java.io)</font>;
  119. <font color=blue>importClass(java.io.File)</font>;
  120. // Access to Ant-Properties by their names
  121. dir = <font color=blue>project</font>.getProperty("fs.dir");
  122. includes = <font color=blue>MyProject</font>.getProperty("fs.includes");
  123. excludes = <font color=blue>self.getProject()</font> .<font color=blue>getProperty("fs.excludes")</font>;
  124. // Create a &lt;fileset dir="" includes="" /&gt;
  125. fs = project.<font color=blue>createDataType("fileset")</font>;
  126. fs.setDir( new File(dir) );
  127. <font color=blue>fs.setIncludes(includes)</font>;
  128. fs.setExcludes(excludes);
  129. // Get the files of that fileset
  130. ds = fs.getDirectoryScanner(project);
  131. // Get the source files (array)
  132. srcFiles = ds.getIncludedFiles();
  133. // iterate over that array
  134. for (i=0; i&lt;srcFiles.length; i++) {
  135. // get the values via Java API
  136. var basedir = fs.getDir(project);
  137. var filename = srcFiles[i];
  138. var file = <font color=blue>new File(basedir, filename)</font>;
  139. var size = file.length();
  140. // create and use a Task via Ant API
  141. echo = MyProject.<font color=blue>createTask("echo")</font>;
  142. echo.setMessage(filename + ": " + size + " byte");
  143. echo.<font color=blue>perform()</font>;
  144. }
  145. ]]&gt;&lt;/script&gt;
  146. &lt;/target&gt;
  147. &lt;/project&gt;
  148. </pre></blockquote>
  149. <p>We want to use the Java API. Because we don�t want always typing the package signature
  150. we do an import. Rhino knows to different methods for import statements: one for packages
  151. and one for a single class. <br>
  152. The &lt;script&gt; task populates the Project instance under
  153. the name <i>project</i>, so we can use that reference. Another way is to use its given name
  154. or getting its reference from the task itself.<br>
  155. The Project provides methods for accessing and setting properties, creating DataTypes and
  156. Tasks and much more.<br>
  157. After creating a FileSet object we initialize that by calling its set-methods. Then we can
  158. use that object like a normal Ant task (&lt;copy&gt; for example).<br>
  159. For getting the size of a file we instantiate a <code>java.io.File</code>. So we are using
  160. normal Java API here.<br>
  161. Finally we use the &lt;echo&gt; task for producing the output. The task is not executed by
  162. its execute() method, because the perform() method (implemented in Task itself) does the
  163. apropriate logging before and after invoking execute().
  164. </p>
  165. <hr>
  166. <p align="center">Copyright &copy; 2000-2003 Apache Software Foundation. All rights
  167. Reserved.</p>
  168. </body>
  169. </html>