如何防止在Ant中执行依赖项?

时间:2015-04-24 07:11:25

标签: ant

在调试build.xml文件或Ant任务时,我经常想要执行一个任务而不执行其依赖项。有没有办法从命令行执行此操作?

例如,使用此build.xml文件:

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

是否有一个执行任务B但不执行任务A的命令?

2 个答案:

答案 0 :(得分:1)

您可以使用ifunless在属性上执行任何有条件的目标。

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

未指定条件的输出:

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

A:
     [echo] in A

B:
     [echo] in B

BUILD SUCCESSFUL
Total time: 0 seconds

在命令行中指定条件的输出:

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

A:

B:
     [echo] in B

BUILD SUCCESSFUL
Total time: 0 seconds

注意:

  • Ant控制台输出将显示目标是&#34; hit&#34;即使入境被条件阻止。
  • ifunless条件不执行布尔检查。他们只是检查属性是否已定义。

答案 1 :(得分:0)

您必须重新构建Ant脚本才能实现此目的:

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

如果设置了some.property,则会先执行A,然后执行B。否则,它将跳过任务A并自行执行B