Ant Template for Java Web Application

Suppose you have a Java web application that has structure as follow:

- Project
...- src
.....+ com
......jdbc.properties
......log4j.xml
...- war
......- WEB-INF
.........+ classes
.........+ jsp
.........+ css
...........applicationContext.xml
......+ images
.......build.num
.......build.xml
.......build.properties
.......MANIFEST.MF

src is the source folder.
src/jdbc.properties contains all settings related to JDBC connection.
src/log4j.xml contains all settings related to logging properties.
war/WEB-INF/classes is the folder where the compiled java class files will be put into.
war/WEB-INF/jsp is the folder that contains all JSP file or velocity/freemarker template (if you use any).
war/WEB-INF/css is the folder that contains all CSS file.
war/WEB-INF/applicationContext.xml is the spring configuration file.
war/images is the folder that contain all images file
build.num is an ant generated file for helping incrementing the build number.
build.xml is the ant build file.
build.properties is the ant properties file.
MANIFEST.MF is the generated manifest file.


Below is an example of the build.xml file for a simple, flexible and sufficient solution of project build file.

And here is the build.properties file.


For a copy paste, the text will be provided below:



<?xml version="1.0"?>
<project name="Admin" default="all" basedir=".">
<property file="build.properties" />
<path id="classpath">
<fileset dir="${project.lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>

<target name="clean" depends="clean-class">
<delete dir="${dist.dir}" />
</target>

<target name="clean-class" description="clean all classes">
<delete dir="${build.dest}" />
</target>

<target name="compile" description="compile src">
<mkdir dir="${build.dest}" />
<javac source="1.5" target="1.5" srcdir="${src.dir}" destdir="${build.dest}" debug="true">
<classpath refid="classpath" />
</javac>
<copy todir="${build.dest}">
<fileset dir="${src.dir}">
<exclude name="**/*.java" />
</fileset>
</copy>
</target>

<!-- Create binary distribution -->
<target name="dist" description="Create binary distribution">
<buildnumber file="build.num" />
<tstamp>
<format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>
<manifest file="MANIFEST.MF">
<attribute name="Built-By" value="${user.name}" />
<attribute name="Implementation-Version" value="${version.num}.${build.number}" />
<attribute name="Built-Date" value="${TODAY}" />
</manifest>
<mkdir dir="${dist.dir}" />
<copy todir="${dist.dir}/${project.distname}.war">
<fileset dir="${war.dir}" />
<fileset file="MANIFEST.MF" />
</copy>
</target>
<target name="all" depends="clean,compile,dist" />
</project>




project.dir=.
war.dir=${project.dir}/war
project.web.dir=${project.dir}/war/WEB-INF
project.lib.dir=${project.dir}/war/WEB-INF/lib
build.dest=${project.web.dir}/classes
src.dir=${project.dir}/src
test.dir=${project.dir}/src/
docs.dir=${project.dir}/docs
dist.dir=${project.dir}/dist/
java.home=/home/user/jdk1.5.0_16/

version.num=2.0.1
project.distname=projectname


0 comments: (+add yours?)