Enumerating Ant Targets Programatically

In my previous blog post, I provided some sample code showing how to execute ant targets programatically from Java. This code showed how to execute the default ant target.

Enumerating the targets is a similarly easy process. The code below shows how to iterate through all the ant targets within an ant build file.

Project p = new Project();
// Setup Project p
Hashtable table = p.getTargets();
Set set = table.keySet();
Iterator iter = set.iterator();
while (iter.hasNext()) {
    System.out.println("Target:"+(String)iter.next());
}