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="app.name" value="testProject"/>
    <property name="jar.name" value="${app.name}.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>
    
    <target name="clean">
        <echo message="CLEANING PROJECT" /> 
        <delete dir="${build.dir}" />
        <delete dir="${jar.location}" />
    </target>
    
    <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}" 
                classpath="${jboss_home}/common/lib/jboss-javaee.jar">                
        </javac>
    </target>
    
    <target name="build-jar" description="Create jar" depends="compile">
        <echo message="CREATING JAR" /> 
        <jar jarfile="${jar.location}/${jar.name}" index="true" update="false" duplicate="fail">
            <fileset dir="${classes.dir}" />
        </jar>
    </target>
        
    <target name="deploy" depends="build-jar">
        <echo message="DEPLOYING APPLICATION" /> 
        <delete dir="${deploy.dir}/${jar.name}" />
        <copy todir="${deploy.dir}" file="${jar.location}/${jar.name}" overwrite="true" />
    </target>
</project> 

1 comment:

  1. Try Apache Maven (http://maven.apache.org/), 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.

    ReplyDelete