Jayakumari Arumugham
Jayakumari Arumugham

Reputation: 353

How to use one target in multiple targets' 'depends' property in ANT build?

I am using ANT build for deployment process. For that, I followed the below points,

  1. Created five targets in ANT named 'initiate.deploy' (to initiate deployment), 'svn.checkout' (checkout source from SVN into workspace) , 'generate.ear' (EAR generation) and 'deploy.ear' (deploy EAR into Server), 'clean.workspace' (Cleaning workspace dirtory).
  2. The target 'initiate.deploy' is my default target.
  3. I need to clean the workspace directory before 'svn.checkout' target and after 'deploy.ear' target.
  4. I put 'clean.workspace' target in 'depends' property of 'svn.checkout' target and in 'initiate.deploy' target.

My Code:

    <target name="initiate.deploy" description="Initiate deployment" depends="svn.checkout, generate.ear, deploy.ear, clean.workspace">
        ..........................
    </target>

    <target name="svn.checkout" description="SVN checkout" depends ="clean.workspace">
        ..........................
    </target>

But the target 'clean.workspace' is executed only once before 'svn.checkout' but not after 'deploy.ear' target.

Build sequence is created as follows.

Build sequence for target(s) 'initiate.deploy' is [clean.workspace, svn.checkout, check.workSpace, update.property.file, generate.ear, deploy.ear, initiate.deploy]

How to use one target in multiple targets' 'depends' property in ANT build?

Upvotes: 1

Views: 555

Answers (1)

Lolo
Lolo

Reputation: 4347

As explained in the Ant documentation:

In a chain of dependencies stretching back from a given target such as D above, each target gets executed only once, even when more than one target depends on it.

My understanding is that this is designed to avoid cycles in the dependencies graph.

Due to this, you need to modify your targets, for example by removing clean.workspace from the dependencies of initiate.deploy and invoking it explicitely via the antcall task:

<target name="initiate.deploy" description="Initiate deployment" depends="svn.checkout, generate.ear, deploy.ear">
    ..........................
    <antcall target="clean.workspace" />
</target>

<target name="svn.checkout" description="SVN checkout" depends ="clean.workspace">
    ..........................
</target>

Update:

As mentioned in the comments, the antcall task will start the invoked target in a new Ant project, which can create an undesirable overhead. To avoid this behavior, it is possible to wrap the target as a macrodef and invoke it as a task in any other target. You may then change the invoked target such that it calls the new macrodef, in order to keep it available as a dependency of other tasks:

<target name="initiate.deploy" description="Initiate deployment" depends="svn.checkout, generate.ear, deploy.ear">
    ..........................
    <clean.workspace.macro />
</target>

<target name="svn.checkout" description="SVN checkout" depends ="clean.workspace">
    ..........................
</target>

<target name="clean.workspace">
    <clean.workspace.macro />
</target>

<macrodef name="clean.workspace.macro">
   <sequential>
        <!-- do the workspace cleanup -->
        ..........................
   </sequential>
</macrodef>

Upvotes: 1

Related Questions