当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Ant - iterate on a folder to get run arguments

问题描述:

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.

网友答案:

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>
网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: