subant failonerror微妙之处

时间:2009-01-30 22:09:54

标签: ant

我有ant代码,可以在所有子目录中启动发布版本:

<target name="all-release" >
    <subant target="sub-release" failonerror="true">
        <fileset dir="." includes="*/build.xml" />
    </subant>
</target>

如上所述,如果任何单个构建失败,则all-release将快速失败(以后的构建都不会成功。如果我切换failonerror =“false”,则all-release将一直成功。事实证明所有的子构建都是独立的,所以我真正想要的是:

运行所有子版本构建,然后如果一个或多个子版本发生故障,则所有版本都会失败(理想情况下会出现关于哪些版本失败的错误消息)。

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

建议您查看ant-contrib任务中可用的扩展程序。

“for”任务可能会根据您的要求进行调整。

使用ant-contrib taskdef的'all-release'目标可能如下所示:

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

<target name="all-release">
    <for keepgoing="true" param="file">
        <path>
            <fileset dir="." includes="*/build.xml" />
        </path>
        <sequential>
            <ant antfile="@{file}" target="sub-release" />
        </sequential>
    </for>
</target>

使用其他一些ant-contrib功能,可能会获得失败列表。

build.xml上面的示例日志:

$ ant all-release
Buildfile: build.xml

all-release:
     [echo] /work/Scratch/dir1/build.xml

sub-release:
     [echo] dir1
     [echo] /work/Scratch/dir2/build.xml

sub-release:
     [echo] dir2
      [for] /work/Scratch/dir2/build.xml: The following error occurred while executing this line:
      [for] /work/Scratch/build.xml:17: The following error occurred while executing this line:
      [for] /work/Scratch/dir2/build.xml:6: dir2 failed
     [echo] /work/Scratch/dir3/build.xml

sub-release:
     [echo] dir3

BUILD FAILED
/work/Scratch/build.xml:11: Keepgoing execution: 1 of 3 iterations failed.

Total time: 0 seconds

答案 1 :(得分:1)

Antelope Ant扩展具有try-catch命令,可用于满足您的需求:

<taskdef name="try" classname="ise.antelope.tasks.TryTask"/>
....
<try break="false" printmessage="true" >
   <antcall target="xmlValidate" />
   <antcall target="runJunit" />
   <antcall target="..." />
   <catch>
      <property name="haderrors" value="true"/>
   </catch>
</try>
<fail message="FAILED" if="haderrors" />

break=false让我们在失败后继续下一个命令。但是失败的目标设置了最后检查的haderrors属性。我在构建作业中使用了很多(并且它工作正常)但我不确定它是否适用于<fileset>内的<subant>。也许您必须明确列出所有<subant>次来电。