1

我正在使用 ant 来获取目录中的所有文件并仅并行执行 5 个文件,然后再执行 5 个文件。已经执行的文件不应再次执行。

<target name="ParallelTest" description="Checking the parallelTest">
        <for param="file" >
            <path> 
                <fileset dir="C:/RCBuild3/ofs/bin/prd1">
                        <include name="*.xml"/> 
                </fileset>
            </path>
            <sequential>
                <antcall target="parallelexecutoin">
                    <param name="productfile" value="@{file}"/>
                </antcall>
            </sequential>
        </for> 

    </target>
    <target name="parallelexecutoin">
        <exec dir="C:/RCBuild3/ofs/bin/" executable="cmd">
            <arg value="/c"/>
            <arg value="productupload.bat"/>
            <arg value="-fileName"/>
            <arg value="${productfile}"/>
        </exec>
    </target>    

上面的代码按顺序执行。

4

1 回答 1

2

先上代码:

<fileset dir="C:/RCBuild3/ofs/bin/prd1" id="src.files">
    <include name="*.xml"/> 
</fileset>

<pathconvert pathsep="," property="file.list" refid="src.files"/>

<for list="${file.list}" delimiter="," param="file" parallel="true" threadCount="5">
    <sequential>
        <antcall target="parallelexecutoin">
            <param name="productfile" value="@{file}"/>
        </antcall>
    </sequential>
</for>

解释:

首先,您fileset为所有需要处理的文件准备一个。

然后,使用pathconvert将文件集转换为属性“file.list”,例如:filename1.xml,filename2.xml,filename3.xml.

for任务的 java 代码(隐藏在 Ant 文件后面)将使用逗号将“file.list”拆分为List,并循环遍历List. 对于 中的每个元素List,循环体(sequential部分)将运行。

parallel告诉for任务使用多个线程运行循环体,并且threadcount是可以同时运行的最大线程数。

因此,使用parallel = trueand threadcount = 5,就像您所描述的那样:一次 5 个文件。

于 2013-03-27T07:11:35.833 回答