user3492304
user3492304

Reputation: 163

How to handle errors in 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?

Currently i call targets using antcall task.

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

Please suggest.

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

Upvotes: 0

Views: 2899

Answers (2)

David W.
David W.

Reputation: 107090

Currently i call targets using antcall task.

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>

When I say:

$ ant package

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:

  • generate-source
  • compile
  • package

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.

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.

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.

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>

If I run:

$ ant

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

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

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.

Upvotes: 2

Gautam Jose
Gautam Jose

Reputation: 696

Instead of trying to skip the target you can continue the task ignoring the failure.

For e.g for taskdef you can try including parameter onerror="ignore" for javac task you can include failonerror="false"

Upvotes: 0

Related Questions