foreach with Ant

I’ve been building a cool little ANT Compiler script, and found I could really use the ability to loop through a set of files. Namely sql files and then execute each file. I ended up finding a cool little add-on to ant. Ant-Contrib Adds in a bunch of functionality. The piece I used was the foreach task. Check it out below.

The Code (build.xml)

<!-- ===================================== -->
<!-- = Include The Ant-Contrib Functions = -->
<!-- ===================================== -->
<taskdef resource="net/sf/antcontrib/"/>

<!-- ====================================== -->
<!-- = Function to exececute foreach file = -->
<!-- ====================================== -->
<target name="DB.SQL">
	<sql driver="${JDBC.Driver}"
		<transaction src="${SQLFile}"/>

<!-- ================================ -->
<!-- = Scans Folder for *.sql Files = -->
<!-- ================================ -->
<target name="runSQLScripts">
	<foreach target="DB.SQL"
			<fileset dir="." casesensitive="yes">
				<include name="Scripts/*.sql"/>

It loops through every .sql file in the Scripts folder and executes it with the DB.SQL task. 😉


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s