Running one command for multiple files with Ant
I am trying to compile a bunch of descriptor templates into one compiled file using ant. I have several folders, each containing about 4 templates, and I want to compile them all into one file. With folders like:
folder01
|- templates
|- f1_01.handlebars
|- f1_02.handlebars
|- f1_03.handlebars
|- f1_04.handlebars
folder02
|- templates
|- f2_01.handlebars
|- f2_02.handlebars
|- f2_03.handlebars
|- f2_04.handlebars
build.xml
I really want to run the command:
handlebars **/templates/*.handlebars -f compiled-templates.js
I tried the following but it seems only 1 file is in the output js file.
<macrodef name="handlebars">
<attribute name="target"/>
<sequential>
<apply executable="${handlebars}" failonerror="false">
<fileset dir="." includes="**/templates/">
<include name="*.handlebars"/>
</fileset>
<arg value="-f compiled-templates.js"/>
</apply>
</sequential>
</macrodef>
Also, oddly enough, the output file starts with a space character, which I cannot get rid of. Any help would be greatly appreciated.
I ended up with a task <concat>
to create one file from all templates and run the executable once in that file.
<concat destfile="all.handlebars" append="true">
<fileset dir="." includes="**/templates/">
<include name="*.handlebars"/>
</fileset>
</concat>
After searching stackoverflow many times and more importantly reading the docs, I came up with this solution that works.
<echo level="info" message="Pre Compiling templates" />
<apply parallel="true" failonerror="true" executable="node">
<arg value="${webclient.dir.build}/node_modules/handlebars/bin/handlebars" />
<srcfile />
<fileset dir="${webclient}/app/templates" includes="**/*.handlebars"/>
<arg line="-f ${webclient}/app/templates/handlebars.templates.js -m -a" />
</apply>
try:
...
<arg line="-f compiled-templates.js"/>
...
instead:
...
<arg value="-f compiled-templates.js"/>
...
Use a task <script>
where you can insert Javascript or Groovy code that does the iterative work. It is good practice to call some short scripts to help with such problems, as they are usually more expressive than the clever XML outline conventions.