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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <!--
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. -->
  15. <html>
  16. <head>
  17. <meta http-equiv="Content-Language" content="en-us"></meta>
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>Script Task</title>
  20. </head>
  21. <body>
  22. <h2><a name="script">Script</a></h2>
  23. <h3>Description</h3>
  24. <p>Execute a script in a
  25. <a href="http://jakarta.apache.org/bsf" target="_top">Apache BSF</a>
  26. or
  27. <a href="https://scripting.dev.java.net">JSR 223</a> supported language.
  28. </p>
  29. <p><b>Note:</b>
  30. This task depends on external libraries not included in the Apache Ant distribution.
  31. See <a href="../install.html#librarydependencies">Library Dependencies</a>
  32. for more information.
  33. </p>
  34. <p>
  35. The task may use the BSF scripting manager or the JSR 223 manager that
  36. is included in JDK6 and higher. This is controlled by the <code>manager</code>
  37. attribute. The JSR 223 scripting manager is indicated by "javax".
  38. </p>
  39. <p>All items (tasks, targets, etc) of the running project are
  40. accessible from the script, using either their <code>name</code> or
  41. <code>id</code> attributes (as long as their names are considered
  42. valid Java identifiers, that is).
  43. This is controlled by the "setbeans" attribute of the task.
  44. The name "project" is a pre-defined reference to the Project, which can be
  45. used instead of the project name. The name "self" is a pre-defined reference to the actual
  46. <code>&lt;script&gt;</code>-Task instance.<br>From these objects you have access to the Ant Java API, see the
  47. <a href="../api/index.html">JavaDoc</a> (especially for
  48. <a href="../api/org/apache/tools/ant/Project.html">Project</a> and
  49. <a href="../api/org/apache/tools/ant/taskdefs/optional/Script.html">Script</a>) for more information.</p>
  50. <p>If you are using JavaScript under BSF, a good resource is <a target="_blank" href="http://www.mozilla.org/rhino/doc.html">
  51. http://www.mozilla.org/rhino/doc.html</a> as we are using their JavaScript interpreter.</p>
  52. <p>Scripts can do almost anything a task written in Java could do.</p>
  53. <p>Rhino provides a special construct - the <i>JavaAdapter</i>. With that you can
  54. create an object which implements several interfaces, extends classes and for which you
  55. can overwrite methods. Because this is an undocumented feature (yet), here is the link
  56. 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">
  57. Groups@Google: "Rhino, enum.js, JavaAdapter?"</a> by Norris Boyd in the newsgroup
  58. <i>netscape.public.mozilla.jseng</i>.</p>
  59. <p>If you are creating Targets programmatically, make sure you set the
  60. Location to a useful value. In particular all targets should have
  61. different location values.</p>
  62. <h3>Parameters</h3>
  63. <table border="1" cellpadding="2" cellspacing="0">
  64. <tr>
  65. <td valign="top"><b>Attribute</b></td>
  66. <td valign="top"><b>Description</b></td>
  67. <td align="center" valign="top"><b>Required</b></td>
  68. </tr>
  69. <tr>
  70. <td valign="top">language</td>
  71. <td valign="top">The programming language the script is written in.
  72. Must be a supported Apache BSF or JSR 223 language</td>
  73. <td valign="top" align="center">Yes</td>
  74. </tr>
  75. <tr>
  76. <td valign="top">manager</td>
  77. <td valign="top">
  78. <em>Since: Ant 1.7. </em>
  79. The script engine manager to use. This can have
  80. one of three values ("auto", "bsf" or "javax").
  81. The default value is "auto".
  82. <dl>
  83. <li>"bsf" use the BSF scripting manager to run
  84. the language.</li>
  85. <li>"javax" use the <em>javax.scripting</em> manager
  86. to run the language. (This will only work for JDK6 and higher).</li>
  87. <li>"auto" use the BSF engine if it exists,
  88. otherwise use the <em>javax.scripting</em> manager.</li>
  89. </dl>
  90. </td>
  91. <td valign="top" align="center">No</td>
  92. </tr>
  93. <tr>
  94. <td valign="top">src</td>
  95. <td valign="top">The location of the script as a file, if not inline</td>
  96. <td valign="top" align="center">No</td>
  97. </tr>
  98. <tr>
  99. <td valign="top">encoding</td>
  100. <td valign="top">The encoding of the script as a file. <em>since Ant 1.10.2.</em></td>
  101. <td valign="top" align="center">No - defaults to default JVM encoding</td>
  102. </tr>
  103. <tr>
  104. <td valign="top">setbeans</td>
  105. <td valign="top">
  106. This attribute controls whether to set variables for
  107. all properties, references and targets in the running script.
  108. If this attribute is false, only the the "project" and "self" variables are set.
  109. If this attribute is true all the variables are set. The default value of this
  110. attribute is "true". <em>Since Ant 1.7</em>
  111. </td>
  112. <td valign="top" align="center">No</td>
  113. </tr>
  114. <tr>
  115. <td valign="top">classpath</td>
  116. <td valign="top">
  117. The classpath to pass into the script. <em>Since Ant 1.7</em>
  118. </td>
  119. <td align="center" valign="top">No</td>
  120. </tr>
  121. <tr>
  122. <td valign="top">classpathref</td>
  123. <td valign="top">The classpath to use, given as a
  124. <a href="../using.html#references">reference</a> to a path defined elsewhere.
  125. <em>Since Ant 1.7</em></td>
  126. <td align="center" valign="top">No</td>
  127. </tr>
  128. </table>
  129. <h3>Parameters specified as nested elements</h3>
  130. <h4>classpath</h4>
  131. <p><em>Since Ant 1.7</em></p>
  132. <p>
  133. <code>Script</code>'s <code>classpath</code> attribute is a
  134. <a href="../using.html#path">path-like structure</a> and can also be set via a nested
  135. <code>&lt;classpath&gt;</code> element.
  136. <p>
  137. If a classpath is set, it will be used as the current thread
  138. context classloader, and
  139. as the classloader given to the BSF manager.
  140. This means that it can be used to specify
  141. the classpath containing the language implementation for BSF
  142. or for JSR 223 managers.
  143. This can be useful if one wants
  144. to keep ${user.home}/.ant/lib free of lots of scripting language
  145. specific jar files.
  146. </p>
  147. <p>
  148. <b>NB: (Since Ant 1.7.1)</b>
  149. This classpath <em>can</em> be used to
  150. specify the location of
  151. the BSF jar file and/or languages
  152. that have engines in the BSF jar file. This includes the
  153. javascript, jython, netrexx and jacl languages.
  154. </p>
  155. </p>
  156. <h3>Examples</h3>
  157. The following snippet shows use of five different languages:
  158. <blockquote><pre>
  159. &lt;property name="message" value="Hello world"/&gt;
  160. &lt;script language="groovy"&gt;
  161. println("message is " + message)
  162. &lt;/script&gt;
  163. &lt;script language="beanshell"&gt;
  164. System.out.println("message is " + message);
  165. &lt;/script&gt;
  166. &lt;script language="judoscript"&gt;
  167. println 'message is ', message
  168. &lt;/script&gt;
  169. &lt;script language="ruby"&gt;
  170. print 'message is ', $message, "\n"
  171. &lt;/script&gt;
  172. &lt;script language="jython"&gt;
  173. print "message is %s" % message
  174. &lt;/script&gt;
  175. </pre>
  176. </blockquote>
  177. <p>
  178. Note that for the <i>jython</i> example, the script contents <b>must</b>
  179. start on the first column.
  180. </p>
  181. <p>
  182. Note also that for the <i>ruby</i> example, the names of the set variables are prefixed
  183. by a '$'.
  184. <p>
  185. The following script shows a little more complicated jruby example:
  186. </p>
  187. <blockquote><pre>
  188. &lt;script language="ruby"&gt;
  189. xmlfiles = Dir.new(".").entries.delete_if { |i| ! (i =~ /\.xml$/) }
  190. xmlfiles.sort.each { |i| $self.log(i) }
  191. &lt;/script&gt;
  192. </pre>
  193. </blockquote>
  194. <p>
  195. The same example in groovy is:
  196. </p>
  197. <blockquote><pre>
  198. &lt;script language="groovy"&gt;
  199. xmlfiles = new java.io.File(".").listFiles().findAll{ it =~ "\.xml$"}
  200. xmlfiles.sort().each { self.log(it.toString())}
  201. &lt;/script&gt;
  202. </pre>
  203. </blockquote>
  204. <p>
  205. The following example shows the use of classpath to specify the location
  206. of the beanshell jar file.
  207. </p>
  208. <blockquote><pre>
  209. &lt;script language="beanshell" setbeans="true"&gt;
  210. &lt;classpath&gt;
  211. &lt;fileset dir="${user.home}/lang/beanshell" includes="*.jar" /&gt;
  212. &lt;/classpath&gt;
  213. System.out.println("Hello world");
  214. &lt;/script&gt;
  215. </pre>
  216. </blockquote>
  217. <p>
  218. The following script uses javascript to create a number of
  219. echo tasks and execute them.
  220. </p>
  221. <blockquote><pre>
  222. &lt;project name=&quot;squares&quot; default=&quot;main&quot; basedir=&quot;.&quot;&gt;
  223. &lt;target name=&quot;main&quot;&gt;
  224. &lt;script language=&quot;javascript&quot;&gt; &lt;![CDATA[
  225. for (i=1; i&lt;=10; i++) {
  226. echo = squares.createTask(&quot;echo&quot;);
  227. echo.setMessage(i*i);
  228. echo.perform();
  229. }
  230. ]]&gt; &lt;/script&gt;
  231. &lt;/target&gt;
  232. &lt;/project&gt;
  233. </pre></blockquote>
  234. <p>generates</p>
  235. <blockquote><pre>
  236. main:
  237. 1
  238. 4
  239. 9
  240. 16
  241. 25
  242. 36
  243. 49
  244. 64
  245. 81
  246. 100
  247. BUILD SUCCESSFUL
  248. </pre></blockquote>
  249. <p>Now a more complex example using the Java API and the Ant API. The goal is to list the
  250. filesizes of all files a <code>&lt;fileset/&gt;</code> caught.</p>
  251. <blockquote><pre>
  252. &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  253. &lt;project name="<font color=blue>MyProject</font>" basedir="." default="main"&gt;
  254. &lt;property name="fs.dir" value="src"/&gt;
  255. &lt;property name="fs.includes" value="**/*.txt"/&gt;
  256. &lt;property name="fs.excludes" value="**/*.tmp"/&gt;
  257. &lt;target name="main"&gt;
  258. &lt;script language="javascript"&gt; &lt;![CDATA[
  259. // import statements
  260. <font color=blue>// importPackage(java.io)</font>;
  261. <font color=blue>importClass(java.io.File)</font>;
  262. // Access to Ant-Properties by their names
  263. dir = <font color=blue>project</font>.getProperty("fs.dir");
  264. includes = <font color=blue>MyProject</font>.getProperty("fs.includes");
  265. excludes = <font color=blue>self.getProject()</font>.<font color=blue>getProperty("fs.excludes")</font>;
  266. // Create a &lt;fileset dir="" includes=""/&gt;
  267. fs = project.<font color=blue>createDataType("fileset")</font>;
  268. fs.setDir( new File(dir) );
  269. <font color=blue>fs.setIncludes(includes)</font>;
  270. fs.setExcludes(excludes);
  271. // Get the files (array) of that fileset
  272. ds = fs.getDirectoryScanner(project);
  273. srcFiles = ds.getIncludedFiles();
  274. // iterate over that array
  275. for (i=0; i&lt;srcFiles.length; i++) {
  276. // get the values via Java API
  277. var basedir = fs.getDir(project);
  278. var filename = srcFiles[i];
  279. var file = <font color=blue>new File(basedir, filename)</font>;
  280. var size = file.length();
  281. // create and use a Task via Ant API
  282. echo = MyProject.<font color=blue>createTask("echo")</font>;
  283. echo.setMessage(filename + ": " + size + " byte");
  284. echo.<font color=blue>perform()</font>;
  285. }
  286. ]]&gt;&lt;/script&gt;
  287. &lt;/target&gt;
  288. &lt;/project&gt;
  289. </pre></blockquote>
  290. <p>We want to use the Java API. Because we don't want always typing the package signature
  291. we do an import. Rhino knows two different methods for import statements: one for packages
  292. and one for a single class. By default only the <i>java</i> packages are available, so
  293. <i>java.lang.System</i> can be directly imported with <code>importClass/importPackage</code>.
  294. For other packages you have to prefix the full classified name with <i>Packages</i>.
  295. For example Ant's <i>FileUtils</i> class can be imported with
  296. <code>importClass(<b>Packages</b>.org.apache.tools.ant.util.FileUtils)</code>
  297. <br>
  298. The <code>&lt;script&gt;</code> task populates the Project instance under
  299. the name <i>project</i>, so we can use that reference. Another way is to use its given name
  300. or getting its reference from the task itself.<br>
  301. The Project provides methods for accessing and setting properties, creating DataTypes and
  302. Tasks and much more.<br>
  303. After creating a FileSet object we initialize that by calling its set-methods. Then we can
  304. use that object like a normal Ant task (<code>&lt;copy&gt;</code> for example).<br>
  305. For getting the size of a file we instantiate a <code>java.io.File</code>. So we are using
  306. normal Java API here.<br>
  307. Finally we use the <code>&lt;echo&gt;</code> task for producing the output. The task is not executed by
  308. its execute() method, because the perform() method (implemented in Task itself) does the
  309. appropriate logging before and after invoking execute().
  310. </p>
  311. <p>
  312. Here is an example of using beanshell to create an ant
  313. task. This task will add filesets and paths to a referenced
  314. path. If the path does not exist, it will be created.
  315. </p>
  316. <blockquote><pre>
  317. &lt;!--
  318. Define addtopath task
  319. --&gt;
  320. &lt;script language="beanshell"&gt;
  321. import org.apache.tools.ant.Task;
  322. import org.apache.tools.ant.types.Path;
  323. import org.apache.tools.ant.types.FileSet;
  324. public class AddToPath extends Task {
  325. private Path path;
  326. public void setRefId(String id) {
  327. path = getProject().getReference(id);
  328. if (path == null) {
  329. path = new Path(getProject());
  330. getProject().addReference(id, path);
  331. }
  332. }
  333. public void add(Path c) {
  334. path.add(c);
  335. }
  336. public void add(FileSet c) {
  337. path.add(c);
  338. }
  339. public void execute() {
  340. // Do nothing
  341. }
  342. }
  343. project.addTaskDefinition("addtopath", AddToPath.class);
  344. &lt;/script&gt;
  345. </pre></blockquote>
  346. <p>
  347. An example of using this task to create a path
  348. from a list of directories (using antcontrib's
  349. <a href="http://ant-contrib.sourceforge.net/tasks/tasks/for.html">
  350. &lt;for&gt;</a> task) follows:
  351. </p>
  352. <blockquote><pre>
  353. &lt;path id="main.path"&gt;
  354. &lt;fileset dir="build/classes"/&gt;
  355. &lt;/path&gt;
  356. &lt;ac:for param="ref" list="commons,fw,lps"
  357. xmlns:ac="antlib:net.sf.antcontrib"&gt;
  358. &lt;sequential&gt;
  359. &lt;addtopath refid="main.path"&gt;
  360. &lt;fileset dir="${dist.dir}/@{ref}/main"
  361. includes="**/*.jar"/&gt;
  362. &lt;/addtopath&gt;
  363. &lt;/sequential&gt;
  364. &lt;/ac:for&gt;
  365. </pre></blockquote>
  366. </body>
  367. </html>