2

I have an ant script that compile my program, build the jar and then run it. My program takes one argument that is a file.

In my run target I would like to be able to execute my jar with all the files that are in a specified folder, one by one, as argument.

Do I have to write all the different cases by hand like:

<target name="run" depends="jar">
     <mkdir dir="${output.dir}" />
     <java jar="${myjar}.jar" output="${output.dir}/${test1}" fork="true">
          <arg value="${test.dir}/${test1}" />
     </java>
     <java jar="${myjar}.jar" output="${output.dir}/${test2}" fork="true">
          <arg value="${test.dir}/${test2}" />
     </java>

     #and so on

</target>

or is there a way to maybe iterate over my test directory and save me the writing of 20 other cases?

Thanks.

talnicolas
  • 13,885
  • 7
  • 36
  • 56

2 Answers2

1

I found my solution using the <apply> tag (doc).

My script looks like it now and every execution is outputted in separate files thanks to the <redirector> tag:

<target name="run" depends="jar">
    <mkdir dir="${output.dir}" />
    <apply executable="java">
         <fileset dir="${test.dir}" />
         <arg value="-jar"/>
         <arg path="${jar.dir}/${myjar}.jar" />
         <srcfile />
         <redirector>
              <outputmapper id="out" type="glob" from="*" to="${output.dir}/*" />
         </redirector>
    </apply>
</target>
talnicolas
  • 13,885
  • 7
  • 36
  • 56
0

The foreach tag in ant-contrib does what you want. Give it a fileset and point it at the target that runs your jar.

Nialscorva
  • 2,924
  • 2
  • 15
  • 16
  • yes I didn't want to have to install something else... I just found the answer I couldn't find before, http://stackoverflow.com/questions/1467991/ant-how-to-execute-a-command-for-each-file-in-directory, that uses the `apply` tag. I'll try it. – talnicolas Nov 29 '11 at 04:46