blog.onlysimpler.com

A blog... only simpler.

An Ant script for building a web application.

Contains targets for compiling, running junit tests, producing test reports and finally producing a distributable war file.

<project name="Project Name" default="dist" basedir=".">

    <!-- Input and output directories and files -->   
    <property name="src.dir" value="${basedir}/JavaSource" />
    <property name="test.src.dir" value="${basedir}/TestJavaSource" />
    <property name="web.dir" value="${basedir}/WebContent" />
    <property name="build.dir" value="${basedir}/build" />
    <property name="assemble.dir" value="${basedir}/assemble" />
    <property name="assemble.classes.dir" value="${assemble.dir}/WEB-INF/classes" />
    <property name="test.dir" value="${basedir}/test" />
    <property name="dist.dir" value="${basedir}/dist" />
    <property name="lib.dir" value="${basedir}/lib" />   
    <property name="war.file" value="${dist.dir}/projectname.war" />
    
    <!-- Setup the compile path -->   
    <path id="compile.classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar" />
    </path>

    <!-- Setup the test classpath -->
        <path id="test.classpath">
        <path refid="compile.classpath" />
        <pathelement location="${build.dir}" />
        <fileset dir="${web.dir}/WEB-INF/lib" includes="**/*.jar" />   
    </path>

    <!-- Define what to include/exclude in the war file -->
    <patternset id="war.patternset">
        <exclude name="**/*.java" />
        <exclude name="your/package/structure/test/**/*" />
    </patternset>

    <!-- Targets -->

    <target name="clean" description="Prepare for a clean build by deleting the output directories.">
        <delete dir="${build.dir}"/>
        <delete dir="${assemble.dir}" />
        <delete dir="${dist.dir}" />   
        <delete dir="${test.dir}" />
    </target>

    <target name="compile" depends="clean" description="Compile all the code for this project.">
        <mkdir dir="${build.dir}"/>
        <javac srcdir="${src.dir}" destdir="${build.dir}" debug="true" deprecation="true">
            <classpath refid="compile.classpath"/>
            <src path="${test.src.dir}" />
        </javac>
        <!-- make sure we pick up any config files in the source and conf directories -->
        <copy todir="${build.dir}">
            <fileset dir="${src.dir}">
                <include name="**/*.xml" />
                <include name="**/*.properties" />
            </fileset>
        </copy>
    </target>

    <target name="test" depends="compile" description="Run the unit tests.">
        <mkdir dir="${test.dir}" />
        <junit printsummary="on" 
               showoutput="on"
               filtertrace="off"
               fork="true"
               forkmode="perBatch"
               failureproperty="tests.failed">
            <classpath refid="test.classpath"/>
            <batchtest todir="${test.dir}">
                <fileset dir="${test.src.dir}">
                    <include name="**/*Test*.java"/>
                    <exclude name="**/AllTests.java"/>
                </fileset>
                <formatter type="xml" />
           </batchtest>
        </junit>
        <junitreport todir="${test.dir}">
            <fileset dir="${test.dir}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report format="frames" todir="${test.dir}"/>
        </junitreport>
    </target>

    <target name="assemble" 
            depends="test"
            description="Assemble all required artifacts in the build directory.">
        <mkdir dir="${assemble.dir}" />
        <copy todir="${assemble.dir}">
            <fileset dir="${web.dir}">
                <exclude name="WEB-INF/web.xml" />
                <exclude name="WEB-INF/classes" />
                <exclude name="WEB-INF/classes/**/*" />
                <exclude name="META-INF/MANIFEST.MF" />
            </fileset>
        </copy>
        <mkdir dir="${assemble.classes.dir}" />
        <copy todir="${assemble.classes.dir}">
            <fileset dir="${build.dir}" />
        </copy>
    </target>
    
    <target name="dist" depends="assemble" description="Build a distributable war file.">
        <mkdir dir="${dist.dir}" />
        <war destfile="${war.file}" 
             basedir="${assemble.dir}"
             webxml="${web.dir}/WEB-INF/web.xml"
             manifest="${web.dir}/META-INF/MANIFEST.MF">
            <patternset refid="war.patternset"/>
        </war>
    </target>

</project>