Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Creating ANT task which invokes target with dynamic properties

Thread Tools

Creating ANT task which invokes target with dynamic properties

Posts: n/a
I want to create an Ant task which loops through subdirectories and
then calls an Ant target this is easy enough through the
project.executeTarget method. But I want to pass the subdirectory
name as a property to the "called" target. This I do through the
project.setProperty method. Yet the target only has the original
value of the property and not the modified value. I am assuming that
this is because once a target and its tasks are loaded, the properties
are then fixed immutable.

So my strategy is to "refresh" the target before my task executes the
target. I was thinking to create a new target, do a shallow copy of
info, copy to the project and then execute the new target. Thoughts?
Other ideas?

Here is the sample ANT build code:

<target name="build" >
<IterateDirectoryTask RootDirectory="."

<target name="TestTarget" >
<echo message="Hello from TestTarget" />
<echo message=" subdirectory = ${subdirectory}" />

And here is the TASK:

public class IterateDirectoryTask extends Task {
private String _rootDirectory;
private String _executeTargetName;

public void setRootDirectory(String aDirectoryName) {
_rootDirectory = aDirectoryName;

public void setExecuteTarget(String aTargetName) {
_executeTargetName = aTargetName;

public void execute() throws BuildException {
if (_executeTargetName == null) {
throw new BuildException("ExecuteTarget must be

File lRootDirectory = new File(_rootDirectory);
if (!lRootDirectory.isDirectory()) {
throw new BuildException("'"+_rootDirectory+"' is
not a directory");

File[] lFiles = lRootDirectory.listFiles();
for (int i=0; i < lFiles.length; i++) {
if (lFiles[i].isDirectory()) {
Target lTarget =
(Target)project.getTargets().get(_executeTargetNam e);
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing ant task name when running a task teggy Java 0 05-29-2007 02:20 PM
A java class invokes a JSP? Mich Java 5 11-02-2006 03:59 AM
Maven using ANT plugin for SCP task : Embedded error: Could not create task or type of type: scp. krabhi Java 1 08-09-2006 04:19 PM
Re: Problem in ant replace task and replaceregexp task to update xml Victor Java 0 09-01-2004 03:58 AM
junit ant task problems loading up a .properties file Scout Java 0 08-04-2003 05:30 PM