Over a million developers have joined DZone.
Silver Partner

Search Properties Within Files Using Ant

· Java Zone

The Java Zone is brought to you in partnership with DCHQ.  Discover how to deploy & monitor multi-tier Java applications on any cloud in seconds.

The following example shows how to use Ant to search in a target directory, including and excluding files/dirs, each property key in label.properties. It produces a new file with the unused properties.

+: It can easily integrated in another ant-based project
-: It has poor performance if you have many properties to search, because it searches all files for each property.

<?xml version="1.0" encoding="windows-1252" ?>
<project name="SearchInFiles" default="search.in.files">
  <property file="build.properties"/>
  <taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
      <pathelement location="${ant.contrib.home}/ant-contrib-1.0b3.jar"/>
    </classpath>
  </taskdef>
  <target name="-init" depends="clean">
    <tstamp/>
    <echo>Checking all files under: ${target.path} </echo>
    <echo>File types to check: ${include.patterns}</echo>
    <echo>BEWARE: Will skip: ${exclude.patterns}</echo>
    <echo>Label's file that will check to see if used: ${path.to.labels.bundle} 

    </echo>
    <debug>target.prefix: ${target.prefix}</debug>
  </target>
  <target name="clean">
    <delete file="outlog.txt" failonerror="false"/>
    <delete file="${not_used_properties.log}" failonerror="false"/>
  </target>
  <target name="search.in.files" description="search in files" 
          depends="-init,load.properties"/>
  <!-- (\S*) -->
  <target name="load.properties">
    <property file="${path.to.labels.bundle}/labels.properties" 
            prefix="${target.prefix}"/>
    <propertyselector property="list" delimiter="," 
            match="${target.prefix}\..*" select="\0" 
            casesensitive="false"/>
    <foreach list="${list}" delimiter="," target="check.prop"  

                  param="prefix.prop.key"/>
  </target>
  <target name="check.prop" depends="-search.prop, -handle.not.used">
    <!-- depends="-search.prop, -print.occurences" -->
    <debug>${prefix.prop.key} is checked.</debug>
  </target>
  <target name="-search.prop">
    <propertyregex property="prop.key" input="${prefix.prop.key}" 
        regexp="${target.prefix}\.(\S*)" select="\1" casesensitive="false"/>
    <debug>Checking property: ${prop.key}</debug>
    <fileset id="selected.files.set" dir="${target.path}"  

           includes="${include.patterns}" 
        excludes="${exclude.patterns}">
      <contains text="${prop.key}" casesensitive="no"/>
    </fileset>
    <property name="selected.files.list" refid="selected.files.set"/>
    <debug>Files that property found: ${selected.files.list}</debug>
    <!-- <antcall target="print.occurances"/> -->
  </target>
  <target name="-handle.not.used">
    <if>
      <equals arg1="${selected.files.list}" arg2=""/>
      <then>
        <echo>Not used property: ${prop.key} </echo>
        <propertyfile file="${not_used_properties.log}">
          <entry key="${prop.key}" default=""/>
        </propertyfile>
      </then>
    </if>
  </target>
  <target name="-print.occurences"  

               description="just prints the occurences of the prop">
    <for list="${selected.files.list}" param="occurance.filepath" delimiter=";">
      <sequential>
        <echo>Found: @{occurance.filepath}</echo>
      </sequential>
    </for>
  </target>
  <target name="test" description="tester - as it will be called from load props">
    <property name="prop.key" value="spidoxoust"/>
    <antcall target="check.prop"/>
  </target>
  <macrodef name="debug">
    <text name="text"/>
    <sequential>
      <if>
        <equals arg1="${debug.enable}" arg2="true"/>
        <then>
          <echo>DEBUG: @{text}</echo>
        </then>
      </if>
    </sequential>
  </macrodef>
</project>

build.properties

debug.enable=true
path.to.labels.bundle=.
target.path=..\\workDir\\DependentLOVtest\\
target.prefix=targetprops
####################### filters ##############################
include.patterns=**/*.xml,**/*.jspx,**/*.jsff,**/*.java,**/*.jsp
exclude.patterns=${exclude.common.patterns}
exclude.common.patterns=**/*build_scripts*/**/*,**/*classes*/**/*, 

                                    **/*.adf*/**/*,**/*.svn*/**/*,**/*.svn-base

not_used_properties.log=not_used_properties.properties


Downloads:

 

The Java Zone is brought to you in partnership with DCHQ. Learn more about best practices for automating the deployment and management of your Java application on any cloud.

Topics:

Published at DZone with permission of Spyros Doulgeridis , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}