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

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