繁体   English   中英

蚂蚁过滤 - 如果未设置属性则失败

[英]ant filtering - fail if property not set

我有一个 ant build.xml ,它使用<copy>任务来复制各种 xml 文件。 它使用过滤从build.properties文件合并属性。 每个环境(dev、stage、prod)都有一个不同的build.properties来存储该环境的配置。

有时我们会向 Spring XML 或其他需要更新build.properties文件的配置文件添加新属性。

如果build.properties缺少属性,我希望 ant 快速失败。 也就是说,如果任何原始的@...@令牌进入生成的文件,我希望构建终止,以便用户知道他们需要向其本地 build.properties 添加一个或多个属性。

这可以通过内置任务实现吗? 我在文档中找不到任何内容。 我即将编写一个自定义的 ant 任务,但也许我可以省点力气。

谢谢

如果你正在寻找一个特定的属性,你可以使用带有除非属性的失败任务,例如:

<fail unless="my.property">Computer says no. You forgot to set 'my.property'!</fail>

有关更多详细信息,请参阅Ant 的失败任务的文档

您可以在 ant 1.7 中使用LoadFile任务和match条件的组合来完成。

<loadfile property="all-build-properties" srcFile="build.properties"/>
<condition property="missing-properties">
    <matches pattern="@[^@]*@" string="${all-build-properties}"/>
</condition>
<fail message="Some properties not set!" if="missing-properties"/>

我打算建议您尝试使用<property file="${filter.file}" prefix="filter">将属性实际加载到 Ant 中,如果其中任何一个未设置,则fail ,但我认为我错误地解释了您的问题(如果未在属性文件中设置指定的属性,您希望失败)。

我认为您最好的选择可能是使用<exec>来(取决于您的开发平台)对“@”字符执行 grep,然后将属性设置为找到的出现次数。 不确定确切的语法,但是...

<exec command="grep \"@\" ${build.dir} | wc -l" outputproperty="token.count"/>
<condition property="token.found">
    <not>
        <equals arg1="${token.count}" arg2="0"/>
    </not>
</condition>
<fail if="token.found" message="Found token @ in files"/>

由于 Ant 1.6.2 condition也可以嵌套在fail

以下宏可以轻松地有条件地检查多个属性。

<macrodef name="required-property">
    <attribute name="name"/>
    <attribute name="prop" default="@{name}"/>
    <attribute name="if" default="___"/>
    <attribute name="unless" default="___"/>
    <sequential>
        <fail message="You must set property '@{name}'">
            <condition>
                <and>
                    <not><isset property="@{prop}"/></not>
                    <or>
                        <equals arg1="@{if}" arg2="___"/>
                        <isset property="@{if}"/>
                    </or>
                    <or>
                        <equals arg1="@{unless}" arg2="___"/>
                        <not><isset property="@{unless}"/></not>
                    </or>
                </and>
            </condition>
        </fail>
    </sequential>
</macrodef>

<target name="required-property.test">
    <property name="prop" value=""/>
    <property name="cond" value="set"/>
    <required-property name="prop"/>
    <required-property name="prop" if="cond"/>
    <required-property name="prop" unless="cond"/>
    <required-property name="prop" if="cond2"/>
    <required-property name="prop" unless="cond2"/>
    <required-property name="prop" if="cond" unless="cond"/>
    <required-property name="prop" if="cond" unless="cond2"/>
    <required-property name="prop" if="cond2" unless="cond"/>
    <required-property name="prop" if="cond2" unless="cond2"/>
    <required-property name="prop2" unless="cond"/>
    <required-property name="prop2" if="cond2"/>
    <required-property name="prop2" if="cond2" unless="cond"/>
    <required-property name="prop2" if="cond" unless="cond"/>
    <required-property name="prop2" if="cond2" unless="cond2"/>
    <required-property name="success"/>
</target>

如果 exec 命令在您的 ant 版本中被弃用,您可以使用重定向器,例如:

<exec executable="grep">
  <arg line="@ ${build.dir}"/>
  <redirector outputproperty="grep.out"/>
</exec>
<exec executable="wc" inputstring="${grep.out}">
  <arg line="-l"/>
  <redirector outputproperty="token.found"/>
</exec>

创建 token.found 属性

<condition property="token.found">
    <not>
        <equals arg1="${token.count}" arg2="0"/>
    </not>
</condition>
<fail if="token.found" message="Found token @ in files"/>

对于有条件的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM