Search code examples
ant

Apache Ant - How to handle the target error?


We are using below ant target to deploy the application and perform post-deployment tasks. There are multiple applications which are deployed using this target. For few targets, property "failOnError" is set to "false" while for other targets its set to "true".

Once deployment is completed, we need to publish application deployment report which mentions if deplolyment for every application completed successfully or failed.

  1. We are fine if target(s) fails where "failOnError" is set to "false".
  2. However, we would like to execute different target for e.g. "deployment.report" when any target fails where "failOnError" is set to "true".

Is it possible to identify error/failure in a target (mentioned above in point 2) on "deploy" target level and execute "deployment.report" target?

<target name="deploy" 
    depends="upload.jar, create.app, disable.features, enable.features, provision.features, edit.properties, wire.application, distribute.app, deploy.app, start.app">
</target>

<target name="deployment.report">
    <!-- execute java code here to update deployment report csv file -->
</target>

Solution

  • Using long sequential lists of antcall tasks leads to confusing logging and inefficiency. The suggested method is to rely on target dependencies to work out the execution order for you, rather than telling Ant what to do step by step.

    Here's the type of approach I would recommend (note how each target defines what it actually depends on, rather than just stemming from the previous target):

    <project name="test" default="run">
        <target name="run">
            <trycatch>
                <try>
                    <antcall target="deploy" />
                </try>
                <catch>
                    <echo message="Failure detected!" />
                </catch>
                <finally>
                    <antcall target="update.deployment.report" />
                </finally>
            </trycatch>
        </target>
    
        <!-- Dependencies listed alphabetically, order is not enforced -->
    
        <target name="deploy"
            depends="
                create.app,
                deploy.app,
                disable.features,
                distribute.app,
                edit.properties,
                enable.features,
                provision.features,
                start.app,
                upload.daa,
                wire.application
            "
        />
    
        <!-- Target execution order determined by individual target dependencies -->
    
        <target name="upload.daa">
        </target>
    
        <target name="create.app" depends="upload.daa">
        </target>
    
        <target name="disable.features" depends="create.app">
        </target>
    
        <target name="enable.features" depends="create.app">
        </target>
    
        <target name="provision.features" depends="create.app">
        </target>
    
        <target name="edit.properties" depends="create.app">
        </target>
    
        <target name="wire.application" depends="disable.features,enable.features,provision.features,edit.properties">
        </target>
    
        <target name="distribute.app" depends="wire.application">
        </target>
    
        <target name="deploy.app" depends="wire.application">
        </target>
    
        <target name="start.app" depends="deploy.app">
        </target>
    
        <target name="update.deployment.report">
        </target>
    </project>