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

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