我们先看这个build.xml, 有三个target,其中dist默认会调用conditionOne的if部分。ifTarget默认因为有if="conditionOne",如果不设置conditionOne属性就不执行。 elseTarget部分因为unless="conditionOne", 默认执行echo,除非设置conditionOne属性
<project name="testAntCondition">
<target name="dist">
<echo>is dist target sart</echo>
<condition property="conditionOne">
<istrue value="true"/>
</condition>
<antcall target="ifTarget">
</antcall>
<antcall target="elseTarget">
</antcall>
<echo>is dist target end</echo>
</target>
<target name="ifTarget" if="conditionOne">
<echo>is if part</echo>
</target>
<target name="elseTarget" unless="conditionOne">
<echo>is else part</echo>
</target>
</project>
其中dist默认会调用conditionOne的if部分
F:\>ant dist
Buildfile: F:\build.xml
dist:
[echo] is dist target sart
ifTarget:
[echo] is if part
elseTarget:
[echo] is dist target end
BUILD SUCCESSFUL
Total time: 0 seconds
ifTarget默认因为有if="conditionOne",如果不设置conditionOne属性就不执行。
F:\>ant ifTarget
Buildfile: F:\build.xml
ifTarget:
BUILD SUCCESSFUL
Total time: 0 seconds
F:\>ant ifTarget -DconditionOne=yes
Buildfile: F:\build.xml
ifTarget:
[echo] is if part
BUILD SUCCESSFUL
Total time: 0 seconds
elseTarget部分因为unless="conditionOne", 默认执行echo,除非设置conditionOne属性
F:\>ant elseTarget
Buildfile: F:\build.xml
elseTarget:
[echo] is else part
BUILD SUCCESSFUL
Total time: 0 seconds
F:\>ant elseTarget -DconditionOne=no
Buildfile: F:\build.xml
elseTarget:
BUILD SUCCESSFUL
Total time: 0 seconds
dist不接受设置的conditionOne属性
F:\>ant dist -DconditionOne=yes
Buildfile: F:\build.xml
dist:
[echo] is dist target sart
ifTarget:
[echo] is if part
elseTarget:
[echo] is dist target end
BUILD SUCCESSFUL
Total time: 0 seconds
F:\>ant dist -DconditionOne=no
Buildfile: F:\build.xml
dist:
[echo] is dist target sart
ifTarget:
[echo] is if part
elseTarget:
[echo] is dist target end
BUILD SUCCESSFUL
Total time: 0 seconds
看执行效果
https://ant.apache.org/manual/
Ant官方手册
| if | the name of the property that must be set in order for this target to execute, orsomething evaluating to true. | No |
| unless | the name of the property that must not be set in order for this target to execute, orsomething evaluating to false. | No |
本文介绍如何使用Apache Ant通过条件属性来控制不同构建目标的执行流程。重点解释了如何利用if和unless属性来决定是否执行特定的目标,并展示了具体的构建文件配置及执行效果。

406

被折叠的 条评论
为什么被折叠?



