blob: 3e2cc28989606fb7780f945e127ffcea8ddd478b [file] [log] [blame]
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<title>Scriptdef Task</title>
<link rel="stylesheet" type="text/css" href="../stylesheets/antmanual.css">
</head>
<body>
<h2><a name="script">Script</a></h2>
<h3>Description</h3>
<p>Scriptdef can be used to define an Ant task using a scripting language. Ant
scripting languages supported by
<a href="http://jakarta.apache.org/bsf" target="_top">Apache BSF</a> may be
used to define the script. Scriptdef provides a mechanism to encapsulate
control logic from a build within an Ant task minimizing the need for
proviuding control style tasks in Ant itself. Complex logic can be made
available while retaining the simple structure of an Ant build file. Scriptdef
is also useful for prototyping new custom tasks. Certainly as the complexity
of the script increases it would be better to migrate the task definition
into a Java based custom task.
</p>
<p><b>Note:</b> This task depends on external libraries not included in the
Ant distribution. See
<a href="../install.html#librarydependencies">Library Dependencies</a>
for more information.</p>
<p>The attributes and nested elements supported by the task may be defined
using &lt;attribute&gt; and &lt;element&gt; nested elements. These are
available to the script that implements the task as two collection style
script variables <code>attributes</code> and <code>elements</code>. The
elements in the <code>attributes</code> collection may be accessed by the
attribute name. The <code>elements</code> collection is accessed by the nested
element name. This will return a list of all instances of the nested element.
The instances in this list may be accessed by an integer index.
</p>
<p>The name "project" is a pre-defined reference to the Ant Project. For
more information on writing scripts, please refer to the
<a href="script.html">&lt;script&gt;</a> task
</p>
<h3>Parameters</h3>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">name</td>
<td valign="top">the name of the task to be created using the script</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">language</td>
<td valign="top">The programming language the script is written in.
Must be a supported Apache BSF language</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">src</td>
<td valign="top">The location of the script as a file, if not inline</td>
<td valign="top" align="center">No</td>
</tr>
</table>
<h3>Nested elements</h3>
<h4>attribute</h4>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">name</td>
<td valign="top">the name of the attribute</td>
<td valign="top" align="center">Yes</td>
</tr>
</table>
<h4>element</h4>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">name</td>
<td valign="top">the name of the nested element to be supported by the
task defined by the script</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">classname</td>
<td valign="top">the classname of the class to be used for the nested element.
This specifies the class directly and is an alternative to specifying
the Ant type name.</td>
<td valign="top" align="center">No</td>
</tr>
<tr>
<td valign="top">type</td>
<td valign="top">This is the name of an Ant task or type which is to
be used when this element is to be created. This is an alternative
to specifying the class name directly</td>
<td valign="top" align="center">No</td>
</tr>
</table>
<h3>Examples</h3>
<p>
The following definition creates a task which supprts an attribute called
attr and two nested elements, one being a fileset and the other a path. When
executed, the resulting task logs the value of the attribute and the basedir
of the first fileset.
</p>
<pre>
&lt;scriptdef name=&quot;scripttest&quot; language=&quot;javascript&quot;&gt;
&lt;attribute name=&quot;attr1&quot;/&gt;
&lt;element name=&quot;fileset&quot; type=&quot;fileset&quot;/&gt;
&lt;element name=&quot;path&quot; type=&quot;path&quot;/&gt;
&lt;![CDATA[
project.log(&quot;Hello from script&quot;);
project.log(&quot;Attribute attr1 = &quot; + attributes.get(&quot;attr1&quot;));
project.log(&quot;First fileset basedir = &quot;
+ elements.get(&quot;fileset&quot;).get(0).getDir(project));
]]&gt;
&lt;/scriptdef&gt;
&lt;scripttest attr1=&quot;test&quot;&gt;
&lt;path&gt;
&lt;pathelement location=&quot;src&quot;/&gt;
&lt;/path&gt;
&lt;fileset dir=&quot;src&quot;/&gt;
&lt;fileset dir=&quot;main&quot;/&gt;
&lt;/scripttest&gt;
</pre>
<p>
The following variation on the above script lists the number of fileset elements
and iterates through them
</p>
<pre>
&lt;scriptdef name=&quot;scripttest2&quot; language=&quot;javascript&quot;&gt;
&lt;element name=&quot;fileset&quot; type=&quot;fileset&quot;/&gt;
&lt;![CDATA[
filesets = elements.get(&quot;fileset&quot;);
project.log(&quot;Number of filesets = &quot; + filesets.size());
for (i = 0; i &lt; filesets.size(); ++i) {
project.log(&quot;fileset &quot; + i + &quot; basedir = &quot;
+ filesets.get(i).getDir(project));
}
]]&gt;
&lt;/scriptdef&gt
&lt;scripttest2&gt;
&lt;fileset dir=&quot;src&quot;/&gt;
&lt;fileset dir=&quot;main&quot;/&gt;
&lt;/scripttest2&gt;
</pre>
<p>
When a script has a syntax error, the scriptdef name will be listed in the
error. For example in the above script, removing the closing curly bracket
would result in this error
</p>
<p><code>build.xml:15: SyntaxError: missing } in compound
statement (scriptdef &lt;scripttest2&gt;; line 10)</code></p>
<p>
Script errors are only detected when a script task is actually executed.
</p>
<hr>
<p align="center">Copyright &copy; 2000-2003 Apache Software Foundation. All rights
Reserved.</p>
</body>
</html>