如何处理apache ant中的错误? [英] How to handle errors in apache ant?

查看:31
本文介绍了如何处理apache ant中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在依次调用 ant 目标.如果一个目标失败,我想继续序列中的下一个目标.如何做到这一点?

I'm calling ant targets one after the other in sequence. If one target fails I want to continue with the next target in the sequence. How to do this?

目前我使用 antcall 任务调用目标.

Currently i call targets using antcall task.

仅供参考 - 每个目标都使用 taskdef 调用 java 类.这个java类可以抛出异常.

FYI - Each target invokes java class using taskdef. This java class can throw exception.

请提出建议.

注意:- 我不能使用任何其他第三方 jar,如 ant-contrib 等.

Note:- I cannot use any other third party jars like ant-contrib etc..

推荐答案

目前我使用 antcall 任务调用目标.

Currently i call targets using antcall task.

不要使用antcall.构建文件不是程序.它们是依赖矩阵.您让 Ant 决定调用哪些目标以及调用顺序.例如,我有一个名为 package 的目标,它取决于我在目标 compile 中编译的代码.compile 目标取决于我在目标 generate-source 中生成的源代码.我的 build.xml 看起来像这样:

Don't use antcall. Build files aren't programs. They're dependency matrixes. You let Ant decide what targets to call and in what order. For example, I have a target called package which depends upon my code being compiled in target compile. That compile target depends upon my source code being generated in a target generate-source. My build.xml will look something like this:

<project default="package" name="my.proj">
    ...
    <target name="generate-source">
        ...
    </target>

    <target name="compile" depends="generate-source">
        ...
    </target>

    <target name="package" depends="compile">
        ...
    </target>
</project>

当我说:

$ ant package

Ant 查看我的依赖矩阵.它意识到需要先运行compile目标,然后才能运行package,然后意识到需要在之前运行generate-source>编译.因此,Ant 将按以下顺序自动运行目标:

Ant looks at my dependency matrix. It realizes that it needs to run the compile target before it can run package, and then realizes it needs to run generate-source before compile. Thus, Ant will automatically run the targets in this order:

  • 生成源
  • 编译

注意我不必说package 依赖于目标generate-source.相反,我所要做的就是指定主要依赖项,Ant 会自行解决其他后续依赖项.

Note I didn't have to say that package was dependent upon the target generate-source. Instead, all I had to do was specify the primary dependencies, and Ant will work out the other subsequent dependencies by itself.

使用antcall 强制排序有什么问题?因为我可能会弄错.如果我的 build.xml 发生变化怎么办?我现在必须检查并验证我的整个文件,以查看我的 antcall 任务是否仍然正确.此外,在更复杂的构建文件中,您最终可能会一遍又一遍地调用相同的任务.

What's wrong about using antcall to force ordering? Because I may get it wrong. What if my build.xml changes? I now have to go through and verify my entire file looking to see if my antcall tasks are still correct. Also, in more complex build files, you can end up calling the same task over and over.

在我来之前,我们让一位开发人员编写了我们的 build.xml 文件.在一次构建中,他将 build.xml 分成多个构建文件(另一个禁忌),然后手动使用 antantcall 任务来做构建.结果是一些目标在构建过程中被调用了多达 15 次,并且一次构建需要 20 多分钟才能完成,因为特定目标被一次又一次地调用(每个目标都会删除前一次调用生成的工件).构建文件的重写将构建过程缩短到仅 4 分钟.

We had a developer write our build.xml files before I came. In one build, he divided the build.xml into multiple build files (another no-no) and then manually used ant and antcall tasks to do the build. The result were some targets being called up to 15 times in the build process and a build taking over 20 minutes to do as a particular target was called again and again (and each target would remove the artifacts generated by the previous call). A rewrite of the build file shortened the build process to a mere 4 minutes.

作为奖励,每个目标都可以被调用和工作.例如,开发人员可以调用 compile 目标而无需构建整个包——在调试代码时能够做到这一点非常好.此外,不要在正常构建步骤中执行任何清理过程.相反,有一个 clean 目标来删除所有生成的工件.作为正常构建过程的一部分删除构建的工件会减慢调试速度.如果开发人员需要一个干净的石板,他们可以运行 clean 目标.

As a bonus, each target could be called and worked. For example, a developer could call the compile target without having to build the entire package -- something thats nice to be able to do when you are debugging your code. Also, don't do any clean process as part of the normal build steps. Instead, have a clean target that deletes all generated artifacts. Removing built artifacts as part of the normal build process can slow down debugging. If a developer requires a clean slate, they can run the clean target.

作为对您原始查询的回答,几乎所有任务都可以设置为忽略错误并继续.我建议使用可以设置为覆盖默认设置的属性:

As an answer to your original query, Almost all tasks can be set to ignore errors and continue. I recommend to use properties that can be set to override default settings:

<property name="halt.on.error"  value="true"/>

<target name="compile" ...>
    <javac ...
       failonerror="${halt.on.error}"
       .../>
    ....
</target>

如果我跑:

$ ant

构建将在编译错误时停止.但是,如果我跑了:

The build will halt on a compile error. However, if I ran:

$ ant -Dhalt.on.error="false"

这将覆盖构建文件本身中 halt.on.error 属性的设置,并允许程序在出现编译器错误时继续运行.

This will override the setting of the halt.on.error property in the build file itself, and allow the program to continue even on compiler errors.

这篇关于如何处理apache ant中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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