R – Simulating the Maven2 filter mechanism using Ant

antfiltermaven-2

I have a properties file, let say my-file.properties.
In addition to that, I have several configuration files for my application where some information must be filled regarding the content of my-file.properties file.

my-file.properties:

application.version=1.0
application.build=42
user.name=foo
user.password=bar

Thus, in my configuration files, I will find some ${application.version}, ${user.name} that will be replaced by their value taken in the properties file…

When I build my application using Maven2, I only need to specify the properties file and say that my resources files are filtered (as in this answer to another problem). However, I need to achieve the same thing by using only Ant.

I've seen that Ant offers a filter task. However, it forces me to use the pattern @property.key@ (i.e. @user.name@ instead of #{user.name}) in my configuration files, which is not acceptable in my case.

How can I solve my problem?

Best Answer

I think expandproperties is what you are looking for. This acts just like Maven2's resource filters.


INPUT

For instance, if you have src directory (one of many files):

<link href="${css.files.remote}/css1.css"/>

src/test.txt


PROCESS

And in my ANT build file we have this:

<project default="default">
   <!-- The remote location of any CSS files -->
   <property name="css.files.remote" value="/css/theCSSFiles" />     
   ...
   <target name="ExpandPropertiesTest">

      <mkdir dir="./filtered"/>

      <copy todir="./filtered">
         <filterchain>
            <expandproperties/>
         </filterchain>     

         <fileset dir="./src" />
      </copy>
   </target>
</project>

build.xml


OUTPUT

*When you run the ExpandPropertiesTest target you will have the following in your filtered directory: *

    <link href="/css/theCSSFiles/css1.css"/>

filtered/test.txt