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

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