在调试build.xml文件或Ant任务时,我经常想要执行一个任务而不执行其依赖项。有没有办法从命令行执行此操作?
例如,使用此build.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<target name="A" />
<target name="B" depends="A" />
</project>
是否有一个执行任务B但不执行任务A的命令?
答案 0 :(得分:1)
您可以使用if
或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>
未指定条件的输出:
$ 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
注意:
if
和unless
条件不执行布尔检查。他们只是检查属性是否已定义。答案 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
。