Java 从ant构建文件以编程方式检索某些任务

Java 从ant构建文件以编程方式检索某些任务,java,xml,parsing,ant,build,Java,Xml,Parsing,Ant,Build,我有一个build.xml,它导入其他ant xml文件。我想从中获取所有javac任务,这样我就可以看到这些任务的类路径设置(javac用于多个目标)。我提出了以下代码(简化了一点): publicstaticvoidmain(字符串[]args)引发异常{ 项目=新项目(); project.init(); String build=“build.xml”; 文件构建文件=新文件(构建); ProjectHelper.configureProject(项目,构建文件); Hashtableh

我有一个build.xml,它导入其他ant xml文件。我想从中获取所有javac任务,这样我就可以看到这些任务的类路径设置(javac用于多个目标)。我提出了以下代码(简化了一点):

publicstaticvoidmain(字符串[]args)引发异常{
项目=新项目();
project.init();
String build=“build.xml”;
文件构建文件=新文件(构建);
ProjectHelper.configureProject(项目,构建文件);
Hashtableht=project.getTargets();
对于(字符串键:ht.keySet()){
试试{
Target Target=(Target)ht.get(key);
Task[]tasks=target.getTasks();
对于(任务:任务){
if(未知元素的任务实例){
((未知元素)任务)。可以配置();
任务=((未知元素)任务).getTask();
如果(任务==null){
返回;
}                                                                                                                                                                                            
}                                                                                                                                                                                                
if(Javac的任务实例){
//我们开始
}                                                                                                                                                                                                
}                                                                                                                                                                                                    
}捕获(异常忽略){}
}                                                                                                                                                                                                            
}
但是,有些任务(如
MacroDef
)可能嵌套了其他任务。
TaskContainer
界面只指定了
addTask(task)
,我看不到检索嵌套任务的方法

如何检索所有javac任务?有一个不使用ant库的解决方案是可以的,但是XML解析似乎很麻烦,因为ant使用属性、引用、构建文件可以导入其他文件等。

public static void main(String[] args) throws Exception { Project project = new Project(); project.init(); String build = "build.xml"; File buildFile = new File(build); ProjectHelper.configureProject(project, buildFile); Hashtable<String,Object>ht = project.getTargets(); for (String key : ht.keySet()) { try { Target target = (Target)ht.get(key); Task[] tasks = target.getTasks(); for (Task task : tasks) { if (task instanceof UnknownElement) { ((UnknownElement)task).maybeConfigure(); task = ((UnknownElement)task).getTask(); if (task == null) { return; } } if (task instanceof Javac) { // here we go } } } catch(Exception ignore) {} } }
            if (task instanceof Javac) {
                Javac javac = (Javac)task;
                Path path = javac.getClasspath();
                if (path == null) {
                  System.out.println("javac: Path is null");
                } else {
                  System.out.println("javac: Path is " + path.toString());
                }
            }
            if (task instanceof MacroDef) {
              MacroDef macroDef = (MacroDef)task;
              // a Sequence element, but not really, as `unkSeq.getRealThing()` will return null
              UnknownElement unkSeq = macroDef.getNestedTask();
              // Make sure we are dealing with the wrapper, or we won't get very far
              RuntimeConfigurable wrapper = unkSeq.getWrapper();
              // Wrappers can be configured too.
              wrapper.maybeConfigure(project, true);
              Enumeration enumeration = wrapper.getChildren();
              while (enumeration.hasMoreElements()) {
                // children of the wrapper
                RuntimeConfigurable child = (RuntimeConfigurable) enumeration.nextElement();
                UnknownElement unkchild = (UnknownElement)child.getProxy();
                // you can use this to print the name
                System.out.println("child(wrapper): " + unkchild.getTaskName());
                // this will be null, as the macro hasn't "executed"
                System.out.println("child(real): " + unkchild.getRealThing());
              }
            }