代码之家  ›  专栏  ›  技术社区  ›  James Cooper

Ant筛选-如果未设置属性,则失败

  •  29
  • James Cooper  · 技术社区  · 16 年前

    我有一只蚂蚁 build.xml 使用 <copy> 复制各种XML文件的任务。它使用筛选合并来自 build.properties 文件。每个环境(dev、stage、prod)都有不同的 内部版本属性 存储该环境的配置。

    有时,我们会向SpringXML或其他需要更新 内部版本属性 文件。

    如果有属性丢失,我希望Ant快速失败。 内部版本属性 . 也就是说,如果有生的 @...@ 标记将其生成到生成的文件中,我希望生成终止,以便用户知道他们需要向本地build.properties添加一个或多个属性。

    内置任务是否可以这样做?我在医生那里找不到任何东西。我正准备写一个定制的蚂蚁任务,但也许我可以省力。

    谢谢

    5 回复  |  直到 16 年前
        1
  •  19
  •   Jason Day    16 年前

    您可以在Ant1.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"/>
    
        2
  •  97
  •   dovetalk Ahmed H. Saab    6 年前

    如果要查找特定的属性,则只需使用带有“除非”属性的“失败”任务,例如:

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

    参照 the documentation for Ant's fail task 更多细节。

        3
  •  4
  •   matt b    16 年前

    我建议你尝试使用 <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"/>
    
        4
  •  0
  •   khansen    16 年前

    如果在您的Ant版本中不推荐使用exec命令,则可以使用重定向器,例如:

    <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"/>
    

    为条件

        5
  •  0
  •   Vadzim    11 年前

    自从蚂蚁1.6 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>