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

查看:116
本文介绍了如何处理apache 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 分为多个构建文件(另一个否定),然后手动使用 ant antcall 任务做构建.结果是某些目标在构建过程中最多被调用了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.

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

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天全站免登陆