2006-09-11 04:19:00 +00:00
|
|
|
<!--
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
-->
|
2003-10-02 00:38:03 +00:00
|
|
|
<html>
|
|
|
|
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-Language" content="en-us">
|
2005-04-29 18:58:16 +00:00
|
|
|
<link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
|
2003-10-02 00:38:03 +00:00
|
|
|
<title>Scriptdef Task</title>
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
2018-01-22 23:52:21 +01:00
|
|
|
<h2 id="script">Scriptdef</h2>
|
2003-10-02 00:38:03 +00:00
|
|
|
<h3>Description</h3>
|
2010-11-11 17:04:16 +00:00
|
|
|
<p>Scriptdef can be used to define an Apache Ant task using a scripting language. Ant
|
2003-10-02 00:38:03 +00:00
|
|
|
scripting languages supported by
|
2018-01-22 23:52:21 +01:00
|
|
|
<a href="http://jakarta.apache.org/bsf" target="_top">Apache BSF</a>
|
2006-12-12 21:47:56 +00:00
|
|
|
or
|
|
|
|
<a href="https://scripting.dev.java.net">JSR 223</a>
|
|
|
|
may be
|
2003-10-02 00:38:03 +00:00
|
|
|
used to define the script. Scriptdef provides a mechanism to encapsulate
|
|
|
|
control logic from a build within an Ant task minimizing the need for
|
2004-11-12 10:36:29 +00:00
|
|
|
providing control style tasks in Ant itself. Complex logic can be made
|
2003-10-02 00:38:03 +00:00
|
|
|
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>
|
|
|
|
|
2018-01-24 21:40:28 +01:00
|
|
|
<p><strong>Note</strong>: This task depends on external libraries not included in the
|
2003-10-02 00:38:03 +00:00
|
|
|
Ant distribution. See
|
|
|
|
<a href="../install.html#librarydependencies">Library Dependencies</a>
|
|
|
|
for more information.</p>
|
|
|
|
|
2006-06-20 21:44:32 +00:00
|
|
|
|
|
|
|
|
2003-10-02 00:38:03 +00:00
|
|
|
<p>The attributes and nested elements supported by the task may be defined
|
2004-11-19 09:07:12 +00:00
|
|
|
using <code><attribute></code> and <code><element></code> nested elements. These are
|
2003-10-02 00:38:03 +00:00
|
|
|
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>
|
|
|
|
|
2018-01-24 21:40:28 +01:00
|
|
|
<p><strong>Note</strong>: Ant will turn all attribute and element names into all
|
2004-04-23 14:26:47 +00:00
|
|
|
lowercase names, so even if you use name="SomeAttribute", you'll have
|
|
|
|
to use "someattribute" to retrieve the attribute's value from the
|
|
|
|
<code>attributes</code> collection.</p>
|
|
|
|
|
2018-01-24 21:40:28 +01:00
|
|
|
<p>The name "self" (<em>since Ant 1.6.3</em>) is a pre-defined reference to the
|
2006-06-20 21:44:32 +00:00
|
|
|
script def task instance.
|
|
|
|
It can be used for logging, or for integration with the rest of
|
2018-01-22 23:52:21 +01:00
|
|
|
Ant. the <code>self.text attribute</code> contains
|
2006-06-20 21:44:32 +00:00
|
|
|
any nested text passed to the script</p>
|
|
|
|
|
2018-01-22 23:52:21 +01:00
|
|
|
<p>If an attribute or element is not passed in,
|
|
|
|
then <code>attributes.get()</code> or <code>elements.get()</code> will
|
|
|
|
return null. It is up to the script to perform any checks and validation.
|
|
|
|
<code>self.fail(String message)</code>can be used to raise a
|
2006-06-20 21:44:32 +00:00
|
|
|
<code>BuildException</code>.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
2003-10-02 00:38:03 +00:00
|
|
|
<p>The name "project" is a pre-defined reference to the Ant Project. For
|
|
|
|
more information on writing scripts, please refer to the
|
2004-11-19 09:07:12 +00:00
|
|
|
<a href="script.html"><code><script></code></a> task
|
2003-10-02 00:38:03 +00:00
|
|
|
</p>
|
|
|
|
|
2004-07-07 08:32:02 +00:00
|
|
|
|
2003-10-02 00:38:03 +00:00
|
|
|
<h3>Parameters</h3>
|
2018-01-22 23:52:21 +01:00
|
|
|
<table>
|
2003-10-02 00:38:03 +00:00
|
|
|
<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.
|
2006-12-12 21:47:56 +00:00
|
|
|
Must be a supported Apache BSF or JSR 223 language</td>
|
2003-10-02 00:38:03 +00:00
|
|
|
<td valign="top" align="center">Yes</td>
|
|
|
|
</tr>
|
2006-12-12 21:47:56 +00:00
|
|
|
<tr>
|
|
|
|
<td valign="top">manager</td>
|
|
|
|
<td valign="top">
|
|
|
|
The script engine manager to use.
|
2010-05-26 20:33:51 +00:00
|
|
|
See the <a href="../Tasks/script.html">script</a> task
|
2006-12-12 21:47:56 +00:00
|
|
|
for using this attribute.
|
|
|
|
</td>
|
|
|
|
<td valign="top" align="center">No - default is "auto"</td>
|
|
|
|
</tr>
|
2003-10-02 00:38:03 +00:00
|
|
|
<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>
|
Enhanced JavaxScriptRunner, Junit, docs, Javadoc
JavaxScriptRunner:
- Better compiled script reference name to avoid reference name
collision, project wide : execName + ".compiled." +
getScript().hashCode() + "." + getProject().hashCode();
ScriptDef:
- Fixed Javadoc + use of 3rd person
[(oracle style
guideline)](http://www.oracle.com/technetwork/articles/java/index-137868.html#styleguide)
Task manual: documentation for `encoding` and `compiled` options.
- `encoding` : script.html, scriptdef.html, conditions.html,
selectors.html, mapper.html, filterchain.html
- `compiled` : scriptdef.html
Added source file `encoding` option on:
- org.apache.tools.ant.taskdefs.optional.Script
- org.apache.tools.ant.types.optional.AbstractScriptComponent
(inherited by ScriptCondition, ScriptMapper)
- org.apache.tools.ant.types.optional.ScriptFilter
- org.apache.tools.ant.types.optional.ScriptSelector
Test units :
- org.apache.tools.ant.taskdefs.optional.script.ScriptDefTest,
script.xml : src + encoding
- org.apache.tools.ant.taskdefs.optional.RhinoScriptDefTest,
scriptdef.xml : src + encoding, compiled
- new file: src/etc/testcases/taskdefs/optional/script/heavy-script.js
2017-01-20 16:33:35 +01:00
|
|
|
<tr>
|
|
|
|
<td valign="top">encoding</td>
|
2018-01-24 21:40:28 +01:00
|
|
|
<td valign="top">The encoding of the script as a file. <em>since Ant 1.10.2</em>.</td>
|
Enhanced JavaxScriptRunner, Junit, docs, Javadoc
JavaxScriptRunner:
- Better compiled script reference name to avoid reference name
collision, project wide : execName + ".compiled." +
getScript().hashCode() + "." + getProject().hashCode();
ScriptDef:
- Fixed Javadoc + use of 3rd person
[(oracle style
guideline)](http://www.oracle.com/technetwork/articles/java/index-137868.html#styleguide)
Task manual: documentation for `encoding` and `compiled` options.
- `encoding` : script.html, scriptdef.html, conditions.html,
selectors.html, mapper.html, filterchain.html
- `compiled` : scriptdef.html
Added source file `encoding` option on:
- org.apache.tools.ant.taskdefs.optional.Script
- org.apache.tools.ant.types.optional.AbstractScriptComponent
(inherited by ScriptCondition, ScriptMapper)
- org.apache.tools.ant.types.optional.ScriptFilter
- org.apache.tools.ant.types.optional.ScriptSelector
Test units :
- org.apache.tools.ant.taskdefs.optional.script.ScriptDefTest,
script.xml : src + encoding
- org.apache.tools.ant.taskdefs.optional.RhinoScriptDefTest,
scriptdef.xml : src + encoding, compiled
- new file: src/etc/testcases/taskdefs/optional/script/heavy-script.js
2017-01-20 16:33:35 +01:00
|
|
|
<td valign="top" align="center">No - defaults to default JVM encoding</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td valign="top">compiled</td>
|
|
|
|
<td valign="top">If true, the script is compiled before the first
|
|
|
|
evaluation for faster multiple executions, on the condition that the <em>manager</em> is "javax" and the
|
|
|
|
target engine implements <code>javax.script.Compilable</code>.
|
2018-01-22 23:52:21 +01:00
|
|
|
Note that the <code>bsf</code> manager may automatically compiles the script.
|
2018-01-24 21:40:28 +01:00
|
|
|
<em>since Ant 1.10.2</em>.</td>
|
Enhanced JavaxScriptRunner, Junit, docs, Javadoc
JavaxScriptRunner:
- Better compiled script reference name to avoid reference name
collision, project wide : execName + ".compiled." +
getScript().hashCode() + "." + getProject().hashCode();
ScriptDef:
- Fixed Javadoc + use of 3rd person
[(oracle style
guideline)](http://www.oracle.com/technetwork/articles/java/index-137868.html#styleguide)
Task manual: documentation for `encoding` and `compiled` options.
- `encoding` : script.html, scriptdef.html, conditions.html,
selectors.html, mapper.html, filterchain.html
- `compiled` : scriptdef.html
Added source file `encoding` option on:
- org.apache.tools.ant.taskdefs.optional.Script
- org.apache.tools.ant.types.optional.AbstractScriptComponent
(inherited by ScriptCondition, ScriptMapper)
- org.apache.tools.ant.types.optional.ScriptFilter
- org.apache.tools.ant.types.optional.ScriptSelector
Test units :
- org.apache.tools.ant.taskdefs.optional.script.ScriptDefTest,
script.xml : src + encoding
- org.apache.tools.ant.taskdefs.optional.RhinoScriptDefTest,
scriptdef.xml : src + encoding, compiled
- new file: src/etc/testcases/taskdefs/optional/script/heavy-script.js
2017-01-20 16:33:35 +01:00
|
|
|
<td valign="top" align="center">No - defaults to false</td>
|
|
|
|
</tr>
|
2005-03-07 12:21:43 +00:00
|
|
|
<tr>
|
|
|
|
<td valign="top">uri</td>
|
|
|
|
<td valign="top">
|
|
|
|
The XML namespace uri that this definition should live in.
|
|
|
|
</td>
|
|
|
|
<td valign="top" align="center">No</td>
|
|
|
|
</tr>
|
2006-12-12 21:47:56 +00:00
|
|
|
<tr>
|
|
|
|
<td valign="top">classpath</td>
|
|
|
|
<td valign="top">
|
|
|
|
The classpath to pass into the script.
|
|
|
|
</td>
|
|
|
|
<td align="center" valign="top">No</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td valign="top">classpathref</td>
|
|
|
|
<td valign="top">The classpath to use, given as a
|
|
|
|
<a href="../using.html#references">reference</a> to a path defined elsewhere.
|
|
|
|
<td align="center" valign="top">No</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td valign="top">loaderRef</td>
|
|
|
|
<td valign="top">the name of the loader that is
|
2018-01-22 23:52:21 +01:00
|
|
|
used to load the script, constructed from the specified
|
2012-05-26 12:14:49 +00:00
|
|
|
classpath. This allows multiple script definitions
|
2006-12-12 21:47:56 +00:00
|
|
|
to reuse the same class loader.
|
|
|
|
</td>
|
|
|
|
<td align="center" valign="top">No</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
2003-10-02 00:38:03 +00:00
|
|
|
|
|
|
|
<h3>Nested elements</h3>
|
|
|
|
<h4>attribute</h4>
|
2018-01-22 23:52:21 +01:00
|
|
|
<table>
|
2003-10-02 00:38:03 +00:00
|
|
|
<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>
|
2018-01-22 23:52:21 +01:00
|
|
|
<table>
|
2003-10-02 00:38:03 +00:00
|
|
|
<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
|
2006-06-20 23:29:46 +00:00
|
|
|
to specifying the class name directly. If the type is in a namespace,
|
|
|
|
the URI and a : must be prefixed to the type. For example
|
|
|
|
<code>type="antlib:example.org:newtype"</code></td>
|
2003-10-02 00:38:03 +00:00
|
|
|
<td valign="top" align="center">No</td>
|
|
|
|
</tr>
|
2007-04-27 14:37:56 +00:00
|
|
|
<tr>
|
|
|
|
<td valign="top">any resource or resource collection</td>
|
2018-01-22 23:52:21 +01:00
|
|
|
<td valign="top"><em>Since Ant 1.7.1</em>, this task can load scripts
|
|
|
|
from any resource supplied as a nested element.</td>
|
2007-04-27 14:37:56 +00:00
|
|
|
<td valign="top" align="center">No</td>
|
|
|
|
</tr>
|
|
|
|
|
2003-10-02 00:38:03 +00:00
|
|
|
</table>
|
|
|
|
|
2006-12-12 21:47:56 +00:00
|
|
|
<h4>classpath</h4>
|
|
|
|
<p>
|
2010-05-26 20:33:51 +00:00
|
|
|
See the <a href="../Tasks/script.html">script</a> task
|
2006-12-12 21:47:56 +00:00
|
|
|
for using this nested element.
|
|
|
|
</p>
|
2003-10-02 00:38:03 +00:00
|
|
|
|
2007-04-27 14:37:56 +00:00
|
|
|
|
2003-10-02 00:38:03 +00:00
|
|
|
<h3>Examples</h3>
|
|
|
|
|
|
|
|
<p>
|
2004-11-12 10:36:29 +00:00
|
|
|
The following definition creates a task which supports an attribute called
|
2003-10-02 00:38:03 +00:00
|
|
|
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>
|
|
|
|
<scriptdef name="scripttest" language="javascript">
|
|
|
|
<attribute name="attr1"/>
|
|
|
|
<element name="fileset" type="fileset"/>
|
|
|
|
<element name="path" type="path"/>
|
|
|
|
<![CDATA[
|
|
|
|
|
2004-07-07 08:32:02 +00:00
|
|
|
self.log("Hello from script");
|
|
|
|
self.log("Attribute attr1 = " + attributes.get("attr1"));
|
|
|
|
self.log("First fileset basedir = "
|
2003-10-02 00:38:03 +00:00
|
|
|
+ elements.get("fileset").get(0).getDir(project));
|
|
|
|
|
|
|
|
]]>
|
|
|
|
</scriptdef>
|
|
|
|
|
|
|
|
<scripttest attr1="test">
|
|
|
|
<path>
|
|
|
|
<pathelement location="src"/>
|
|
|
|
</path>
|
|
|
|
<fileset dir="src"/>
|
|
|
|
<fileset dir="main"/>
|
|
|
|
</scripttest>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
The following variation on the above script lists the number of fileset elements
|
|
|
|
and iterates through them
|
|
|
|
</p>
|
|
|
|
<pre>
|
|
|
|
<scriptdef name="scripttest2" language="javascript">
|
|
|
|
<element name="fileset" type="fileset"/>
|
|
|
|
<![CDATA[
|
|
|
|
filesets = elements.get("fileset");
|
2004-07-07 08:32:02 +00:00
|
|
|
self.log("Number of filesets = " + filesets.size());
|
2003-10-02 00:38:03 +00:00
|
|
|
for (i = 0; i < filesets.size(); ++i) {
|
2004-07-07 08:32:02 +00:00
|
|
|
self.log("fileset " + i + " basedir = "
|
2003-10-02 00:38:03 +00:00
|
|
|
+ filesets.get(i).getDir(project));
|
|
|
|
}
|
|
|
|
]]>
|
|
|
|
</scriptdef>
|
|
|
|
|
|
|
|
<scripttest2>
|
|
|
|
<fileset dir="src"/>
|
|
|
|
<fileset dir="main"/>
|
|
|
|
</scripttest2>
|
|
|
|
</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
|
2004-11-19 09:07:12 +00:00
|
|
|
statement (scriptdef <code><scripttest2></code>; line 10)</code></p>
|
2003-10-02 00:38:03 +00:00
|
|
|
|
|
|
|
<p>
|
|
|
|
Script errors are only detected when a script task is actually executed.
|
|
|
|
</p>
|
2006-06-20 21:44:32 +00:00
|
|
|
<p>
|
2018-01-22 23:52:21 +01:00
|
|
|
The next example does uses nested text in Jython. It also declares
|
|
|
|
the script in a new xml namespace, which must be used to refer to
|
|
|
|
the task. Declaring scripts in a new namespace guarantees that Ant will
|
|
|
|
not create a task of the same (namespace,localname) name pair.
|
2006-06-20 21:44:32 +00:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<pre>
|
2006-12-12 21:47:56 +00:00
|
|
|
<target name="echo-task-jython">
|
2006-06-20 21:44:32 +00:00
|
|
|
<scriptdef language="jython"
|
|
|
|
name="echo"
|
|
|
|
uri="http://example.org/script">
|
|
|
|
<![CDATA[
|
|
|
|
self.log("text: " +self.text)
|
|
|
|
]]>
|
|
|
|
</scriptdef>
|
|
|
|
</target>
|
|
|
|
|
|
|
|
<target name="testEcho" depends="echo-task-jython"
|
|
|
|
xmlns:s="http://example.org/script">
|
|
|
|
<s:echo>nested text</s:echo>
|
|
|
|
</target>
|
|
|
|
</pre>
|
|
|
|
|
2006-12-12 21:47:56 +00:00
|
|
|
The next example shows the use of <classpath> and
|
|
|
|
"loaderref" to get access to the beanshell jar.
|
|
|
|
<pre>
|
|
|
|
<scriptdef name="b1" language="beanshell"
|
|
|
|
loaderref="beanshell-ref">
|
|
|
|
<attribute name="a"/>
|
|
|
|
<classpath
|
|
|
|
path="${user.home}/scripting/beanshell/bsh-1.3b1.jar"/>
|
|
|
|
self.log("attribute a is " + attributes.get("a"));
|
|
|
|
</scriptdef>
|
|
|
|
|
|
|
|
<scriptdef name="b2" language="beanshell"
|
|
|
|
loaderref="beanshell-ref">
|
|
|
|
<attribute name="a2"/>
|
|
|
|
self.log("attribute a2 is " + attributes.get("a2"));
|
|
|
|
</scriptdef>
|
|
|
|
|
|
|
|
<b1 a="this is an 'a'"/>
|
|
|
|
<b2 a2="this is an 'a2' for b2"/>
|
|
|
|
</pre>
|
2006-06-20 21:44:32 +00:00
|
|
|
<h3>Testing Scripts</h3>
|
|
|
|
|
|
|
|
<p>
|
2018-01-22 23:52:21 +01:00
|
|
|
The easiest way to test scripts is to use the
|
2006-06-20 21:44:32 +00:00
|
|
|
<a href="http://ant.apache.org/antlibs/antunit/">AntUnit</a> ant library.
|
2018-01-22 23:52:21 +01:00
|
|
|
This will run all targets in a script that begin with "test" (and their dependencies).</p>
|
2003-10-02 00:38:03 +00:00
|
|
|
|
|
|
|
</body>
|
2004-07-07 08:32:02 +00:00
|
|
|
</html>
|