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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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>
  27. or <a href="https://jcp.org/aboutJava/communityprocess/maintenance/jsr223/223ChangeLog.html">JSR
  28. 223</a> may be used to define the script. <code>Scriptdef</code> provides a mechanism to encapsulate
  29. control logic from a build within an Ant task minimizing the need for providing control style tasks
  30. in Ant itself. Complex logic can be made available while retaining the simple structure of an Ant
  31. build file. <code>Scriptdef</code> is also useful for prototyping new custom tasks. Certainly as the
  32. complexity of the script increases it would be better to migrate the task definition into a Java
  33. based custom task.</p>
  34. <p><strong>Note</strong>: This task depends on external libraries not included in the Ant
  35. distribution. See <a href="../install.html#librarydependencies">Library Dependencies</a> for more
  36. information.</p>
  37. <p>The attributes and nested elements supported by the task may be defined
  38. using <code>&lt;attribute&gt;</code> and <code>&lt;element&gt;</code> nested elements. These are
  39. available to the script that implements the task as two collection style script
  40. variables <code>attributes</code> and <code>elements</code>. The elements in
  41. the <code>attributes</code> collection may be accessed by the attribute
  42. name. The <code>elements</code> collection is accessed by the nested element name. This will return
  43. a list of all instances of the nested element. The instances in this list may be accessed by an
  44. integer index.</p>
  45. <p><strong>Note</strong>: Ant will turn all attribute and element names into all lowercase names, so
  46. even if you use <var>name</var>=<q>SomeAttribute</q>, you'll have to use <q>someattribute</q> to
  47. retrieve the attribute's value from the <code>attributes</code> collection.</p>
  48. <p>The name <code>self</code> (<em>since Ant 1.6.3</em>) is a pre-defined reference to
  49. the <code>scriptdef</code> task instance. It can be used for logging, or for integration with the
  50. rest of Ant. the <code>self.text attribute</code> contains any nested text passed to the script</p>
  51. <p>If an attribute or element is not passed in, then <code>attributes.get()</code>
  52. or <code>elements.get()</code> will return null. It is up to the script to perform any checks and
  53. validation. <code>self.fail(String message)</code>can be used to raise
  54. a <code>BuildException</code>.</p>
  55. <p>The name <code>project</code> is a pre-defined reference to the Ant Project. For more information
  56. on writing scripts, please refer to the <a href="script.html"><code>&lt;script&gt;</code></a>
  57. task.</p>
  58. <h3>Parameters</h3>
  59. <table class="attr">
  60. <tr>
  61. <th>Attribute</th>
  62. <th>Description</th>
  63. <th>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>Nested elements</h3>
  125. <h4>attribute</h4>
  126. <table class="attr">
  127. <tr>
  128. <th>Attribute</th>
  129. <th>Description</th>
  130. <th>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>Attribute</th>
  142. <th>Description</th>
  143. <th>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. <tr>
  165. <td>any resource or resource collection</td>
  166. <td><em>Since Ant 1.7.1</em>, this task can load scripts from any resource supplied as a nested
  167. element.</td>
  168. <td>No</td>
  169. </tr>
  170. </table>
  171. <h4>classpath</h4>
  172. <p>See the <a href="../Tasks/script.html">script</a> task for using this nested element.</p>
  173. <h3>Examples</h3>
  174. <p>The following definition creates a task which supports an attribute called <var>attr</var> and
  175. two nested elements, one being a fileset and the other a path. When executed, the resulting task
  176. logs the value of the attribute and the <var>basedir</var> of the first fileset.</p>
  177. <pre>
  178. &lt;scriptdef name=&quot;scripttest&quot; language=&quot;javascript&quot;&gt;
  179. &lt;attribute name=&quot;attr1&quot;/&gt;
  180. &lt;element name=&quot;fileset&quot; type=&quot;fileset&quot;/&gt;
  181. &lt;element name=&quot;path&quot; type=&quot;path&quot;/&gt;
  182. &lt;![CDATA[
  183. self.log(&quot;Hello from script&quot;);
  184. self.log(&quot;Attribute attr1 = &quot; + attributes.get(&quot;attr1&quot;));
  185. self.log(&quot;First fileset basedir = &quot;
  186. + elements.get(&quot;fileset&quot;).get(0).getDir(project));
  187. ]]&gt;
  188. &lt;/scriptdef&gt;
  189. &lt;scripttest attr1=&quot;test&quot;&gt;
  190. &lt;path&gt;
  191. &lt;pathelement location=&quot;src&quot;/&gt;
  192. &lt;/path&gt;
  193. &lt;fileset dir=&quot;src&quot;/&gt;
  194. &lt;fileset dir=&quot;main&quot;/&gt;
  195. &lt;/scripttest&gt;</pre>
  196. <p>The following variation on the above script lists the number of fileset elements and iterates
  197. through them</p>
  198. <pre>
  199. &lt;scriptdef name=&quot;scripttest2&quot; language=&quot;javascript&quot;&gt;
  200. &lt;element name=&quot;fileset&quot; type=&quot;fileset&quot;/&gt;
  201. &lt;![CDATA[
  202. filesets = elements.get(&quot;fileset&quot;);
  203. self.log(&quot;Number of filesets = &quot; + filesets.size());
  204. for (i = 0; i &lt; filesets.size(); ++i) {
  205. self.log(&quot;fileset &quot; + i + &quot; basedir = &quot;
  206. + filesets.get(i).getDir(project));
  207. }
  208. ]]&gt;
  209. &lt;/scriptdef&gt
  210. &lt;scripttest2&gt;
  211. &lt;fileset dir=&quot;src&quot;/&gt;
  212. &lt;fileset dir=&quot;main&quot;/&gt;
  213. &lt;/scripttest2&gt;</pre>
  214. <p>When a script has a syntax error, the <code>scriptdef</code> name will be listed in the
  215. error. For example in the above script, removing the closing curly bracket would result in this
  216. error</p>
  217. <pre>build.xml:15: SyntaxError: missing } in compound
  218. statement (scriptdef <code>&lt;scripttest2&gt;</code>; line 10)</pre>
  219. <p>Script errors are only detected when a <code>script</code> task is actually executed.</p>
  220. <p>The next example does uses nested text in Jython. It also declares the script in a new xml
  221. namespace, which must be used to refer to the task. Declaring scripts in a new namespace guarantees
  222. that Ant will not create a task of the same (namespace,localname) name pair.</p>
  223. <pre>
  224. &lt;target name="echo-task-jython"&gt;
  225. &lt;scriptdef language="jython"
  226. name="echo"
  227. uri="http://example.org/script"&gt;
  228. &lt;![CDATA[
  229. self.log("text: " +self.text)
  230. ]]&gt;
  231. &lt;/scriptdef&gt;
  232. &lt;/target&gt;
  233. &lt;target name="testEcho" depends="echo-task-jython"
  234. xmlns:s="http://example.org/script"&gt;
  235. &lt;s:echo&gt;nested text&lt;/s:echo&gt;
  236. &lt;/target&gt;</pre>
  237. <p>The next example shows the use of &lt;classpath&gt; and <var>loaderref</var> to get access to the
  238. beanshell jar.</p>
  239. <pre>
  240. &lt;scriptdef name="b1" language="beanshell"
  241. loaderref="beanshell-ref"&gt;
  242. &lt;attribute name="a"/&gt;
  243. &lt;classpath path="${user.home}/scripting/beanshell/bsh-1.3b1.jar"/&gt;
  244. self.log("attribute a is " + attributes.get("a"));
  245. &lt;/scriptdef&gt;
  246. &lt;scriptdef name="b2" language="beanshell"
  247. loaderref="beanshell-ref"&gt;
  248. &lt;attribute name="a2"/&gt;
  249. self.log("attribute a2 is " + attributes.get("a2"));
  250. &lt;/scriptdef&gt;
  251. &lt;b1 a="this is an 'a'"/&gt;
  252. &lt;b2 a2="this is an 'a2' for b2"/&gt;</pre>
  253. <h3>Testing Scripts</h3>
  254. <p>The easiest way to test scripts is to use
  255. the <a href="https://ant.apache.org/antlibs/antunit/">AntUnit</a> Ant library. This will run all
  256. targets in a script that begin with <q>test</q> (and their dependencies).</p>
  257. </body>
  258. </html>