DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Haikal has posted 8 posts at DZone. View Full User Profile

Sample Ant Script For Building A Webapp.

  • submit to reddit
        // Sample ant script to build a webapp.

<project name="Combine Enrolments" default="dist" basedir=".">
<!-- Note that you'll need ant 1.7 for this build, as 1.6.5 doesn't have comprehensive filelist support -->
	<!-- Pull in properties -->
	<property file="" />
	<!-- Run time jars. These ones will get distributed alongside this app -->
	<filelist id="dist_jars" dir="${lib.dir}"> 
		<file name="${commons-cli.jar}" />
		<file name="${log4j.jar}" />

	<!-- Build time jars -->
	<filelist id="misc_jars">
		<file name="${servlet-api.jar}" />

	<path id="classpath">
        <pathelement location="${build.classes.dir}"/>
 		<filelist refid="dist_jars" />
		<filelist refid="misc_jars" /> 
	<!-- TARGETS -->
	<target name="javadoc" depends="init" description="Generate javadoc">
		<echo>Generating javadoc</echo>
		<javadoc packagenames="*"
				destdir="${doc.dir}" >
			<classpath refid="classpath"/>
	<target name="dist" depends="build,copy_libs" description="Generate an executable jar, ready for distribution">
		<copy file="" todir="${build.classes.dir}"  />

		<pathconvert property="jar.classpath" pathsep=" ">
			    <!-- remove absolute path -->
		        <flattenmapper />

		        <!-- add lib/ prefix -->
		        <globmapper from="*" to="lib/*" />

		  	<!-- lib.home contains all jar files, in several subdirectories -->
	    		<filelist refid="dist_jars" />
		<jar basedir="${build.classes.dir}" destfile="${dist.dir}/${}">
				<attribute name="Main-Class" value="${jar.main.class}"/>

	<target name="build" depends="init,copy_libs" description="Compile the thing already">
		<echo>Building ${}</echo>
		<javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="${build.debug}" >
			<classpath refid="classpath"/>
	<target name="copy_libs" depends="init" description="Copy runtime  dependencies to the dist directory">
		<echo>Copying Dependencies</echo>
		<copy todir="${dist.lib.dir}" >
			<filelist refid="dist_jars" />
			<mapper type="flatten" />
		<copy todir="${build.lib.dir}" >
			<filelist refid="dist_jars" />
			<mapper type="flatten" />
	<target name="clean" depends="init"  description="Clean up build structure">
		<delete dir="${build.dir}" includeEmptyDirs="true"/>
		<delete dir="${dist.dir}" includeEmptyDirs="true" />
		<delete dir="${doc.dir}" includeEmptyDirs="true" />
	<target name="init" description="Initialise build process">
		<echo> Building ${} with Ant Version ${ant.version}</echo>
		<mkdir dir="${build.classes.dir}" />
		<mkdir dir="${build.lib.dir}"/>
		<mkdir dir="${doc.dir}"/>
		<mkdir dir="${dist.dir}" />
		<mkdir dir="${dist.lib.dir}" />