Reputation: 10841
I am using Ant to build my software. Ant creates an Xml file with the echoxml
function and then provides the created xml file to another special program.
Now I would like to list the content of a directory in the echoxml
tag.
How can this be done? My final xml file should look something like this, only providing a path to the directory:
<SomeXmlTag>
<cp>directory/firstFile.jar</cp>
cp>directory/secondFile.jar</cp>
<cp>directory/XYZFile.jar</cp>
</SomeXmlTag>
Upvotes: 1
Views: 323
Reputation: 17640
EchoXML is an ant tast which is already specified to take a nested xml string as its contents, outputting those contents directly to a file (or to the console). So according to the documentation, it doesn't satisfy your needs.
However, it is pretty simple to modify a standard ant task to do this using the javax xml extensions and along with a the File api.
First, See http://ant.apache.org/manual/Tasks/echoxml.html, and confirm that EchoXML does not do what you need it to do.
Now, a simple solution is to implement your own xml writer task ... to simply write your own class :
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);
}
}
And then add this to your build :
<target name="use" description="MyFileTreeWriter task" depends="jar">
<taskdef name="writeDirs" classname="MyFileTreeWriter" classpath="${ant.project.name}.jar"/>
</target>
Upvotes: 1