How do I programmatically concatenate with ANT?
Let me preface by saying that I am new to ant and I am using version 1.6.5 if that matters.
I have a file with a list of files that I want to merge. The important part of my first attempt was this:
<target name="for-each">
<xmlproperty file="scripts.xml" collapseAttributes="true" />
<echo message="testing for-each"/>
<concat destfile="${out}" fixlastline="yes" eol="lf">
<foreach list="${scripts.src}" target="loop" param="var" delimiter=","/>
</concat>
</target>
<target name="loop">
<echo message="File :: ${var}"/>
<fileset file="${SRC_DIR}${var}" />
</target>
However, concat does not support the foreach element.
I don't just want to cut and paste a bunch of files into a concat element because it is reused and can be changed in the source file often, so I want to programmatically iterate over the script elements listed in my file.
What would be the correct syntax or how to do this?
source to share
I think your requirements are:
- load file from another XML file
- join this list file
If so, there is no reason why you should create your own procedural loop. You can do something like:
scripts.xml
<scripts>
<src>file1</src>
<src>file2</src>
</scripts>
build.xml
<xmlproperty file="scripts.xml" collapseAttributes="true" />
<concat destfile="${out}" fixlastline="yes" eol="lf">
<filelist files="${scripts.src}"/>
</concat>
is this a case?
source to share
This solution uses Ant 1.8.1 - I was trying to figure out how to concatenate multiple files in a specific order - and it turns out that the only way to do it is with a file. This is what I came up with:
For a file with a list of filenames:
files.list:
-------------
file1.txt
file2.txt
file3.txt
Load this file into an Ant property and use the chaining filter to suffix the lines with "," and to remove any line breaks:
<!-- put list in format for filelist element -->
<loadfile property="file.includes" srcFile="files.list">
<filterchain>
<suffixlines suffix=", "/>
<striplinebreaks/>
</filterchain>
</loadfile>
This will add the following value to the $ {file.includes} property: "file1.txt, file2.txt, file3.txt". This line is in the correct form to be used in the file list item now, so you can accomplish using the list file and the $ {file.includes} property:
<concat destfile="${dest.file}" fixlastline="yes">
<filelist dir="${basedir}" files="${file.includes}"/>
</concat>
Hope this helps someone.
source to share
Something like this might work (haven't tried it)
<foreach param="file" list="${files}" target="concat_one_file" inheritall="true"/>
<target name="concat_one_file">
<do_concat source="${file}" destination="destination.txt"/>
</target>
<macrodef name="do_concat">
<attribute name="source"/>
<attribute name="destination"/>
<sequential>
<concat destfile="@{destination}" append="yes">
<fileset file="@{source}" />
</concat>
</sequential>
</macrodef>
source to share