我有一个用于构建的Ant XML文件。如何检查一个属性在Ant中是否有价值
我有3个属性。如果这些属性不包含任何值,我想打破构建。此外,如果值为空,我想打破构建。
我该如何在Ant中做到这一点?
我使用Ant而不是Ant-contrib。
我有一个用于构建的Ant XML文件。如何检查一个属性在Ant中是否有价值
我有3个属性。如果这些属性不包含任何值,我想打破构建。此外,如果值为空,我想打破构建。
我该如何在Ant中做到这一点?
我使用Ant而不是Ant-contrib。
您可以使用<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>
这相当于说if (not set ${foo} or ${foo} = "")
是伪代码。您必须从内部读取XML条件。
如果您只关心是否设置了该变量,而不是是否具有实际值,那么您可以在<fail>
任务上使用<unless>
子句。
<fail message="Property "foo" needs to be set"
unless="foo"/>
但是,如果属性已设置但没有值,则不会失败。
有一个窍门,可以使这个简单
<!-- 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>
请记住,我不能重置属性!如果${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>
坦克[蚂蚁滤波如果属性未设定失败。对我很好用 – KK99 2012-02-13 15:00:55
它很好地避免嵌套的ifs。也可以设置条件属性和值。 – user1587504 2013-03-06 08:23:13
很好的回答!我注意到最后一个失败声明缺少一个正确的结束标记 - >缺少斜杠,我无法更正它,因为编辑需要更改至少6个字符:< – AgentKnopf 2014-12-16 14:26:48
你可以尝试使用conditions ...或者创建拥有unless
目标有了Ant addon Flaka你可以使用类似的模式:
<property name="foo" value="bar"/>
...
<fl:unless test="has.property.foo">
...
</fl:unless>
...
<fl:when test="has.property.foo">
...
</fl:when>
混凝土检查emptyness:
<fl:when test=" empty '${foo}' ">
<fail message="Houston we have a problem!!"/>
</fl:when>
一个完整的例子,也使用'eq'的一些等号检查(与wo相反) ULD是 'NEQ'):
<project xmlns:fl="antlib:it.haefelinger.flaka">
<!-- some if/then/else construct -->
<fl:choose>
<!-- if -->
<when test=" '${buildtype}' eq 'prod' ">
<!-- then -->
<echo>..starting ProductionBuild</echo>
</when>
<when test=" '${buildtype}' eq 'test' ">
<!-- then -->
<echo>..starting TestBuild</echo>
</when>
<!-- else -->
<otherwise>
<fl:unless test="has.property.dummybuild">
<fail message="No valid buildtype !, found => '${buildtype}'"/>
</fl:unless>
<echo>.. is DummyBuild</echo>
</otherwise>
</fl:choose>
</project>
与蚂蚁-f的build.xml -Dbuildtype = PROD
或
蚂蚁-f的build.xml -Dbuildtype = PROD -Ddummybuild =任何
[echo] ..starting ProductionBuild
输出
输出与错字=>蚂蚁 - 的build.xml -Dbuildtype = testt
BUILD FAILED
/home/rosebud/workspace/AntTest/build.xml:21: No valid buildtype !, found => 'testt'
与蚂蚁-f的build.xml -DD输出ummybuild =无论
[echo] .. is DummyBuild
建立在其他的答案,这是我的首选形式,作为宏:
<!-- 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>
用作:
<target name="deploy.war" description="Do the war deployment ;)">
<prop-require prop="target.vm" />
<prop-require prop="target.vip" />
<!-- ... -->
为了简洁起见,您可以通过使用<or>
将两个失败元素合并为一个,但我更喜欢我的错误消息来对待我,就像我自己无法想象的那样;)
我在一个老版本的Ant中,所以isset不可用。相反,我在等号中使用了以下符号。的
<target name="xxx">
<echo message="${contextRoot}" />
<if>
<!-- check if the contextRoot property is defined. -->
<equals arg1="${contextRoot}" arg2="$${contextRoot}" />
<then>
<!-- it isn't set, set a default -->
<property name="contextRoot" value="/WebAppCtx" />
</then>
</if>
<echo message="${contextRoot}" />
</target>
可能重复的 - (http://stackoverflow.com/questions/226683/ant-filtering-fail-if-property-not-set) – oers 2012-02-09 08:52:03