Template is a task for generating files from a template file.
replacement values are stored in a JDBC source or in a CSV file (Comma Separated Values).
The token found in the first line of the data file are replaced by values found in the following lines of the data file. One file is generated per value line of the data file
Attribute | Description | Required |
template | file for which the token should be replaced. | Exactly one. |
data | file in CSV format that contains the values to replace. The first line contains the tokens strings. | Yes, if no dburl. |
driver | Class name of the jdbc driver | Yes, if no data |
dburl | Database connection url | Yes, if no data |
userid | Database user name | Yes, if no data |
password | Database password | Yes, if no data |
statement | the SQL select statement | Yes, if no data |
dir | The directory in which generated files are created. | Yes, Exactly one. |
targetname | The template filename of the generated files (must contains token). | Yes, Exactly one. |
ranktoken | the string of token representing the rank of line in the data file. | No, no default value |
rankstart | the start value of rank. | No, 1 (by default) |
overwrite | Overwrite existing files even if the destination files are newer. | No, defaults to false |
This example illustrates a mailing pack with a CVS data file
<taskdef name="template" classname="fr.imag.adele.ant.templatetask.TemplateTask" classpath="${task.classpath}"/> <target name="test"> <!-- generate the message bodies --> <template template="template.txt" data="data.csv" targetname="__#__.__LASTNAME__.BODY.txt" dir="generated" overwrite="yes" ranktoken="__#__" rankstart="1000" /> <!-- generate the allbuild.xml fragments --> <template template="template.build.xml" data="data.csv" targetname="build.__#__.xml" dir="generated" overwrite="yes" ranktoken="__#__" rankstart="1000" /> <!-- generate the allbuild.xml by concating the fragments --> <concat destfile="generated/allbuild.xml" > <header><![CDATA[ <project name="Send all mails" default="all" basedir="."> <target name="all"> ]]></header> <footer><![CDATA[ </target> </project> ]]></footer> <fileset dir="generated" includes="build.*.xml"/> </concat> </target>
The file data.csv contains:
__LASTNAME__;__FIRSTNAME__;__EMAIL__ MARTIN;Jacques;jm@aol.com DUPONT;Pascal;dp@nowhere.org DUPOND;Georges;dg@yahoo.net
The file template.txt contains:
Happy New Year __FIRSTNAME__ __LASTNAME__ (__#__) Didier
The file template.build.xml contains:
<mail from="youraddress@yourorganization.org" replyto="yourreplyaddress@yourorganization.org" tolist="__EMAIL__" subject="Hello __FIRSTNAME__ __LASTNAME__" messagefile="__#__.__LASTNAME__.BODY.txt" mailhost="yourmailhost" />
This example illustrates a mailing pack with a JDBC datasource
<template template="template.txt" driver="${driver}" dburl="${url}" userid="${userid}" password="${password}" statement="SELECT * FROM Contact;" targetname="__#__.__LASTNAME__.BODY.txt" dir="generated" overwrite="yes" ranktoken="__#__" rankstart="1000" />
The SQL table named Contact is created with those statements:
CREATE TABLE Contact ( __LASTNAME__ VARCHAR(30), __FIRSTNAME__ VARCHAR(30), __EMAIL__ VARCHAR(60) PRIMARY KEY ); INSERT INTO Contact VALUES ('MARTIN','Jacques', 'jm@aol.com'); INSERT INTO Contact VALUES ('DUPONT','Pascal', 'dp@nowhere.org'); INSERT INTO Contact VALUES ('DUPOND','Georges', 'dg@yahoo.net');
Copyright © 2004 Didier Donsez. All rights Reserved.