假设我在两个不同的构建文件中使用相同名称的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
答案 0 :(得分:2)
分配项目名称时,您可以访问这两个目标
<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
如果没有分配项目名称,则导入的目标如果具有相同的名称则会被隐藏。