ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Abley <james.ab...@volantis.com>
Subject Utility macrodef to verify all filterset tokens are replaced
Date Fri, 05 Aug 2005 08:02:52 GMT
Inspired by a comment from Jan earlier this week on the list, here is
one of my commonly used macrodefs. 

We find it very useful to use prior to building a war or similar
artifact for deployment. With the number of environments that we have,
it is a great time saver to detect that you don't have a configuration
setting of @image.server@ still in a config file before you deploy to a
production environment.

    <!-- 
        Macrodef for searching for unreplaced filterset tokens prior to
building a war file
    -->
    <macrodef name="check-filterset-tokens" 
        description="search a fileset for a given token, outputting any
matches to the file specified in the attribute outFile">
        <attribute name="outFile" 
        description="name of the file to output any matching lines to"
/>
        <attribute name="pattern" default='[">]@[^@]+@'
        description="regex to check the filterset for. Default matches a
double quote or greater than char followed by a standard filterset token
- typically found in an xml file to be templated by the build" />
        <element name="files" implicit="yes" 
        description="fileset containing the files to search. This can be
any ANT path-like structure" />
        <sequential>
            <delete file="@{outFile}" />
            
            <concat destfile="@{outFile}">
                <files />
                
                <filterchain>
                    <linecontainsregexp>
                        <regexp pattern="@{pattern}" />   
                    </linecontainsregexp>
                </filterchain>        
            </concat>
            
            <loadfile property="antutils.check.filtersets.file"
srcfile="@{outFile}" />
        
            <fail message="
            Some filterset tokens have not been replaced.
${line.separator}
${line.separator}
${antutils.check.filtersets.file}
${line.separator}
${line.separator}Please see the file @{outFile}">
                <condition>
                    <!-- check that the property was set by the loadfile
task -->
                    <isset property="antutils.check.filtersets.file" />
                </condition>
            </fail>

            <delete file="@{outFile}" />
            
        </sequential>
    </macrodef>

To fully leverage it, it helps to know regular expressions fairly well,
but that's no bad skill to master.

Sample usage:

    <!--
        Check that all filterset tokens have been included and all
properties have been defined
    -->
    <target name="-check-filterset-tokens">
        <path id="templated-files">
            <fileset dir="${temp.dir}">
                <include name="**/*" />
                <exclude name="**/*.jar" />
            </fileset>
        </path>

        <property name="filterset.results.file"
location="filterset/results.txt" />

        <!--
            default check looking for unreplaced tokens (element
character 
            data or attribute) in xml files [">]@xxx@
        -->
        <check-filterset-tokens outfile="${filterset.results.file}">
            <path refid="templated-files" />
        </check-filterset-tokens>

        <!-- 
            a more complex check here to see that all properties used in
            filtersets have been defined. Since there are some
expressions 
            in the xml files of the form ${xxx}, we assume that all
tokens 
            will be of the form ${xxx.yyy}
        -->
        <check-filterset-tokens outfile="${filterset.results.file}"
pattern="\$\{[^\}]+\.[^\}]+\}">
            <path refid="templated-files" />
        </check-filterset-tokens>

        <!-- check in properties files for simple @xxx@ -->
        <check-filterset-tokens outfile="${filterset.results.file}"
pattern="@[^@\']+@">
            <path refid="templated-files" />
        </check-filterset-tokens>
    </target>


Hope others find it useful or can improve it, or point out how it could
be done in a simpler way.

James

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message