Wednesday, 6 July 2011

JavaEE: Building web project using Ant

Ant is a great tool for building your projects. In this example I'll create simple jar (of course you can create war and ear archives in this case you should use <war ...> and <ear ...> tags) and deploy it on Jboss .

Usually in web projects we have many libraries and need to include them (in my case i use jboss jar)
<?xml version="1.0"?>

<project name="testProject" default="deploy">    
    <property name="" value="testProject"/>
    <property name="" value="${}.jar" />
    <property name="build.dir" value="build"/>
    <property name="classes.dir" location="${build.dir}/classes" />
    <property name="jar.location" location="${build.dir}/jar" />
    <property name="compiler.src.encoding" value="UTF-8" />
    <property name="jboss_home" location="C:/serverTest/jboss-5.0.0.GA" />
    <property name="deploy.dir" location="C:/serverTest/jboss-5.0.0.GA/server/default/deploy" />
    <target name="create-dirs"> 
        <echo message="CREATING DIRS" /> 
        <mkdir dir="${classes.dir}" />
        <mkdir dir="${jar.location}" />
    <target name="clean">
        <echo message="CLEANING PROJECT" /> 
        <delete dir="${build.dir}" />
        <delete dir="${jar.location}" />
    <target name="compile" description="Compiling classes" depends="clean,create-dirs">
        <echo message="COMPILING CLASSES" /> 
        <javac srcdir="ejbModule" destdir="${classes.dir}" 
                optimize="true" encoding="${compiler.src.encoding}" 
    <target name="build-jar" description="Create jar" depends="compile">
        <echo message="CREATING JAR" /> 
        <jar jarfile="${jar.location}/${}" index="true" update="false" duplicate="fail">
            <fileset dir="${classes.dir}" />
    <target name="deploy" depends="build-jar">
        <echo message="DEPLOYING APPLICATION" /> 
        <delete dir="${deploy.dir}/${}" />
        <copy todir="${deploy.dir}" file="${jar.location}/${}" overwrite="true" />

1 comment:

  1. Try Apache Maven (, it is far more simple for use for common tasks like compilation, packaging a jar, war, ear, etc. Also Maven will take care of all your project dependencies, which is a very nice thing.