如何防止依赖项在 Ant 中执行?
How do I prevent a dependency from executing in 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 的命令?
您必须重构 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
。
您可以使用 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
备注:
- Ant 控制台输出将显示目标是 "hit",即使输入被条件阻止。
if
和unless
条件不做布尔检查。他们只是检查 属性 是否被定义。
调试 build.xml 文件或 Ant 任务时,我经常想执行一个任务而不执行其依赖项。有没有办法从命令行执行此操作?
例如,对于这个 build.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<target name="A" />
<target name="B" depends="A" />
</project>
是否有执行任务 B 但不执行任务 A 的命令?
您必须重构 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
。
您可以使用 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
备注:
- Ant 控制台输出将显示目标是 "hit",即使输入被条件阻止。
if
和unless
条件不做布尔检查。他们只是检查 属性 是否被定义。