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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 script def task instance.
  39. It can be used for logging purposes</p>
  40. <p>The name "project" is a pre-defined reference to the Ant Project. For
  41. more information on writing scripts, please refer to the
  42. <a href="script.html"><code>&lt;script&gt;</code></a> task
  43. </p>
  44. <h3>Parameters</h3>
  45. <table border="1" cellpadding="2" cellspacing="0">
  46. <tr>
  47. <td valign="top"><b>Attribute</b></td>
  48. <td valign="top"><b>Description</b></td>
  49. <td align="center" valign="top"><b>Required</b></td>
  50. </tr>
  51. <tr>
  52. <td valign="top">name</td>
  53. <td valign="top">the name of the task to be created using the script</td>
  54. <td valign="top" align="center">Yes</td>
  55. </tr>
  56. <tr>
  57. <td valign="top">language</td>
  58. <td valign="top">The programming language the script is written in.
  59. Must be a supported Apache BSF language</td>
  60. <td valign="top" align="center">Yes</td>
  61. </tr>
  62. <tr>
  63. <td valign="top">src</td>
  64. <td valign="top">The location of the script as a file, if not inline</td>
  65. <td valign="top" align="center">No</td>
  66. </tr>
  67. <tr>
  68. <td valign="top">uri</td>
  69. <td valign="top">
  70. The XML namespace uri that this definition should live in.
  71. </td>
  72. <td valign="top" align="center">No</td>
  73. </tr>
  74. </table>
  75. <h3>Nested elements</h3>
  76. <h4>attribute</h4>
  77. <table border="1" cellpadding="2" cellspacing="0">
  78. <tr>
  79. <td valign="top"><b>Attribute</b></td>
  80. <td valign="top"><b>Description</b></td>
  81. <td align="center" valign="top"><b>Required</b></td>
  82. </tr>
  83. <tr>
  84. <td valign="top">name</td>
  85. <td valign="top">the name of the attribute</td>
  86. <td valign="top" align="center">Yes</td>
  87. </tr>
  88. </table>
  89. <h4>element</h4>
  90. <table border="1" cellpadding="2" cellspacing="0">
  91. <tr>
  92. <td valign="top"><b>Attribute</b></td>
  93. <td valign="top"><b>Description</b></td>
  94. <td align="center" valign="top"><b>Required</b></td>
  95. </tr>
  96. <tr>
  97. <td valign="top">name</td>
  98. <td valign="top">the name of the nested element to be supported by the
  99. task defined by the script</td>
  100. <td valign="top" align="center">Yes</td>
  101. </tr>
  102. <tr>
  103. <td valign="top">classname</td>
  104. <td valign="top">the classname of the class to be used for the nested element.
  105. This specifies the class directly and is an alternative to specifying
  106. the Ant type name.</td>
  107. <td valign="top" align="center">No</td>
  108. </tr>
  109. <tr>
  110. <td valign="top">type</td>
  111. <td valign="top">This is the name of an Ant task or type which is to
  112. be used when this element is to be created. This is an alternative
  113. to specifying the class name directly</td>
  114. <td valign="top" align="center">No</td>
  115. </tr>
  116. </table>
  117. <h3>Examples</h3>
  118. <p>
  119. The following definition creates a task which supports an attribute called
  120. attr and two nested elements, one being a fileset and the other a path. When
  121. executed, the resulting task logs the value of the attribute and the basedir
  122. of the first fileset.
  123. </p>
  124. <pre>
  125. &lt;scriptdef name=&quot;scripttest&quot; language=&quot;javascript&quot;&gt;
  126. &lt;attribute name=&quot;attr1&quot;/&gt;
  127. &lt;element name=&quot;fileset&quot; type=&quot;fileset&quot;/&gt;
  128. &lt;element name=&quot;path&quot; type=&quot;path&quot;/&gt;
  129. &lt;![CDATA[
  130. self.log(&quot;Hello from script&quot;);
  131. self.log(&quot;Attribute attr1 = &quot; + attributes.get(&quot;attr1&quot;));
  132. self.log(&quot;First fileset basedir = &quot;
  133. + elements.get(&quot;fileset&quot;).get(0).getDir(project));
  134. ]]&gt;
  135. &lt;/scriptdef&gt;
  136. &lt;scripttest attr1=&quot;test&quot;&gt;
  137. &lt;path&gt;
  138. &lt;pathelement location=&quot;src&quot;/&gt;
  139. &lt;/path&gt;
  140. &lt;fileset dir=&quot;src&quot;/&gt;
  141. &lt;fileset dir=&quot;main&quot;/&gt;
  142. &lt;/scripttest&gt;
  143. </pre>
  144. <p>
  145. The following variation on the above script lists the number of fileset elements
  146. and iterates through them
  147. </p>
  148. <pre>
  149. &lt;scriptdef name=&quot;scripttest2&quot; language=&quot;javascript&quot;&gt;
  150. &lt;element name=&quot;fileset&quot; type=&quot;fileset&quot;/&gt;
  151. &lt;![CDATA[
  152. filesets = elements.get(&quot;fileset&quot;);
  153. self.log(&quot;Number of filesets = &quot; + filesets.size());
  154. for (i = 0; i &lt; filesets.size(); ++i) {
  155. self.log(&quot;fileset &quot; + i + &quot; basedir = &quot;
  156. + filesets.get(i).getDir(project));
  157. }
  158. ]]&gt;
  159. &lt;/scriptdef&gt
  160. &lt;scripttest2&gt;
  161. &lt;fileset dir=&quot;src&quot;/&gt;
  162. &lt;fileset dir=&quot;main&quot;/&gt;
  163. &lt;/scripttest2&gt;
  164. </pre>
  165. <p>
  166. When a script has a syntax error, the scriptdef name will be listed in the
  167. error. For example in the above script, removing the closing curly bracket
  168. would result in this error
  169. </p>
  170. <p><code>build.xml:15: SyntaxError: missing } in compound
  171. statement (scriptdef <code>&lt;scripttest2&gt;</code>; line 10)</code></p>
  172. <p>
  173. Script errors are only detected when a script task is actually executed.
  174. </p>
  175. <hr>
  176. <p align="center">Copyright &copy; 2000-2005 The Apache Software Foundation. All rights
  177. Reserved.</p>
  178. </body>
  179. </html>