Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Search Properties Within Files Using Ant

DZone's Guide to

Search Properties Within Files Using Ant

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

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:

 

Are you using Bitbucket to accomplish your company's mission? Share your company’s mission with #Forthecode for a chance to be featured on our homepage, our social media channels, or win a free t-shirt!

Topics:

Published at DZone with permission of Spyros Doulgeridis, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}