问题描述
|
我正在尝试检查是否设置了ant build脚本的参数。我已经尝试了很多方法来做到这一点,但没有成功。我用“ 0”定义参数。
这是我尝试过的代码示例;
<ac:if>
<equals arg1=\"@{maindir}\" arg2=\"\" />
<ac:then>
<echo message=\"maindir argument is empty. Current properties will be used.\" />
<property file=\"build.properties\" />
</ac:then>
<ac:else>
<echo message=\"maindir = ${maindir}\" />
<ac:if>
<ac:available file=\"${maindir}/build.properties\" type=\"file\" />
<ac:then>
<property file=\"${maindir}/build.properties\" />
</ac:then>
<ac:else>
<fail message=\"${maindir} is not a valid path.\" />
</ac:else>
</ac:if>
</ac:else>
</ac:if>
共有三种情况;
参数可能未定义。蚂蚁应该先进入
论点定义得很好。
用错误的路径定义的参数
对于第二种情况,脚本正在运行。
对于第3种情况,脚本正在运行。
但是对于第一种情况,我的意思是当我不定义maindir参数时,ant的行为类似于第三种情况。那是我的问题。
为什么蚂蚁会那样做?
解决方法
也许您可以尝试为参数设置默认值?
<condition property=\"maindir\" value=\"[default]\">
<not>
<isset property=\"maindir\"/>
</not>
</condition>
<echo message=\"${maindir}\" />
我尝试了此方法,并且在没有传递任何参数的情况下,works3ѭ的值是[default]
。
, 看来有两个问题:
在第一个if等于条件下,您有@{maindir}
。除非它是宏的参数,否则应为${maindir}
,与示例中的其余部分相同
如果尚未设置属性,则不会将其评估为任何内容。因此,如果未定义maindir,则${maindir}
将计算为${maindir}
,而不是空字符串。
解决此问题的最简单方法是将@符号更改为$符号,并在开头添加一条语句以将该属性默认为值:
<property name=\"maindir\" value=\".\" />
这会将属性默认设置为当前目录,因此可以完全消除外部if,因为不再需要它。 ant中的属性是只读的,因此,如果用户明确指定一个值(例如,从命令行),则将使用该值,并且上面的行将不起作用-仅当用户使用时,它才起作用没有为maindir指定值。
实际上,我认为您可以通过执行以下操作完全摆脱蚂蚁破坏:
<property name=\"maindir\" value=\".\" />
<fail message=\"${maindir}/build.properties is not a valid path.\">
<condition>
<not>
<available file=\"${maindir}/build.properties\" />
</not>
</condition>
</fail>
<property file=\"${maindir}/build.properties\" />
这应该与您的示例所要达到的效果完全相同。