Apache Ant 如何解决重复目标

How does Apache Ant resolve duplicate targets

假设我在 ant 中有两个目标,在两个不同的构建文件中具有相同的名称,但是一个被导入到另一个。

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>

ant如何解决重复目标?

如果它在单个文件中,则重复的目标会抛出错误,但是由于它是导入的,因此不会抛出错误。

当我 运行 ant twice 我得到

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

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

BUILD SUCCESSFUL
Total time: 0 seconds

如果 ant 确实将第一个声明作为目标,那么为什么不在 build.xml

中向上移动 import 语句
<?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>

仍然输出与

相同的结果
$ ant twice
Buildfile: /Users/nav/Codes/build.xml

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

BUILD SUCCESSFUL
Total time: 0 seconds

分配项目名称后,您就可以访问这两个目标

<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>

调用 ant -p

    Buildfile: build.xml

Main targets:

Other targets:

 build2.twice
 once
 twice

如果没有指定项目名称,则导入的目标如果名称相同,则会被隐藏。