我正在使用 Ant 来构建我的软件。 Ant 使用 echoxml
创建一个 Xml 文件函数,然后将创建的 xml 文件提供给另一个特殊程序。
现在我想列出 echoxml
中的目录内容标签。
如何做到这一点?我的最终 xml 文件应如下所示,仅提供目录路径:
<SomeXmlTag>
<cp>directory/firstFile.jar</cp>
cp>directory/secondFile.jar</cp>
<cp>directory/XYZFile.jar</cp>
</SomeXmlTag>
最佳答案
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>
关于java - Ant 以自定义格式列出目录内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7932879/