如何解析和解释ant的build.xml [英] How to parse and interpret ant's build.xml

查看:27
本文介绍了如何解析和解释ant的build.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有用于读取和 ant build.xml 并从中检索元素的 Ant API?具体来说,我希望能够检索路径元素中的值并能够遍历路径中的所有元素.

Is there an Ant API for reading and ant build.xml and retrieving elements from it? Specifically I want to be able to retrieve the values in a path element and be able to walk all of the elements in the path.

我的目的是检索给定路径并确保在清单中正确引用它,以便在产品投入生产时构建和清单匹配.

My purpose is to retrieve a given path and ensure that it is referenced correctly in a manifest, so that the build and the manifest match when the product goes out to production.

关于使用 XML API 的响应(并感谢您),问题是当前构建的构建文件比这更复杂.即类路径引用不同的类路径并包含它,并且类路径中引用的元素本身定义在一个属性文件中,因此有太多的 Ant API 无法合理重新创建.

Regarding the responses (and thank you for them) to use an XML API, the problem is that the build file as currently constructed is more complex than that. Namely the classpath references a different classpath and includes it and the elements referenced in the classpath are themselves defined in a properties file, so there is too much of the Ant API to reasonably recreate.

推荐答案

您可以使用 ProjectHelper 类通过构建文件配置您的项目.如果您要检查的路径包含在引用中,那么您可以通过其 ID 从项目中获取引用.

You can use the ProjectHelper class to configure your project with a buildfile. If the path you want to check is contained in a reference, then you can just get the reference from the project by its ID.

例如,如果您的 build.xml 中有这样的内容:

For example, if you have something like this in your build.xml:

<path id="classpath">
    <fileset dir="${basedir}/lib" includes="*.jar"/>
</path>

然后就可以通过如下代码获取Path引用:

Then you can get the Path reference with the following code:

import java.io.File;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import org.apache.tools.ant.types.Path;

public class Test {
    public static void main(String[] args) throws Exception {    
        Project project = new Project();
        File buildFile = new File("build.xml");
        project.init();
        ProjectHelper.configureProject(project, buildFile);

        Path path = (Path) project.getReference("classpath");
     }
}

请注意,ProjectHelper.configureProject 在 ant 1.6.2 中已弃用,但在 1.7 中未弃用.

Note that ProjectHelper.configureProject is deprecated in ant 1.6.2, but not in 1.7.

这篇关于如何解析和解释ant的build.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆