How do I prevent a dependency from executing in Ant?

When debugging a build.xml file, or an Ant task, I often want to execute one task without executing its dependencies. Is there a way to do this from the command line?

For example, with this build.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <target name="A" />
    <target name="B" depends="A" />
</project>

is there a command that will execute task B but not task A?

Upvotes: 2

Views: 243

Answers (2)

ewan.chalmers
ewan.chalmers

Reputation: 16235

You can make execution of any target conditional on a property using if or unless.

<project default="B">

    <target name="A" unless="no.a">
        <echo>in A</echo>
    </target>

    <target name="B" depends="A" >
        <echo>in B</echo>
    </target>

</project>

Output with no condition specified:

$ ant
Buildfile: C:\Users\sudocode\tmp\ant\build.xml

A:
     [echo] in A

B:
     [echo] in B

BUILD SUCCESSFUL
Total time: 0 seconds

Output with condition specified on command line:

$ ant -Dno.a=any
Buildfile: C:\Users\sudocode\tmp\ant\build.xml

A:

B:
     [echo] in B

BUILD SUCCESSFUL
Total time: 0 seconds

Notes:

  • Ant console output will show that the target was "hit" even if entry was blocked by the condition.
  • The if and unless conditions do not do boolean check. They just check whether the property is defined or not.

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521339

You will have to restructure your Ant script to achieve this:

<target name="B">
    <if>
        <isset property="some.property"/>
        <then>
            <antcall target="A">
        </then>
    </if>
    <!-- execute task B here -->
</target>

If some.property is set, then it will first execute A followed by B. Otherwise, it will skip task A and execute B by itself.

Upvotes: 0

Related Questions