ant filtering - fail if property not set

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

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

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.

Tags:

Java

Ant