ant filtering - fail if property not set

前端 未结 5 1697
后悔当初
后悔当初 2020-12-15 16:47

I\'ve got a ant build.xml that uses the task to copy a variety of xml files. It uses filtering to merge in properties from a bu

相关标签:
5条回答
  • 2020-12-15 17:13

    I was going to suggest that you attempt to use <property file="${filter.file}" prefix="filter"> to actually load the properties into Ant, and then fail if any of them are not set, but I think I was interpreting your problem wrong (that you wanted to fail if a specified property was not set in the properties file).

    I think your best bet might be to use <exec> to (depending on your dev platform) do a grep for the "@" character, and then set a property to the number of occurences found. Not sure of exact syntax but...

    <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 讨论(0)
  • 2020-12-15 17:21

    If you are looking for a specific property, you can just use the fail task with the unless attribute, e.g.:

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

    Refer to the documentation for Ant's fail task for more detail.

    0 讨论(0)
  • 2020-12-15 17:22

    if exec command is deprecated in your version of ant you can use redirectors, something like:

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

    to create the token.found property

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

    for the conditonal

    0 讨论(0)
  • 2020-12-15 17:27

    You can do it in ant 1.7, using a combination of the LoadFile task and the match condition.

    <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 讨论(0)
  • 2020-12-15 17:27

    Since Ant 1.6.2 condition can also be nested inside fail.

    The following macro makes it easy to conditionally check multiple properties.

    <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>
    
    0 讨论(0)
提交回复
热议问题