2001-02-13 12:32:01 +00:00
< html >
< head >
2004-09-17 12:53:48 +00:00
< meta http-equiv = "Content-Language" content = "en-us" > < / meta >
2005-04-29 18:58:16 +00:00
< link rel = "stylesheet" type = "text/css" href = "../stylesheets/style.css" >
2002-02-03 22:11:39 +00:00
< title > Script Task< / title >
2001-02-13 12:32:01 +00:00
< / head >
< body >
< h2 > < a name = "script" > Script< / a > < / h2 >
< h3 > Description< / h3 >
2003-09-12 14:15:17 +00:00
< p > Execute a script in a
2002-11-14 07:48:25 +00:00
< a href = "http://jakarta.apache.org/bsf" target = "_top" > Apache BSF< / a > supported language.< / p >
2001-08-30 13:23:14 +00:00
< 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 >
2001-02-13 12:32:01 +00:00
< p > All items (tasks, targets, etc) of the running project are
accessible from the script, using either their < code > name< / code > or
2002-06-01 12:26:43 +00:00
< code > id< / code > attributes (as long as their names are considered
2003-09-12 14:15:17 +00:00
valid Java identifiers, that is).
2006-09-02 23:11:04 +00:00
This is controlled by the "setbeans" attribute of the task.
2002-06-01 12:26:43 +00:00
The name "project" is a pre-defined reference to the Project, which can be
2003-06-16 10:09:09 +00:00
used instead of the project name. The name "self" is a pre-defined reference to the actual
2005-04-29 18:58:16 +00:00
< code > < script> < / code > -Task instance.< br > From these objects you have access to the Ant Java API, see the
2003-06-16 10:09:09 +00:00
< a href = "../api/index.html" > JavaDoc< / a > (especially for
< a href = "../api/org/apache/tools/ant/Project.html" > Project< / a > and
< a href = "../api/org/apache/tools/ant/taskdefs/optional/Script.html" > Script< / a > ) for more information.< / p >
< p > If you are using JavaScript a good resource is < a target = "_blank" href = "http://www.mozilla.org/rhino/doc.html" >
http://www.mozilla.org/rhino/doc.html< / a > as we are using their JavaScript interpreter.< / p >
2001-02-13 12:32:01 +00:00
< p > Scripts can do almost anything a task written in Java could do.< / p >
2004-09-15 13:17:12 +00:00
< p > Rhino provides a special construct - the < i > JavaAdapter< / i > . With that you can
2003-09-12 14:15:17 +00:00
create an object which implements several interfaces, extends classes and for which you
can overwrite methods. Because this is an undocumented feature (yet), here is the link
to an explanation: < a href = "http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&newwindow=1&frame=right&th=610d2db45c0756bd&seekm=391EEC3C.5236D929%40yahoo.com#link2" >
Groups@Google: "Rhino, enum.js, JavaAdapter?"< / a > by Norris Boyd in the newsgroup
< i > netscape.public.mozilla.jseng< / i > .< / p >
2001-02-13 12:32:01 +00:00
< 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" > language< / td >
< td valign = "top" > The programming language the script is written in.
2002-11-14 07:48:25 +00:00
Must be a supported Apache BSF language< / td >
2002-07-09 21:06:15 +00:00
< td valign = "top" align = "center" > Yes< / td >
2001-02-13 12:32:01 +00:00
< / 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 >
2006-09-02 23:11:04 +00:00
< tr >
< td valign = "top" > setbeans< / td >
< td valign = "top" >
This attribute controls whether to set variables for
all properties, references and targets in the running script.
If this attribute is false, only the the "project" and "self" variables are set.
If this attribute is true all the variables are set. The default value of this
attribute is "true". < em > Since Ant 1.7< / em >
< / td >
< td valign = "top" align = "center" > No< / td >
< / tr >
< tr >
< td valign = "top" > classpath< / td >
< td valign = "top" >
The classpath to pass into the script. < em > Since Ant 1.7< / em >
< / 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.
< em > Since Ant 1.7< / em > < / td >
< td align = "center" valign = "top" > No< / td >
< / tr >
2001-02-13 12:32:01 +00:00
< / table >
2006-09-02 23:11:04 +00:00
< h3 > Parameters specified as nested elements< / h3 >
< h4 > classpath< / h4 >
< p > < em > Since Ant 1.7< / em > < / p >
< p >
< code > Script< / code > 's < code > classpath< / code > attribute is a
< a href = "../using.html#path" > path-like structure< / a > and can also be set via a nested
< code > < classpath> < / code > element.
< p >
If a classpath is set, it will be used as the current thread context classloader, and
as the classloader given to the BSF manager. This means that it can be used to specify
the classpath containing the language implementation. This can be usefull if one wants
to keep ${user.home}/.ant/lib free of lots of scripting language specific jar files.
< / p >
< p >
< b > NB:< / b > This classpath cannot (currently) be used to specify the location of
the BSF jar file.
< / p >
< / p >
2001-02-13 12:32:01 +00:00
< h3 > Examples< / h3 >
2004-09-17 12:53:48 +00:00
The following snippet shows use of five different languages:
2004-09-15 13:17:12 +00:00
< blockquote > < pre >
< property name="message" value="Hello world"/>
< script language="groovy">
println("message is " + message)
< /script>
< script language="beanshell">
System.out.println("message is " + message);
< /script>
2004-09-15 14:19:38 +00:00
< script language="judoscript">
println 'message is ', message
< /script>
2004-09-17 12:53:48 +00:00
< script language="ruby">
print 'message is ', $message, "\n"
< /script>
2004-09-15 13:17:12 +00:00
< script language="jython">
print "message is %s" % message
< /script>
< / pre >
< / blockquote >
< p >
Note that for the < i > jython< / i > example, the script contents < b > must< / b >
start on the first column.
< / p >
2006-09-02 23:11:04 +00:00
< p >
Note also that for the < i > ruby< / i > example, the names of the set variables are prefixed
by a '$'.
2004-09-17 12:53:48 +00:00
< p >
2004-11-12 10:36:29 +00:00
The following script shows a little more complicated jruby example:
2004-09-17 12:53:48 +00:00
< / p >
< blockquote > < pre >
< script language="ruby">
xmlfiles = Dir.new(".").entries.delete_if { |i| ! (i =~ /\.xml$/) }
xmlfiles.sort.each { |i| $self.log(i) }
< /script>
< / pre >
< / blockquote >
< p >
The same example in groovy is:
< / p >
< blockquote > < pre >
< script language="groovy">
xmlfiles = new java.io.File(".").listFiles().findAll{ it =~ "\.xml$"}
xmlfiles.sort().each { self.log(it.toString())}
< /script>
2006-09-02 23:11:04 +00:00
< / pre >
< / blockquote >
< p >
The following example shows the use of classpath to specify the location
of the beanshell jar file.
< / p >
< blockquote > < pre >
< script language="beanshell" setbeans="true">
< classpath>
< fileset dir="${user.home}/lang/beanshell" includes="*.jar" />
< /classpath>
System.out.println("Hello world");
< /script>
2004-09-17 12:53:48 +00:00
< / pre >
< / blockquote >
2004-09-15 13:17:12 +00:00
< p >
The following script uses javascript to create a number of
echo tasks and execute them.
< / p >
2001-02-13 12:32:01 +00:00
< blockquote > < pre >
< project name=" squares" default=" main" basedir=" ." >
2004-09-15 13:17:12 +00:00
< target name=" main" >
2001-02-13 12:32:01 +00:00
< script language=" javascript" > < ![CDATA[
for (i=1; i< =10; i++) {
echo = squares.createTask(" echo" );
echo.setMessage(i*i);
2004-09-15 13:17:12 +00:00
echo.perform();
2001-02-13 12:32:01 +00:00
}
]]> < /script>
< /target>
< /project>
< / pre > < / blockquote >
< p > generates< / p >
< blockquote > < pre >
main:
1
4
9
16
25
36
49
64
81
100
BUILD SUCCESSFUL
< / pre > < / blockquote >
< p > Another example, using < a href = "../using.html#references" > references by id< / a >
and two different scripting languages:< / p >
< blockquote > < pre >
< project name=" testscript" default=" main" >
< target name=" sub" >
< echo id=" theEcho" />
< /target>
< target name=" sub1" >
< script language=" netrexx" > < ![CDATA[
theEcho.setMessage(" In sub1" )
sub.execute
]]> < /script>
< /target>
< target name=" sub2" >
< script language=" javascript" > < ![CDATA[
theEcho.setMessage(" In sub2" );
sub.execute();
]]> < /script>
< /target>
< target name=" main" depends=" sub1,sub2" />
< /project>
< / pre > < / blockquote >
< p > generates< / p >
< blockquote > < pre >
sub1:
In sub1
sub2:
In sub2
main:
BUILD SUCCESSFUL
< / pre > < / blockquote >
2003-06-16 10:09:09 +00:00
< p > Now a more complex example using the Java API and the Ant API. The goal is to list the
2004-11-19 09:07:12 +00:00
filesizes of all files a < code > < fileset/> < / code > caught.< / p >
2003-06-16 10:09:09 +00:00
< blockquote > < pre >
< ?xml version="1.0" encoding="ISO-8859-1"?>
< project name="< font color = blue > MyProject< / font > " basedir="." default="main">
< property name="fs.dir" value="src"/>
< property name="fs.includes" value="**/*.txt"/>
< property name="fs.excludes" value="**/*.tmp"/>
< target name="main">
< script language="javascript"> < ![CDATA[
// import statements
< font color = blue > // importPackage(java.io)< / font > ;
< font color = blue > importClass(java.io.File)< / font > ;
// Access to Ant-Properties by their names
dir = < font color = blue > project< / font > .getProperty("fs.dir");
includes = < font color = blue > MyProject< / font > .getProperty("fs.includes");
excludes = < font color = blue > self.getProject()< / font > .< font color = blue > getProperty("fs.excludes")< / font > ;
2004-11-19 09:07:12 +00:00
// Create a < fileset dir="" includes=""/>
2003-06-16 10:09:09 +00:00
fs = project.< font color = blue > createDataType("fileset")< / font > ;
fs.setDir( new File(dir) );
< font color = blue > fs.setIncludes(includes)< / font > ;
fs.setExcludes(excludes);
2003-11-25 11:30:45 +00:00
// Get the files (array) of that fileset
2003-06-16 10:09:09 +00:00
ds = fs.getDirectoryScanner(project);
srcFiles = ds.getIncludedFiles();
// iterate over that array
for (i=0; i< srcFiles.length; i++) {
// get the values via Java API
var basedir = fs.getDir(project);
var filename = srcFiles[i];
var file = < font color = blue > new File(basedir, filename)< / font > ;
var size = file.length();
// create and use a Task via Ant API
echo = MyProject.< font color = blue > createTask("echo")< / font > ;
echo.setMessage(filename + ": " + size + " byte");
echo.< font color = blue > perform()< / font > ;
}
]]> < /script>
< /target>
< /project>
< / pre > < / blockquote >
2005-03-01 23:22:01 +00:00
< p > We want to use the Java API. Because we don't want always typing the package signature
2003-11-25 11:30:45 +00:00
we do an import. Rhino knows two different methods for import statements: one for packages
2003-09-12 14:15:17 +00:00
and one for a single class. By default only the < i > java< / i > packages are available, so
< i > java.lang.System< / i > can be directly imported with < code > importClass/importPackage< / code > .
2005-03-01 23:22:01 +00:00
For other packages you have to prefix the full classified name with < i > Packages< / i > .
For example Ant's < i > FileUtils< / i > class can be imported with
< code > importClass(< b > Packages< / b > .org.apache.tools.ant.util.FileUtils)< / code >
2003-09-12 14:15:17 +00:00
< br >
2004-11-19 09:07:12 +00:00
The < code > < script> < / code > task populates the Project instance under
2003-06-16 10:09:09 +00:00
the name < i > project< / i > , so we can use that reference. Another way is to use its given name
or getting its reference from the task itself.< br >
The Project provides methods for accessing and setting properties, creating DataTypes and
Tasks and much more.< br >
After creating a FileSet object we initialize that by calling its set-methods. Then we can
2004-11-19 09:07:12 +00:00
use that object like a normal Ant task (< code > < copy> < / code > for example).< br >
2003-06-16 10:09:09 +00:00
For getting the size of a file we instantiate a < code > java.io.File< / code > . So we are using
normal Java API here.< br >
2004-11-19 09:07:12 +00:00
Finally we use the < code > < echo> < / code > task for producing the output. The task is not executed by
2003-06-16 10:09:09 +00:00
its execute() method, because the perform() method (implemented in Task itself) does the
2004-11-12 10:36:29 +00:00
appropriate logging before and after invoking execute().
2003-06-16 10:09:09 +00:00
< / p >
2001-02-13 12:32:01 +00:00
< hr >
2006-09-02 23:11:04 +00:00
< p align = "center" > Copyright © 2000-2006 The Apache Software Foundation. All rights
2001-02-13 12:32:01 +00:00
Reserved.< / p >
< / body >
2004-09-15 13:17:12 +00:00
< / html >