From 59ad4c1654ec703e773d8e7dce593e57f7699690 Mon Sep 17 00:00:00 2001
From: Peter Reilly
Date: Mon, 16 Jun 2003 10:09:09 +0000
Subject: [PATCH] Added some more documentation to the script task. PR: 20805
Author: Jan Materne
git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274672 13f79535-47bb-0310-9956-ffa450edef68
---
docs/manual/OptionalTasks/script.html | 82 ++++++++++++++++++++++++++-
1 file changed, 79 insertions(+), 3 deletions(-)
diff --git a/docs/manual/OptionalTasks/script.html b/docs/manual/OptionalTasks/script.html
index 3287ca174..f2147d391 100644
--- a/docs/manual/OptionalTasks/script.html
+++ b/docs/manual/OptionalTasks/script.html
@@ -18,7 +18,13 @@ accessible from the script, using either their name
or
id
attributes (as long as their names are considered
valid Java identifiers, that is).
The name "project" is a pre-defined reference to the Project, which can be
-used instead of the project name.
+used instead of the project name. The name "self" is a pre-defined reference to the actual
+<script>-Task instance.
From these objects you have access to the Ant Java API, see the
+JavaDoc (especially for
+Project and
+Script) for more information.
+If you are using JavaScript a good resource is
+http://www.mozilla.org/rhino/doc.html as we are using their JavaScript interpreter.
Scripts can do almost anything a task written in Java could do.
Parameters
@@ -117,10 +123,80 @@ main:
BUILD SUCCESSFUL
+Now a more complex example using the Java API and the Ant API. The goal is to list the
+filesizes of all files a <fileset/> caught.
+
+
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<project name="MyProject" 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
+ // importPackage(java.io);
+ importClass(java.io.File);
+
+ // Access to Ant-Properties by their names
+ dir = project.getProperty("fs.dir");
+ includes = MyProject.getProperty("fs.includes");
+ excludes = self.getProject() .getProperty("fs.excludes");
+
+ // Create a <fileset dir="" includes="" />
+ fs = project.createDataType("fileset");
+ fs.setDir( new File(dir) );
+ fs.setIncludes(includes);
+ fs.setExcludes(excludes);
+
+ // Get the files of that fileset
+ ds = fs.getDirectoryScanner(project);
+
+ // Get the source files (array)
+ 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 = new File(basedir, filename);
+ var size = file.length();
+
+ // create and use a Task via Ant API
+ echo = MyProject.createTask("echo");
+ echo.setMessage(filename + ": " + size + " byte");
+ echo.perform();
+ }
+ ]]></script>
+ </target>
+</project>
+
+We want to use the Java API. Because we donīt want always typing the package signature
+we do an import. Rhino knows to different methods for import statements: one for packages
+and one for a single class.
+The <script> task populates the Project instance under
+the name project, so we can use that reference. Another way is to use its given name
+or getting its reference from the task itself.
+The Project provides methods for accessing and setting properties, creating DataTypes and
+Tasks and much more.
+After creating a FileSet object we initialize that by calling its set-methods. Then we can
+use that object like a normal Ant task (<copy> for example).
+For getting the size of a file we instantiate a java.io.File
. So we are using
+normal Java API here.
+Finally we use the <echo> task for producing the output. The task is not executed by
+its execute() method, because the perform() method (implemented in Task itself) does the
+apropriate logging before and after invoking execute().
+
+
+
-Copyright © 2000-2002 Apache Software Foundation. All rights
+
Copyright © 2000-2003 Apache Software Foundation. All rights
Reserved.