Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Apache Ant resolve duplicate targets

Tags:

apache

ant

Say I have two targets in ant with the same name in two different build files but, one is imported into the other.

build.xml

<project>
    <target name="once">
        <echo>once</echo>
        </target>

        <target name="twice">
            <echo>twice-a in build.xml</echo>
            </target>
        <!-- duplicate target twice imported again from build2.xml -->
        <import file="build2.xml"/>

</project>

build2.xml

<project>

    <target name="twice">
        <echo>twice-a in build2.xml</echo>
    </target>

</project>

How does ant resolve duplicate targets ?

If it had been in a single file it would have a duplicate target thrown an error however since its imported its not throwing an error.

When I run ant twice I get

$ ant twice
Buildfile: /Users/nav/Codes/build.xml

twice:
     [echo] twice-a in build.xml

BUILD SUCCESSFUL
Total time: 0 seconds

If ant does take the first declaration as the target then why doesn't moving the import statement up in build.xml

<?xml version="1.0"?>

<project>
    <!-- import moved to the top -->
    <import file="build2.xml"/>

    <target name="once">
        <echo>once</echo>
        </target>

        <target name="twice">
            <echo>twice-a in build.xml</echo>
            </target>
</project>

still outputs the same as

$ ant twice
Buildfile: /Users/nav/Codes/build.xml

twice:
     [echo] twice-a in build.xml

BUILD SUCCESSFUL
Total time: 0 seconds
like image 921
Nav Avatar asked Nov 28 '25 12:11

Nav


1 Answers

When you assign project names, then you can access the both targets

<project name="build1">
<target name="once">
    <echo>once</echo>
    </target>

    <target name="twice">
        <echo>twice-a in build.xml</echo>
        </target>
    <!-- duplicate target twice imported again from build2.xml -->
    <import file="build2.xml"/>

</project>

Build2

<project name="build2">

 <target name="twice">
     <echo>twice-a in build2.xml</echo>
  </target>

</project>

call ant -p

    Buildfile: build.xml

Main targets:

Other targets:

 build2.twice
 once
 twice

If not project name is assign the imported target are hidden if they have the same name.

like image 105
Hank Lapidez Avatar answered Nov 30 '25 02:11

Hank Lapidez