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 kB

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