【问题标题】:Ant list directory content in a custom format自定义格式的 Ant 列表目录内容
【发布时间】:2011-12-17 11:41:31
【问题描述】:

我正在使用 Ant 构建我的软件。 Ant 使用echoxml 函数创建一个 XML 文件,然后将创建的 xml 文件提供给另一个特殊程序。

现在我想在echoxml 标签中列出一个目录的内容。

如何做到这一点?我最终的 xml 文件应该是这样的,只提供目录的路径:

<SomeXmlTag>

<cp>directory/firstFile.jar</cp>

cp>directory/secondFile.jar</cp>

<cp>directory/XYZFile.jar</cp>

</SomeXmlTag>

【问题讨论】:

    标签: java xml deployment ant build


    【解决方案1】:

    EchoXML 是一个 ant tast,它已经指定将嵌套的 xml 字符串作为其内容,将这些内容直接输出到文件(或控制台)。因此,根据文档,它不能满足您的需求。

    但是,使用 javax xml 扩展和 File api 来修改标准 ant 任务非常简单。

    首先,请参阅http://ant.apache.org/manual/Tasks/echoxml.html,并确认 EchoXML 不会执行您需要它执行的操作。

    现在,一个简单的解决方案是实现您自己的 xml 编写器任务……简单地编写您自己的类:

    public class MyFileTreeWriter extends Task {
        public void execute() {
               File dirs = new File("./");
               //Alternatively, you can use apache's FileUtils directory walkers https://commons.apache.org/io/api-1.4/index.html?org/apache/commons/io/DirectoryWalker.html      
    
      // Psuedo code below,  uses standard javax.xml.* packages ... 
      for (String file : dirs.listFiles()){
      Element em = document.createElement("file");
      em.appendChild(document.createTextNode(file);
      rootElement.appendChild(em); 
    
        }
    }
    

    然后将其添加到您的构建中:

    <target name="use" description="MyFileTreeWriter task" depends="jar">
        <taskdef name="writeDirs" classname="MyFileTreeWriter" classpath="${ant.project.name}.jar"/>
    </target>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      • 2015-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多