Automating software compilation, test, packaging and delivery with Ant
Ant is a great tool for working with Java projects.
In fact, maybe the need for it does not come as an evidence to you if you’ve been using an IDE like eclipse which does all the path resolution and compilation by some kind of magic! However, when not all developers on a project use the same IDE or if you want to have your source on an automated build system, you’ll find ant very powerfull. With ant, I’m able to have an automated build system compile all my sources, run all my jUnit tests, generate Javadoc, several JARs (with javadoc, with source, without javadoc, …) and run a CheckStyle utility on the source ! Moreover, I can run all this by typing a single command: ant
An Ant build script is an XML file composed of various elements. The first kind of element is the “property” element which can be used for defining constants:
<property name="build.dir" location="bin" />
You can also define “path” elements such as your classpath:
<path id="project.classpath"> <pathelement location="${build.prod.dir}" /> <pathelement location="${build.test.dir}" /> <fileset dir="${lib.dir}"> <include name="*.jar" /> </fileset> </path>
and then you have several “target” elements. Each of these target elements contains information on what should be done. For example, a target that will create a JAR is given below:
<target name="jar" depends="compile"> <mkdir dir="${build.jar.dir}"/> <jar destfile="${build.jar.dir}/${ant.project.name}.jar" basedir="${build.prod.dir}"> <manifest> <attribute name="Main-Class" value="eu.netqos.apa.APA"/> </manifest> </jar> </target>
The “depends” attribute to the target element refers to another target element which should be executed before the current target instructions are performed.
Extensive documentation can be found on the Ant website which is very well written and contains all the details you’ll need.
A complete build.xml file for a typical project is given below:
<?xml version="1.0"?> <project name="MyProject" default="compile" basedir="."> <property name="build.dir" location="bin" /> <property name="build.prod.dir" location="${build.dir}/prod"/> <property name="build.test.dir" location="${build.dir}/test"/> <property name="build.jar.dir" location="${build.dir}/jar"/> <property name="doc.dir" location="doc" /> <property name="src.dir" location="src" /> <property name="test.dir" location="test" /> <property name="test.xml.dir" location="${build.dir}/test-results" /> <property name="lib.dir" location="lib" />
<path id="project.classpath"> <pathelement location="${build.prod.dir}" /> <pathelement location="${build.test.dir}" /> <fileset dir="${lib.dir}"> <include name="*.jar" /> </fileset> </path>
<target name="prepare"> <mkdir dir="${build.prod.dir}" /> <mkdir dir="${build.test.dir}" /> <mkdir dir="${build.jar.dir}" /> <mkdir dir="${test.xml.dir}" /> <mkdir dir="${doc.dir}" /> </target>
<target name="compile" depends="prepare"> <javac srcdir="${src.dir}" destdir="${build.prod.dir}"> <classpath refid="project.classpath" /> </javac> </target>
<target name="compile-tests" depends="compile"> <javac srcdir="${test.dir}" destdir="${build.test.dir}"> <classpath refid="project.classpath" /> </javac> </target>
<target name="test" depends="compile-tests"> <junit haltonfailure="false"> <classpath refid="project.classpath" /> <formatter type="brief" usefile="false" /> <formatter type="xml" /> <batchtest todir="${test.xml.dir}"> <fileset dir="${build.test.dir}" includes="**/*Test.class" /> </batchtest> </junit> </target>
<target name="javadoc"> <javadoc packagenames="net.vanwambeke.nicolas.*" sourcepath="src" defaultexcludes="yes" destdir="doc" author="true" version="true" use="true" windowtitle="MyProject API"> <classpath refid="project.classpath" /> </javadoc> </target> <target name="jar" depends="compile"> <mkdir dir="${build.jar.dir}"/> <jar destfile="${build.jar.dir}/${ant.project.name}.jar" basedir="${build.prod.dir}"> <manifest> <attribute name="Main-Class" value="eu.netqos.apa.APA"/> </manifest> </jar> </target>
<target name="cc-build" depends="test,javadoc,jar,checkstyle" /> <target name="checkstyle"> <taskdef resource="checkstyletask.properties" classpath="/home/nvanwam/checkstyle-4.3/checkstyle-all-4.3.jar"/> <checkstyle config="/home/nvanwam/checkstyle-4.3/sun_checks.xml" maxErrors="200"> <fileset dir="${src.dir}" includes="**/*.java"/> <formatter type="plain"/> <formatter type="xml" toFile="${test.xml.dir}/checkstyle_errors.xml"/> </checkstyle> </target>
</project>Tags: Technical