The FreeMarker Ant Task

You can use FreeMarker to transform XML documents inside Ant buildfiles.

In order to do this, you must first define the freemarker.ext.ant.FreeMarkerXmlTask inside your buildfile, then call the task. Suppose you want to transform several XML documents to HTML using the hypothetical "xml2html.template" template, with XML documents being located in the directory "xml" and HTML documents generated into directory "HTML". You would write something like:

	

<taskdef name="freemarker" classname="freemarker.ext.ant.FreeMarkerXmlTask">
  <classpath>
    <pathelement location="freemarker.jar" />
    <pathelement location="jdom.jar" />
  </classpath>
</taskdef>
<mkdir dir="html" />
<freemarker basedir="xml" destdir="html" includes="**/*.xml" template="xml2html.template" />

	
	

The task would invoke the template for every XML document. Every document would be parsed into a JDOM tree, then wrapped into a freemarker.ext.jdom.NodeListModel (see "Model for XML") and made available as a variable named "document" in the root model. Further, all properties defined by the build file would be made available as a hash model named "properties". Several other models are made available; for detailed description of what variables are made available to templates as well as what other attributes can the task accept, see the JavaDoc for freemarker.ext.ant.FreeMarkerXmlTask.