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.

scriptdef.html 11 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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">
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>Scriptdef Task</title>
  20. </head>
  21. <body>
  22. <h2 id="script">Scriptdef</h2>
  23. <h3>Description</h3>
  24. <p><code>Scriptdef</code> can be used to define an Apache Ant task using a scripting language. Ant
  25. scripting languages supported by <a href="https://jakarta.apache.org/bsf" target="_top">Apache
  26. BSF</a> or <a href="https://jcp.org/aboutJava/communityprocess/maintenance/jsr223/223ChangeLog.html"
  27. target="_top">JSR 223</a> may be used to define the script. <code>Scriptdef</code> provides a
  28. mechanism to encapsulate control logic from a build within an Ant task minimizing the need for
  29. providing control style tasks in Ant itself. Complex logic can be made available while retaining the
  30. simple structure of an Ant build file. <code>Scriptdef</code> is also useful for prototyping new
  31. custom tasks. Certainly as the complexity of the script increases it would be better to migrate the
  32. task definition into a Java based custom task.</p>
  33. <p><strong>Note</strong>: This task depends on external libraries not included in the Ant
  34. distribution. See <a href="../install.html#librarydependencies">Library Dependencies</a> for more
  35. information.</p>
  36. <p>The attributes and nested elements supported by the task may be defined
  37. using <code>&lt;attribute&gt;</code> and <code>&lt;element&gt;</code> nested elements. These are
  38. available to the script that implements the task as two collection style script
  39. variables <code class="code">attributes</code> and <code class="code">elements</code>. The elements
  40. in the <code class="code">attributes</code> collection may be accessed by the attribute
  41. name. The <code class="code">elements</code> collection is accessed by the nested element name. This
  42. will return a list of all instances of the nested element. The instances in this list may be
  43. accessed by an integer index.</p>
  44. <p><strong>Note</strong>: Ant will turn all attribute and element names into all lowercase names, so
  45. even if you use <var>name</var>=<q>SomeAttribute</q>, you'll have to use <q>someattribute</q> to
  46. retrieve the attribute's value from the <code class="code">attributes</code> collection.</p>
  47. <p>The name <code class="code">self</code> (<em>since Ant 1.6.3</em>) is a pre-defined reference to
  48. the <code>scriptdef</code> task instance. It can be used for logging, or for integration with the
  49. rest of Ant. The <code class="code">self.text</code> attribute contains any nested text passed to
  50. the script</p>
  51. <p>If an attribute or element is not passed in, then <code class="code">attributes.get()</code>
  52. or <code class="code">elements.get()</code> will return null. It is up to the script to perform any
  53. checks and validation. <code class="code">self.fail(String message)</code>can be used to raise
  54. a <code>BuildException</code>.</p>
  55. <p>The name <code class="code">project</code> is a pre-defined reference to the Ant Project. For
  56. more information on writing scripts, please refer to
  57. the <a href="script.html"><code>&lt;script&gt;</code></a> task.</p>
  58. <h3>Parameters</h3>
  59. <table class="attr">
  60. <tr>
  61. <th scope="col">Attribute</th>
  62. <th scope="col">Description</th>
  63. <th scope="col">Required</th>
  64. </tr>
  65. <tr>
  66. <td>name</td>
  67. <td>the name of the task to be created using the script</td>
  68. <td>Yes</td>
  69. </tr>
  70. <tr>
  71. <td>language</td>
  72. <td>The programming language the script is written in. Must be a supported Apache BSF or JSR
  73. 223 language</td>
  74. <td>Yes</td>
  75. </tr>
  76. <tr>
  77. <td>manager</td>
  78. <td>The script engine manager to use. See the <a href="../Tasks/script.html">script</a> task
  79. for using this attribute.</td>
  80. <td>No; default is <q>auto</q></td>
  81. </tr>
  82. <tr>
  83. <td>src</td>
  84. <td>The location of the script as a file, if not inline</td>
  85. <td>No</td>
  86. </tr>
  87. <tr>
  88. <td>encoding</td>
  89. <td>The encoding of the script as a file. <em>since Ant 1.10.2</em>.</td>
  90. <td>No; defaults to default JVM character encoding</td>
  91. </tr>
  92. <tr>
  93. <td>compiled</td>
  94. <td>If true, the script is compiled before the first evaluation for faster multiple executions,
  95. on the condition that the <var>manager</var> is <q>javax</q> and the target engine
  96. implements <code>javax.script.Compilable</code>. Note that the <q>bsf</q> manager may
  97. automatically compile the script. <em>since Ant 1.10.2</em>.</td>
  98. <td>No; defaults to <q>false</q></td>
  99. </tr>
  100. <tr>
  101. <td>uri</td>
  102. <td>The XML namespace uri that this definition should live in.</td>
  103. <td>No</td>
  104. </tr>
  105. <tr>
  106. <td>classpath</td>
  107. <td>The classpath to pass into the script.</td>
  108. <td>No</td>
  109. </tr>
  110. <tr>
  111. <td>classpathref</td>
  112. <td>The classpath to use, given as a <a href="../using.html#references">reference</a> to a path
  113. defined elsewhere.
  114. <td>No</td>
  115. </tr>
  116. <tr>
  117. <td>loaderRef</td>
  118. <td>the name of the loader that is used to load the script, constructed from the specified
  119. classpath. This allows multiple script definitions to reuse the same class loader.
  120. </td>
  121. <td>No</td>
  122. </tr>
  123. </table>
  124. <h3>Parameters specified as nested elements</h3>
  125. <h4>attribute</h4>
  126. <table class="attr">
  127. <tr>
  128. <th scope="col">Attribute</th>
  129. <th scope="col">Description</th>
  130. <th scope="col">Required</th>
  131. </tr>
  132. <tr>
  133. <td>name</td>
  134. <td>the name of the attribute</td>
  135. <td>Yes</td>
  136. </tr>
  137. </table>
  138. <h4>element</h4>
  139. <table class="attr">
  140. <tr>
  141. <th scope="col">Attribute</th>
  142. <th scope="col">Description</th>
  143. <th scope="col">Required</th>
  144. </tr>
  145. <tr>
  146. <td>name</td>
  147. <td>the name of the nested element to be supported by the task defined by the script</td>
  148. <td>Yes</td>
  149. </tr>
  150. <tr>
  151. <td>classname</td>
  152. <td>the classname of the class to be used for the nested element. This specifies the class
  153. directly and is an alternative to specifying the Ant type name.</td>
  154. <td>No</td>
  155. </tr>
  156. <tr>
  157. <td>type</td>
  158. <td>This is the name of an Ant task or type which is to be used when this element is to be
  159. created. This is an alternative to specifying the class name directly. If the type is in a
  160. namespace, the URI and a <q>:</q> must be prefixed to the type. For
  161. example <var>type</var>=<q>antlib:example.org:newtype</q></td>
  162. <td>No</td>
  163. </tr>
  164. </table>
  165. <h4>classpath</h4>
  166. <p>See the <a href="../Tasks/script.html">script</a> task for using this nested element.</p>
  167. <h4>any resource collection</h4>
  168. <p><em>Since Ant 1.7.1</em></p>
  169. <p>This task can load scripts from any resource supplied as a nested element.</p>
  170. <h3>Examples</h3>
  171. <p>The following definition creates a task which supports an attribute called <var>attr</var> and
  172. two nested elements, one being a fileset and the other a path. When executed, the resulting task
  173. logs the value of the attribute and the <var>basedir</var> of the first fileset.</p>
  174. <pre>
  175. &lt;scriptdef name=&quot;scripttest&quot; language=&quot;javascript&quot;&gt;
  176. &lt;attribute name=&quot;attr1&quot;/&gt;
  177. &lt;element name=&quot;fileset&quot; type=&quot;fileset&quot;/&gt;
  178. &lt;element name=&quot;path&quot; type=&quot;path&quot;/&gt;
  179. &lt;![CDATA[
  180. self.log(&quot;Hello from script&quot;);
  181. self.log(&quot;Attribute attr1 = &quot; + attributes.get(&quot;attr1&quot;));
  182. self.log(&quot;First fileset basedir = &quot;
  183. + elements.get(&quot;fileset&quot;).get(0).getDir(project));
  184. ]]&gt;
  185. &lt;/scriptdef&gt;
  186. &lt;scripttest attr1=&quot;test&quot;&gt;
  187. &lt;path&gt;
  188. &lt;pathelement location=&quot;src&quot;/&gt;
  189. &lt;/path&gt;
  190. &lt;fileset dir=&quot;src&quot;/&gt;
  191. &lt;fileset dir=&quot;main&quot;/&gt;
  192. &lt;/scripttest&gt;</pre>
  193. <p>The following variation on the above script lists the number of fileset elements and iterates
  194. through them</p>
  195. <pre>
  196. &lt;scriptdef name=&quot;scripttest2&quot; language=&quot;javascript&quot;&gt;
  197. &lt;element name=&quot;fileset&quot; type=&quot;fileset&quot;/&gt;
  198. &lt;![CDATA[
  199. filesets = elements.get(&quot;fileset&quot;);
  200. self.log(&quot;Number of filesets = &quot; + filesets.size());
  201. for (i = 0; i &lt; filesets.size(); ++i) {
  202. self.log(&quot;fileset &quot; + i + &quot; basedir = &quot;
  203. + filesets.get(i).getDir(project));
  204. }
  205. ]]&gt;
  206. &lt;/scriptdef&gt
  207. &lt;scripttest2&gt;
  208. &lt;fileset dir=&quot;src&quot;/&gt;
  209. &lt;fileset dir=&quot;main&quot;/&gt;
  210. &lt;/scripttest2&gt;</pre>
  211. <p>When a script has a syntax error, the <code>scriptdef</code> name will be listed in the
  212. error. For example in the above script, removing the closing curly bracket would result in this
  213. error</p>
  214. <pre class="output">build.xml:15: SyntaxError: missing } in compound
  215. statement (scriptdef &lt;scripttest2&gt;; line 10)</pre>
  216. <p>Script errors are only detected when a <code>script</code> task is actually executed.</p>
  217. <p>The next example does uses nested text in Jython. It also declares the script in a new xml
  218. namespace, which must be used to refer to the task. Declaring scripts in a new namespace guarantees
  219. that Ant will not create a task of the same (namespace,localname) name pair.</p>
  220. <pre>
  221. &lt;target name="echo-task-jython"&gt;
  222. &lt;scriptdef language="jython"
  223. name="echo"
  224. uri="http://example.org/script"&gt;
  225. &lt;![CDATA[
  226. self.log("text: " +self.text)
  227. ]]&gt;
  228. &lt;/scriptdef&gt;
  229. &lt;/target&gt;
  230. &lt;target name="testEcho" depends="echo-task-jython"
  231. xmlns:s="http://example.org/script"&gt;
  232. &lt;s:echo&gt;nested text&lt;/s:echo&gt;
  233. &lt;/target&gt;</pre>
  234. <p>The next example shows the use of <code>&lt;classpath&gt;</code> and <var>loaderref</var> to get
  235. access to the beanshell jar.</p>
  236. <pre>
  237. &lt;scriptdef name="b1" language="beanshell"
  238. loaderref="beanshell-ref"&gt;
  239. &lt;attribute name="a"/&gt;
  240. &lt;classpath path="${user.home}/scripting/beanshell/bsh-1.3b1.jar"/&gt;
  241. self.log("attribute a is " + attributes.get("a"));
  242. &lt;/scriptdef&gt;
  243. &lt;scriptdef name="b2" language="beanshell"
  244. loaderref="beanshell-ref"&gt;
  245. &lt;attribute name="a2"/&gt;
  246. self.log("attribute a2 is " + attributes.get("a2"));
  247. &lt;/scriptdef&gt;
  248. &lt;b1 a="this is an 'a'"/&gt;
  249. &lt;b2 a2="this is an 'a2' for b2"/&gt;</pre>
  250. <h3>Testing scripts</h3>
  251. <p>The easiest way to test scripts is to use the <a href="https://ant.apache.org/antlibs/antunit/"
  252. target="_top">AntUnit</a> Ant library. This will run all targets in a script that begin
  253. with <q>test</q> (and their dependencies).</p>
  254. </body>
  255. </html>