我有一个Ant XML文件,我用它来构建.
我有3个属性.如果这些属性不包含任何值,我想打破构建.如果值为空,我还想打破构建.
我怎么能在Ant中这样做?
我使用Ant而不是Ant-contrib.
Dav*_* W. 60
您可以使用<fail>任务使用条件:
<fail message="Property "foo" needs to be set to a value">
<condition>
<or>
<equals arg1="${foo}" arg2=""/>
<not>
<isset property="foo"/>
</not>
</or>
</condition>
Run Code Online (Sandbox Code Playgroud)
这相当于说if (not set ${foo} or ${foo} = "")是伪代码.您必须从内到外阅读XML条件.
如果您只关心变量是否已设置,您可以<unless>在<fail>任务上使用该子句,而不是它是否具有实际值.
<fail message="Property "foo" needs to be set"
unless="foo"/>
Run Code Online (Sandbox Code Playgroud)
但是,如果设置了属性,则不会失败,但没有值.
有一个技巧可以使这更简单
<!-- Won't change the value of `${foo}` if it's already defined -->
<property name="foo" value=""/>
<fail message="Property "foo" has no value">
<condition>
<equals arg1="${foo}" arg2=""/>
</condition>
</fail>
Run Code Online (Sandbox Code Playgroud)
请记住,我无法重置财产!如果${foo}已有值,则<property>上述任务将不执行任何操作.这样,我可以消除这种<isset>情况.它可能很好,因为你有三个属性:
<property name="foo" value=""/>
<property name="bar" value=""/>
<property name="fubar" value=""/>
<fail message="You broke the build, you dufus">
<condition>
<or>
<equals arg1="${foo}" arg2=""/>
<equals arg1="${bar}" arg2=""/>
<equals arg1="${fubar}" arg2=""/>
</or>
</condition>
</fail>
Run Code Online (Sandbox Code Playgroud)
Ben*_*age 12
基于其他答案,这是我的首选形式,作为宏:
<!-- Macro to require a property is not blank -->
<macrodef name="prop-require">
<attribute name="prop"/>
<sequential>
<fail message="Property "@{prop}" must be set">
<condition>
<not>
<isset property="@{prop}"/>
</not>
</condition>
</fail>
<fail message="Property "@{prop}" must not be empty">
<condition>
<equals arg1="${@{prop}}" arg2=""/>
</condition>
</fail>
</sequential>
</macrodef>
Run Code Online (Sandbox Code Playgroud)
用作:
<target name="deploy.war" description="Do the war deployment ;)">
<prop-require prop="target.vm" />
<prop-require prop="target.vip" />
<!-- ... -->
Run Code Online (Sandbox Code Playgroud)
为简洁起见,您可以使用一个将两个失败元素合并为一个<or>,但我更喜欢我的错误消息来对待我,就像我自己无法思考;)
| 归档时间: |
|
| 查看次数: |
62960 次 |
| 最近记录: |