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.

04.02.2007
| 17824 views |
  • 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="build.properties" />
	
	<!-- DEFINITIONS -->
	<!-- 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}" />
	</filelist>

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

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

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

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

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

	<target name="build" depends="init,copy_libs" description="Compile the thing already">
		<echo>Building ${ant.project.name}</echo>
		<javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="${build.debug}" >
			<classpath refid="classpath"/>
		</javac>
	</target>
	
	<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>
		<copy todir="${build.lib.dir}" >
			<filelist refid="dist_jars" />
			<mapper type="flatten" />
		</copy>
	</target>
	
	<target name="clean" depends="init"  description="Clean up build structure">
		<echo>Cleaning...</echo>
		<delete dir="${build.dir}" includeEmptyDirs="true"/>
		<delete dir="${dist.dir}" includeEmptyDirs="true" />
		<delete dir="${doc.dir}" includeEmptyDirs="true" />
	</target>
	
	<target name="init" description="Initialise build process">
		<tstamp/>
		<echo> Building ${ant.project.name} 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}" />
	</target>
</project>