即使依赖目标失败,Ant也会运行目标

时间:2013-11-13 05:38:26

标签: java email ant build

我的构建文件中有以下代码

<target name="foo">
<some stuff>
</target>
<target name="bar" depends="foo">
<some other stuff>
</target>

当我ant bar时,即使bar失败,我也希望foo目标能够投放。我该怎么做?

我不想使用ant-contrib的try catch。

2 个答案:

答案 0 :(得分:3)

首先,Ant不是编程/脚本语言。它是一个构建工具,因此它有局限性。

根据您的要求,我从手册中找到了这个:

  

-keep-going,在失败的目标上执行不依赖的所有目标

想想“依赖”是为什么设计的。当依赖项失败时,不应执行目标。

另一种方法是使用subant(当然,没有depends)。

<target name="foo">
    <fail message="fail" />
</target>

<target name="bar">
    <subant failonerror="false" target="foo">
        <fileset dir="." includes="build.xml"/>
    </subant>
    <echo message="still runs"/>
</target>

哪个输出:

bar:

foo:
   [subant] Failure for target 'foo' of: c:\Tools\files\build.xml
   [subant] The following error occurred while executing this line:
   [subant] c:\Tools\files\build.xml:11: fail
   [echo] still runs

但是......看起来很难看。

另一种方法是实现自定义Ant入口点,并在其中执行任何操作。使用ant -main <class> bar启动您的Ant。

如果您想坚持使用depends,并且foo中的任何任务不支持failonerror,并且您不想try-catch,那么我也不知道怎么做。

答案 1 :(得分:2)

考虑Antcontrib: TryCatch

<target name="foo">
  <some stuff>
</target>

<target name="bar">
  <trycatch>
    <try>
      <antcall target="foo"/>
    </try>
    <catch>
      <fail/>
    </catch>
    <finally>
      <some other stuff>
    </finally>
  </trycatch>
</target>
相关问题