【问题标题】:ant filtering - fail if property not set蚂蚁过滤 - 如果未设置属性则失败
【发布时间】:2010-09-18 14:34:51
【问题描述】:

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

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

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

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

谢谢

【问题讨论】:

    标签: java ant


    【解决方案1】:

    我打算建议您尝试使用 <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"/>
    

    【讨论】:

      【解决方案2】:

      如果您的 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"/>
      

      条件式

      【讨论】:

        【解决方案3】:

        您可以在 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"/>
        

        【讨论】:

        • 不错。我实际上想做相反的事情(确保某些关键文件中有@'s - 有时有人不小心签入了没有替换标记的版本)并且能够使用这种方法。
        • 非常好。对不起,我之前没有接受这个。看起来像我所追求的。
        • 你可以在失败中嵌套条件:
        【解决方案4】:

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

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

        详情请参阅the documentation for Ant's fail task

        【讨论】:

          【解决方案5】:

          由于 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>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-07-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多