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:

  |- templates
       |- f1_01.handlebars
       |- f1_02.handlebars
       |- f1_03.handlebars
       |- f1_04.handlebars
  |- templates
       |- f2_01.handlebars
       |- f2_02.handlebars
       |- f2_03.handlebars
       |- f2_04.handlebars


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"/>
        <apply executable="${handlebars}" failonerror="false">
            <fileset dir="." includes="**/templates/">
                <include name="*.handlebars"/>
            <arg value="-f compiled-templates.js"/>


Also, oddly enough, the output file starts with a space character, which I cannot get rid of. Any help would be greatly appreciated.


source to share

4 answers

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"/>




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="${}/node_modules/handlebars/bin/handlebars" />
  <srcfile />
  <fileset dir="${webclient}/app/templates" includes="**/*.handlebars"/>
  <arg line="-f ${webclient}/app/templates/handlebars.templates.js -m -a" />





<arg line="-f compiled-templates.js"/>



<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.



All Articles