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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  4. <title>Scriptdef Task</title>
  5. </head>
  6. <body>
  7. <h2><a name="script">Script</a></h2>
  8. <h3>Description</h3>
  9. <p>Scriptdef can be used to define an Ant task using a scripting language. Ant
  10. scripting languages supported by
  11. <a href="http://jakarta.apache.org/bsf" target="_top">Apache BSF</a> may be
  12. used to define the script. Scriptdef provides a mechanism to encapsulate
  13. control logic from a build within an Ant task minimizing the need for
  14. proviuding control style tasks in Ant itself. Complex logic can be made
  15. available while retaining the simple structure of an Ant build file. Scriptdef
  16. is also useful for prototyping new custom tasks. Certainly as the complexity
  17. of the script increases it would be better to migrate the task definition
  18. into a Java based custom task.
  19. </p>
  20. <p><b>Note:</b> This task depends on external libraries not included in the
  21. Ant distribution. See
  22. <a href="../install.html#librarydependencies">Library Dependencies</a>
  23. for more information.</p>
  24. <p>The attributes and nested elements supported by the task may be defined
  25. using &lt;attribute&gt; and &lt;element&gt; nested elements. These are
  26. available to the script that implements the task as two collection style
  27. script variables <code>attributes</code> and <code>elements</code>. The
  28. elements in the <code>attributes</code> collection may be accessed by the
  29. attribute name. The <code>elements</code> collection is accessed by the nested
  30. element name. This will return a list of all instances of the nested element.
  31. The instances in this list may be accessed by an integer index.
  32. </p>
  33. <p>The name "project" is a pre-defined reference to the Ant Project. For
  34. more information on writing scripts, please refer to the
  35. <a href="script.html">&lt;script&gt;</a> task
  36. </p>
  37. <h3>Parameters</h3>
  38. <table border="1" cellpadding="2" cellspacing="0">
  39. <tr>
  40. <td valign="top"><b>Attribute</b></td>
  41. <td valign="top"><b>Description</b></td>
  42. <td align="center" valign="top"><b>Required</b></td>
  43. </tr>
  44. <tr>
  45. <td valign="top">name</td>
  46. <td valign="top">the name of the task to be created using the script</td>
  47. <td valign="top" align="center">Yes</td>
  48. </tr>
  49. <tr>
  50. <td valign="top">language</td>
  51. <td valign="top">The programming language the script is written in.
  52. Must be a supported Apache BSF language</td>
  53. <td valign="top" align="center">Yes</td>
  54. </tr>
  55. <tr>
  56. <td valign="top">src</td>
  57. <td valign="top">The location of the script as a file, if not inline</td>
  58. <td valign="top" align="center">No</td>
  59. </tr>
  60. </table>
  61. <h3>Nested elements</h3>
  62. <h4>attribute</h4>
  63. <table border="1" cellpadding="2" cellspacing="0">
  64. <tr>
  65. <td valign="top"><b>Attribute</b></td>
  66. <td valign="top"><b>Description</b></td>
  67. <td align="center" valign="top"><b>Required</b></td>
  68. </tr>
  69. <tr>
  70. <td valign="top">name</td>
  71. <td valign="top">the name of the attribute</td>
  72. <td valign="top" align="center">Yes</td>
  73. </tr>
  74. </table>
  75. <h4>element</h4>
  76. <table border="1" cellpadding="2" cellspacing="0">
  77. <tr>
  78. <td valign="top"><b>Attribute</b></td>
  79. <td valign="top"><b>Description</b></td>
  80. <td align="center" valign="top"><b>Required</b></td>
  81. </tr>
  82. <tr>
  83. <td valign="top">name</td>
  84. <td valign="top">the name of the nested element to be supported by the
  85. task defined by the script</td>
  86. <td valign="top" align="center">Yes</td>
  87. </tr>
  88. <tr>
  89. <td valign="top">classname</td>
  90. <td valign="top">the classname of the class to be used for the nested element.
  91. This specifies the class directly and is an alternative to specifying
  92. the Ant type name.</td>
  93. <td valign="top" align="center">No</td>
  94. </tr>
  95. <tr>
  96. <td valign="top">type</td>
  97. <td valign="top">This is the name of an Ant task or type which is to
  98. be used when this element is to be created. This is an alternative
  99. to specifying the class name directly</td>
  100. <td valign="top" align="center">No</td>
  101. </tr>
  102. </table>
  103. <h3>Examples</h3>
  104. <p>
  105. The following definition creates a task which supprts an attribute called
  106. attr and two nested elements, one being a fileset and the other a path. When
  107. executed, the resulting task logs the value of the attribute and the basedir
  108. of the first fileset.
  109. </p>
  110. <pre>
  111. &lt;scriptdef name=&quot;scripttest&quot; language=&quot;javascript&quot;&gt;
  112. &lt;attribute name=&quot;attr1&quot;/&gt;
  113. &lt;element name=&quot;fileset&quot; type=&quot;fileset&quot;/&gt;
  114. &lt;element name=&quot;path&quot; type=&quot;path&quot;/&gt;
  115. &lt;![CDATA[
  116. project.log(&quot;Hello from script&quot;);
  117. project.log(&quot;Attribute attr1 = &quot; + attributes.get(&quot;attr1&quot;));
  118. project.log(&quot;First fileset basedir = &quot;
  119. + elements.get(&quot;fileset&quot;).get(0).getDir(project));
  120. ]]&gt;
  121. &lt;/scriptdef&gt;
  122. &lt;scripttest attr1=&quot;test&quot;&gt;
  123. &lt;path&gt;
  124. &lt;pathelement location=&quot;src&quot;/&gt;
  125. &lt;/path&gt;
  126. &lt;fileset dir=&quot;src&quot;/&gt;
  127. &lt;fileset dir=&quot;main&quot;/&gt;
  128. &lt;/scripttest&gt;
  129. </pre>
  130. <p>
  131. The following variation on the above script lists the number of fileset elements
  132. and iterates through them
  133. </p>
  134. <pre>
  135. &lt;scriptdef name=&quot;scripttest2&quot; language=&quot;javascript&quot;&gt;
  136. &lt;element name=&quot;fileset&quot; type=&quot;fileset&quot;/&gt;
  137. &lt;![CDATA[
  138. filesets = elements.get(&quot;fileset&quot;);
  139. project.log(&quot;Number of filesets = &quot; + filesets.size());
  140. for (i = 0; i &lt; filesets.size(); ++i) {
  141. project.log(&quot;fileset &quot; + i + &quot; basedir = &quot;
  142. + filesets.get(i).getDir(project));
  143. }
  144. ]]&gt;
  145. &lt;/scriptdef&gt
  146. &lt;scripttest2&gt;
  147. &lt;fileset dir=&quot;src&quot;/&gt;
  148. &lt;fileset dir=&quot;main&quot;/&gt;
  149. &lt;/scripttest2&gt;
  150. </pre>
  151. <p>
  152. When a script has a syntax error, the scriptdef name will be listed in the
  153. error. For example in the above script, removing the closing curly bracket
  154. would result in this error
  155. </p>
  156. <p><code>build.xml:15: SyntaxError: missing } in compound
  157. statement (scriptdef &lt;scripttest2&gt;; line 10)</code></p>
  158. <p>
  159. Script errors are only detected when a script task is actually executed.
  160. </p>
  161. <hr>
  162. <p align="center">Copyright &copy; 2000-2003 Apache Software Foundation. All rights
  163. Reserved.</p>
  164. </body>
  165. </html>