How do I compile my project using Ant?
Author: Deron Eriksson
Description: This Ant tutorial describes how to compile Java classes using the javac task and filesets.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20

Page: < 1 2

(Continued from page 1)

So, let's create our build script so that we include the required jarW files in the 'javac' task's classpathW (note that this is a completely different classpath than the EclipseSW 'tomcat-demo' project's .classpath file). To do this, I created an AntSW 'path' with 'project-classpath' as it's id. This path includes 4 sets of files, each of which is called a 'fileset'.


<?xml version="1.0" encoding="UTF-8"?>
<project name="tomcat-demo" default="compile" basedir=".">
	<property name="tomcat-home" value="/apache-tomcat-5.5.20" />

	<path id="project-classpath">
		<fileset dir="web/WEB-INF/lib" includes="*.jar" />
		<fileset dir="${tomcat-home}/bin" includes="*.jar" />
		<fileset dir="${tomcat-home}/common/lib" includes="*.jar" />
		<fileset dir="${tomcat-home}/server/lib" includes="*.jar" />

	<target name="clean">
		<delete dir="bin" />
		<mkdir dir="bin" />

	<target name="copy-non-java-files">
		<copy todir="bin" includeemptydirs="false">
			<fileset dir="src" excludes="**/*.java" />

	<target name="compile" depends="clean,copy-non-java-files">
		<javac srcdir="src" destdir="bin" classpathref="project-classpath" />


One of the filesets refers to all the jar files within the project's web/WEB-INF/lib/ directory so that we can have a reference to the commons-lang jar file being utilized by TestServlet. The other three filesets refer to all the jar files within Tomcat's bin/, common/lib/, and server/lib/ directories so that we can reference classes such as HttpServlet, which TestServlet extends.

The 'project-classpath' path is referenced by the javac task via the 'classpathref' attribute.

For housekeeping, I created a 'clean' target to delete the bin directory and its contents and to recreate it, and I added this target as a dependency to be run before the 'compile' target. I also created a 'copy-non-java-files' target to copy all the files that don't have a .java extension from the src to the bin directory. This feature can be useful if you're using things like HibernateW mapping files, which are located within your src directory.

For convenience, we can add our 'tomcat-demo' build.xml file to our Ant view in Eclipse.

Eclipse Ant View

If we double-click tomcat-demo's 'compile' target in Eclipse, we see the following result in our console:

Buildfile: C:\projects\workspace\tomcat-demo\build.xml
   [delete] Deleting directory C:\projects\workspace\tomcat-demo\bin
    [mkdir] Created dir: C:\projects\workspace\tomcat-demo\bin
     [copy] Copying 1 file to C:\projects\workspace\tomcat-demo\bin
    [javac] Compiling 1 source file to C:\projects\workspace\tomcat-demo\bin
Total time: 1 second

If we inspect our bin directory (I won't do that here), we can verify that our TestServlet.class indeed was compiled and placed in the bin directory, and the file was also moved to bin.

Just as an interesting aside, we could easily replace

	<fileset dir="${tomcat-home}/bin" includes="*.jar" />
	<fileset dir="${tomcat-home}/common/lib" includes="*.jar" />
	<fileset dir="${tomcat-home}/server/lib" includes="*.jar" />


	<fileset dir="${tomcat-home}" includes="**/*.jar" />

Personally I feel that the first representation of the TomcatSW jar files gives a more accurate representation of the jar files being utilized than the second approach, so I prefer the lengthy but more informative first approach.

Page: < 1 2