个性化阅读
专注于IT技术分析

apache ant的if和unless属性

如果和除非两者都是<target>元素(任务)的属性,则为Ant。这些属性用于控制任务是否运行。

除目标外,它还可以与<target>和<junit>元素一起使用。

在早期版本和Ant 1.7.1中,这些属性仅是属性名称。如果定义了属性,则即使该值为false也会运行。

例如,即使传递false之后也无法停止执行。

// build.xml

<project name="java-ant project" default="run">
<target name="compile">
    <available property="file.exists" file="some-file"/>
    <echo>File is compiled</echo>
</target>
<target name="run" depends="compile" if="file.exists">
    <echo>File is executed</echo>
</target>
</project>

输出:

无参数:通过不带命令行参数的方式运行。只需在终端输入ant,但首先定位到项目位置,它将显示空输出。

带参数:现在传递参数butfalse。

ant-0Dfile.exists = false

现在传递论点但正确。 Ant -Dfile.exists = true

从Ant 1.8.0开始,我们可以使用允许仅在value为true时执行的属性扩展。在新版本中,它为我们提供了更大的灵活性,现在我们可以从命令行覆盖条件值。请参阅下面的示例。

// build.xml

<project name="java-ant project" default="run">
<target name="compile" unless="file.exists">
    <available property="file.exists" file="some-file"/>
</target>
<target name="run" depends="compile" if="${file.exists}">
    <echo>File is executed</echo>
</target>
</project>

输出:

无参数:在没有命令行参数的情况下运行它。只需在终端输入ant,但首先要找到项目的位置,它将显示空的输出。

带自变量:现在传递自变量,但为false。ant-Dfile.exists = false

没有输出,因为这次如果不执行。

带自变量:现在传递自变量,但为true。现在显示输出,因为评估了if。 Ant -Dfile.exists = true

赞(0)
未经允许不得转载:srcmini » apache ant的if和unless属性

评论 抢沙发

评论前必须登录!