ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dale Anson <>
Subject Re: How do I walk the task tree from Java?
Date Fri, 28 Oct 2005 17:02:26 GMT
Hi Ashley,

It is that hard, and maybe even harder.  Antelope has a progress bar to 
show how progress of a build.  It tries to count the number of tasks 
that will be executed prior to the build starting, then updates the 
progress bar based on the taskFinished build events, so basically, it is 
traversing the tree as you're attempting. Here are the comments at the 
top of that class:

"The progress bar show progress of a running Ant target by counting the 
of tasks that the target performs and incrementing the progress bar as each
task is completed. The assumption is that a single target will be called.
While this target may call other targets, the progress is based on the 
target. The initial target must be set prior to the build starting by 
Ant is not particularly friendly about providing information about itself --
Project does not tell which target is currently executing, BuildEvent 
does not
tell which target started the execution cycle, TaskContainer provides no
information about nested tasks, and so on. This class has a number of hacks
to get around these limitiations, some of which are strictly against the
spirit of Java programming (e.g., I use PrivilegedAccessor to read the 
fields in RuntimeConfigurable). These hacks make this a fragile class as it
is not strictly based on the public Ant API.
Ant 1.6 made it even harder. This class needs a bit more work."

This progress listener worked fairly well with Ant 1.5, not as well with 
Ant 1.6, but you might get some pointers from it since you're using 
1.5.2.  You do need access to the "children" field in 
RuntimeConfigurable, I didn't find a way to get it without resorting to 
cheating with PrivilegedAccessor.

You can see the code for the progress listener at:

The PrivilegedAccessor is at:

Hope this helps!


Ashley Williams wrote:

> Hi,
> I'm trying to write a method in java that takes a root target and  
> then walks down the task tree until it comes to the desired object,  
> but don't seem to find the api to do this. I sort of got it to work  
> in Ant 1.6.2 but wasn't really happy with the results as I still  
> don't fully understand what I've written:
> However I need to use Ant 1.5.2 which is missing two crucial methods:  
> RuntimeConfigurableWrapper.getChildren and  
> RuntimeConfigurable.getProxy() - is there anything I should be using  
> in it's place? Is my strategy the right one?
> I have a hunch that I'm doing the wrong thing as no tree should be  
> this difficult to walk as i seem to have made it.
> Many Thanks
> - AW
> /**
>          * Recursively searches the given task until a match is  found 
> for the given path.
>          * @param searchTask
>          * @param pathComponents
>          * @param pathIndex
>          * @return
>          */
>         private Object findInTask(Task searchTask, String[]  
> pathComponents, int pathIndex) {
>             Object resultObj;
>             boolean morePathComponents = pathIndex <  
> pathComponents.length;
>             if (morePathComponents) {
>                 resultObj = null;
>                 Enumeration children =  
> searchTask.getRuntimeConfigurableWrapper().getChildren();
>                 search:
>                 while (children.hasMoreElements()) {
>                     RuntimeConfigurable child =  (RuntimeConfigurable) 
> children.nextElement();
>                     if (child.getElementTag().equals(pathComponents 
> [pathIndex])) {
>                         // we have a match but need to check if  there 
> are more children to recurse
>                         Object childProxy = child.getProxy();
>                         if (childProxy instanceof Task) {
>                             resultObj = findInTask((Task)  childProxy, 
> pathComponents, pathIndex + 1);
>                         } else {
>                             resultObj = childProxy;
>                         }
>                         break search;
>                     }
>                 }
>             } else {
>                 resultObj = searchTask;
>             }
>             return resultObj;
>         }
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message