2008-10-22 38 views
29

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

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

我想蚂蚁快速失败,如果有从build.properties缺少属性。也就是说,如果有任何原始@[email protected]令牌将其生成到生成的文件中,我希望构建死掉,以便用户知道他们需要向其本地build.properties添加一个或多个属性。

这是可能与内置任务?我无法在文档中找到任何内容。我即将写一个自定义的蚂蚁任务,但也许我可以放下自己的努力。

感谢

回答

19

你可以做到这一点的蚂蚁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"/> 
+0

不错。我实际上想做相反的事情(确保某些关键文件中包含@,有时候会有人不小心检查没有替代标记的版本),并且能够使用这种方法。 – 2009-06-22 16:37:27

+0

非常好。对不起,我以前没有接受过。看起来像我以后的样子。 – 2011-01-15 00:05:50

4

我会建议你尝试使用<property file="${filter.file}" prefix="filter">的属性,以实际加载到蚂蚁,然后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"/> 
0

如果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财产

为conditonal

93

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

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

0

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