Monday, April 12, 2010

ANT build file for Java web projects

The ANT build file below can be used to compile and package Java web projects discussed in the previous post. What also can be added are targets to call (unit) tests and automatic application deployment code (copying the *.war file to an autodeploy directory).

The project settings have to be customized for every project. The rest can be reused.

<project name="webproject" basedir="." default="package">
<!-- PROJECT SETTINGS -->
<property name="project.name" value="webproject" />
<property name="project.war"
value="${project.name}.war" />

<!-- FIXED PATHS -->
<property name="path.build" value="build" />
<property name="path.sources" value="src/java" />
<property name="path.classes"
value="build/WEB-INF/classes" />
<property name="path.dist" value="dist" />
<property name="path.lib" value="lib" />
<property name="path.web" value="web" />

<!-- CLEAN -->
<target name="clean">
<echo message="CLEANING PROJECT" />
<delete includeemptydirs="true">
<fileset dir="${path.build}" />
<fileset dir="${path.dist}" />
</delete>
<mkdir dir="${path.classes}" />
<mkdir dir="${path.dist}" />
</target>

<!-- COMPILE -->
<target name="compile" depends="clean">
<echo message="COMPILING PROJECT" />
<path id="lib.path.ref">
<fileset dir="${path.lib}"
includes="*.jar"/>
</path>

<javac srcdir="${path.sources}"
destdir="${path.classes}"
classpathref="lib.path.ref"
debug="true">
</javac>
</target>

<!-- PACKAGE -->
<target name="package" depends="compile">
<echo message="PACKAGING PROJECT" />
<copy todir="${path.build}"
includeEmptyDirs="yes">
<fileset dir="${path.web}" />
</copy>
<copy todir="${path.build}/WEB-INF/classes">
<fileset dir="${path.sources}">
<filename name="**/*.properties" />
</fileset>
</copy>
<copy todir="${path.build}/WEB-INF/lib"
includeEmptyDirs="yes">
<fileset dir="${path.lib}" />
</copy>
<war destfile="${path.dist}/${project.war}"
basedir="${path.build}"/>
</target>
</project>

No comments:

Post a Comment