我的构建文件中有以下代码
<target name="foo">
<some stuff>
</target>
<target name="bar" depends="foo">
<some other stuff>
</target>
当我ant bar
时,即使bar
失败,我也希望foo
目标能够投放。我该怎么做?
我不想使用ant-contrib的try catch。
答案 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)
<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>