Search code examples
antnant

Ant Bulk Search and Replace


I have a properties file in which the keys represent strings in source code files that I would like to search for, and the values represent the replacement string that I would like to replace the strings with (see below for an example).

I would like to perform these replacements over a set of files during my Ant build, however I cant seem to figure out how to perform this operation. A simple replace is easy using the Ant replacement task, but I can't determine if there is a way using Ant tasks to perform this bulk search and replace using a properties file to indicate what to search and replace. I think I may need to write a script to perform this.

Anyone have any ideas if this is possible using Ant tasks?

Example.props

gameStatusCode=statusCode
gameHomeName=homeName
gameAwayName=awayName


Original Source

if(dataitem.gameStatusCode === 'pre'){
  var tmp = dataitem.gameHomeName;
  ... 
}


Replacement Source

if(dataitem.statusCode === 'pre'){
  var tmp = dataitem.homeName;
  ...
}

Solution

  • Use the replace task with replacefilterfile attribute:

    <replace dir="${src}" replacefilterfile="example.props">
      <include name="**/*.java"/>
    </replace>