antcontrib foreach 并行执行不会引发错误 [英] antcontrib foreach executed in parallel does not raise errors

查看:28
本文介绍了antcontrib foreach 并行执行不会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 ant 脚本,当 antcontrib 的 foreach 任务的 parallel 设置为 true 时,我似乎无法找到使失败的方法.有任何想法吗?

I have the following ant script that I can't seem to find a way to make fail when parallel is set to true for antcontrib's foreach task. Any ideas?

<project name="asdf" >
    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
            <pathelement location="../lib/ant/ant-contrib-1.0b3.jar" />
        </classpath>
    </taskdef>
    <target name="build">
        <foreach target="exex-subant" param="foreach.dir" parallel="true" maxthreads="4" inheritall="true" list="1,2,3">
            <param name="target" value="build" />
        </foreach>
    </target>

    <target name="exex-subant">
        <fail>test</fail>
    </target>
</project>

推荐答案

出现这种情况是因为在并行执行时, 使用了 任务,但不设置failonany"属性,或者给出任何方式说明如果任何迭代失败,任务应该失败.

This occurs because when executed in parallel, <foreach> uses the <parallel> task, but does not set the "failonany" property, or give any way to say that the task should fail if any iteration failed.

幸运的是,有一个相对简单的解决方法,即使用 而不是 .在您的示例中,这看起来像这样:

Fortunately, there is a relatively easy workaround, which is to use <for> instead of <foreach>. In your example, that would look like this:

<target name="build">
    <for param="foreach.dir" parallel="true" list="1,2,3">
        <sequential>
            <antcall target="exex-subant" inheritall="true">
                <param name="target" value="build" />
                <param name="foreach.dir" value="@{foreach.dir}" />
            </antcall>
        </sequential>
    </for>
</target>

请注意,您必须显式传入 foreach.dir 属性,然后可以在 exex-subant 目标中以 ${foreach.dir} 的形式访问该属性.

Note that you have to explicitly pass in the foreach.dir property, which will then be accessible in the exex-subant target as ${foreach.dir}.

这将并行执行所有迭代,但如果其中任何一个失败,脚本就会失败(除了 for 任务之外,它不会执行任何操作).

This will execute all iterations in parallel, but the script will fail if any one of them fails (it will not execute anything beyond the for task).

请注意,为了使用 for 任务,您需要 ant 1.6 或更高版本,并且需要将您的 taskdef 更改为:

Note that in order to use the for task, you'll need ant 1.6 or higher, and will need to change your taskdef to:

<taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
        <pathelement location="../lib/ant/ant-contrib-1.0b3.jar" />
    </classpath>
</taskdef>

如果由于某种原因您需要支持旧版本的 ant,那么您必须稍微更改 exex-subant 目标,以便它在失败时更改某些内容.例如,您可以将 exex-subant 中的当前逻辑包装在 try/catch 中,并在 catch 块中创建一个文件.然后在 foreach 终止后,您可以检查该文件是否存在,如果存在则构建失败.这样,如果 foreach 的任何执行失败,则 ant 脚本将在 foreach 完成后失败.

If for some reason you need to support older versions of ant, then you would have to change the exex-subant target slightly, so that it changed something when it failed. For example, you could wrap the current logic in exex-subant within a try/catch, and in the catch block it could create a file. Then after the foreach terminates you can check to see if that file exists, and fail the build if it does. That way, if any execution of the foreach fails, the ant script will fail after the foreach finishes.

请注意,您不能在失败时仅在 exex-subant 中设置属性,因为该属性不会传播回 foreach 循环(这就是我建议创建文件的原因).但我强烈建议只使用 for 任务并且需要 ant 1.6 或更高版本.

Note that you can't just set a property in exex-subant on failure, since the property won't propagate back to the foreach loop (which is why I suggested creating a file). But I'd strongly recommend just using the for task and requiring ant 1.6 or higher.

这篇关于antcontrib foreach 并行执行不会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆