Simple Ant Text Preprocessor

During development sometime it's necessary to generate text files dynamically. This can be source code, HTML files, CSS, XML, properties files or some configuration, etc. Common thing for all these files is that it's text file, usually with default encoding say UTF-8 and requires only in one or to places to be inserted dynamically, depending on environment or deployment box.

There are many complex engines which can be used as text preprocessor: XSLT, Velocity based, JSPs, ASPs etc. These tools are powerful but requires in more efforts in setup or programming. Sometime developer needs in more simple and easy solution, like String.replace etc.

Pete Eakle published Using Ant as a Text Substitution Preprocessor article and described method which is easy and simple for usage. Unfortunately described code is static, and macros names are hardcoded and limited in usage.

Ant already provides this kind of functionality by itself - as ExpandProperties filter: if the data contains data that represents Ant properties (of the form ${...}), that is substituted with the property's actual value. So why don't use Ant itself it as text preprocessor engine? In this case template text file should be written in Ant syntax, and in any place you need in custom value just use ${myproperty} or similar thing. This aproach is flexible and dynamic, as all Ant properties available in current execution context will go into ExpandProperties, and we can load property files or define property in code or use some buil-in ones e.g. ${os.name} or ${user.name} etc in text template.

Target "anttemplate" uses described above ideas to replace tokens in text file. It accepts only one parameter - "dir", specifying directory name where project files resides. Target iterates over all files with *.anttemplate extension, expands Ant properties, determine target file name - as template name without .anttemplate extension, saves new file to disk and deletes original .anttemplate file:

	
	<target name="anttemplate">
		<property name="dir.in"  value="${dir}"/>
		<property name="dir.out" value="${dir}"/>
		
		<echo>dir.in=${dir.in}, dir.out=${dir.out}</echo>
		
		<tstamp>
			<format property="anttemplate.date" pattern="dd-MMM-yyyy HH:mm:ss"/>	
		</tstamp>
		<property name="anttemplate.info" value="NOTE: Automatically generated file by Vadim Melnik Ant Text Preprocessor : ${anttemplate.date}"/>
		
		<mkdir dir="${dir.out}"/>
		
		<copy todir="${dir.out}" verbose="true" overwrite="true" failonerror="true">
			<fileset dir="${dir.in}">
				 <include name="**/*.anttemplate"/>
			</fileset>
			<mapper>
				<globmapper from="*.anttemplate" to="*"/>
			</mapper>
	        <filterchain>
				<expandproperties/>
	        </filterchain>
		</copy>
		<antcall target="anttemplate-delete-template">
			<param name="dir" value="${dir.in}"/>
		</antcall>
	</target>	
	

Template engine usage is very simple:

	<property name="test.dir" value="test"/>
	<property file="build.properties"/>
...
	<antcall target="anttemplate">
		<param name="dir" value="${test.dir}"/>
	</antcall>

Now in project tree under "test" folder we have "mypool.properties.anttemplate" file, like listed below:


#Database connection profile
# ${anttemplate.info}
url=${jdbc.url}
user=${jdbc.user}
password=${jdbc.password}
driver=com.mysql.jdbc.Driver
	

Also we created our own "build.properties" file with dynamic properties values:


#
test.copyright=Copyright � 2010, Some company.
#
jdbc.url=jdbc\:mysql\://localhost\:3306/schema
jdbc.user=root
jdbc.password=sql

After executing "anttemplate" target, "mypool.properties.anttemplate" is transformed into "mypool.properties" file with following content:


#Database connection profile
# NOTE: Automatically generated file by Ant Text Preprocessor : 07-Mar-2010 09:30:53
url=jdbc:mysql://localhost:3306/schema
user=root
password=sql
driver=com.mysql.jdbc.Driver

Another example is "index.html.anttemplate":

<html>
	<head>
		<title>Test page</title>
	</head>
<body>
<h1>Hello World!</h1>

<b>Date:</b> <i>${anttemplate.date}</i>.

<br>	
<hr>
<i>${test.copyright}</i>	
</body>
</html>

Tarnsformed to "index.html" file with following content:

<html>
	<head>
		<title>Test page</title>
	</head>
<body>
<h1>Hello World!</h1>

<b>Date:</b> <i>07-Mar-2010 09:30:53</i>.

<br>	
<hr>
<i>Copyright � 2010, Some company.</i>	
</body>
</html>

Downloads / Source Code

Test Eclipse project AntTextPreprocessor - ant_text_preprocessor.zip.

Source Code https://github.com/vmdocua/ant-text-preprocessor on GitHub.

Have a fun.


Revision:1, Last Modified: Mar/07/2010 15:14 PM, Copyright © Vadim Melnik 2005-2010. Visit my Homepage.