Tuesday, October 11, 2011

Sample Ant Builds

Below are two Ant builds for Java Web JEE projects, using default Eclipse folder structure. Each copies some files that need to be different in the Test vs. Prod build. These were run using Ant 1.8. I'm not indicating these use "best practices," just some that worked for me.

Ant Build.xml One

<project name="myproject" default="dist" basedir=".">

<property name="project.name" value="MyProject" />
<property name="dist.dir" location="dist" />
<property name="dist.prod.dir" location="${dist.dir}/prod" />
<property name="dist.test.dir" location="${dist.dir}/test" />
<property name="conf.prod" location="conf/prod" />
<property name="conf.test" location="conf/test" />
<property name="web.dir" location="WebContent" />
<property name="web.lib" location="${web.dir}/WEB-INF/lib" />
<property name="web.script" location="${web.dir}/scripts" />

<path id="classpath">
<pathelement location="${web.lib}/jstl.jar"></pathelement>
<pathelement location="${web.lib}/standard.jar"></pathelement>
</path>

<target name="dist">
<tstamp />
<echo message="${ant.project.name}: ${ant.file}" />
<delete dir="${dist.dir}"/>
<mkdir dir="${dist.test.dir}"/>
<mkdir dir="${dist.prod.dir}"/>

<echo>Building PROD war file</echo>
<copy file="${conf.prod}/script/validate.js" tofile="${web.script}/validate.js"
overwrite="true" />
<copy file="${conf.prod}/WEB-INF/web.xml" tofile="${web.dir}/WEB-INF/web.xml"
overwrite="true" />
<war destfile="${dist.prod.dir}\${project.name}.war" webxml="${web.dir}\WEB-INF\web.xml">
<fileset dir="${web.dir}" />
</war>

<echo>Building TEST war file</echo>
<copy file="${conf.test}/script/validate.js" tofile="${web.script}/validate.js"
overwrite="true" />
<copy file="${conf.test}/WEB-INF/web.xml" tofile="${web.dir}/WEB-INF/web.xml"
overwrite="true" />
<war destfile="${dist.test.dir}\${project.name}.war" webxml="${web.dir}\WEB-INF\web.xml">
<fileset dir="${web.dir}" />
</war>
<echo>${ant.project.name} build complete</echo>
</target>

</project>


Ant Build.xml Two

<?xml version = '1.0' encoding = 'windows-1252'?>

<project name="myproject" default="make" basedir=".">

<!--Set the output directories-->
<property name="compile.outdir" value="build/classes"/>
<property name="dist.outdir" value="dist"/>

<!-- Project directories -->
<property name="src.dir" value="src"/>
<property name="web.dir" value="WebContent"/>
<property name="web.lib.dir" value="WebContent/WEB-INF/lib"/>
<property name="conf.dir" value="conf"/>
<property name="test.dir" value="test"/>
<property name="prod.dir" value="prod"/>

<!-- Project configuration -->
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.6"/>
<property name="source" value="1.6"/>

<!-- Web application names -->
<property name="webapp.name.test" value="myproject"/>
<property name="webapp.name.prod" value="myproject"/>

<!-- Create a fileset of the webapp libraries -->
<fileset dir="${web.lib.dir}" id="webapp.lib" />

<!-- Create a fileset of the webapp libraries -->
<fileset dir="external.lib/" id="external.lib" />

<!--Set the classpath-->
<path id="classpath">
<pathelement location="${compile.outdir}"/>
<fileset refid="webapp.lib" />
<fileset refid="external.lib" />
</path>

<!--Set the source path-->
<path id="srcpath">
<pathelement location="${src.dir}"/>
</path>

<target name="init">
<tstamp/>
</target>

<target name="compile" depends="init">
<mkdir dir="${compile.outdir}"/>
<echo>My Project compile</echo>
<!--Compile Java source files-->
<javac debug="true" debuglevel="${debuglevel}" destdir="${compile.outdir}" source="${source}" target="${target}" includeantruntime="false">
<classpath refid="classpath"/>
<src refid="srcpath"/>
</javac>
<!--Copy files to output directory-->
<copy file="${src.dir}/com/mysite/myproject/dbutil/Database.properties"
tofile="${compile.outdir}/com/mysite/myproject/dbutil/Database.properties"/>
<copy file="${src.dir}/messages.properties" tofile="${compile.outdir}/messages.properties"/>
<copy file="${src.dir}/log4j.xml" tofile="${compile.outdir}/log4j.xml"/>
<copy file="${src.dir}/log4j.dtd" tofile="${compile.outdir}/log4j.dtd"/>
</target>

<target name="clean">
<!--Delete output directories-->
<delete dir="${compile.outdir}"/>
</target>

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

<target name="make" depends="compile"/>

<target name="rebuild" depends="clean,compile"/>

<macrodef name="build-war">
<attribute name="dist.conf" />
<attribute name="webapp.name" />
<sequential>
<!-- Files differ between Test and Prod in conf dir: -->
<!-- Database.properties, log4j.xml, web.xml, logon.jsp -->
<mkdir dir="${dist.outdir}/@{dist.conf}" />
<mkdir dir="${dist.outdir}/@{dist.conf}/classes" />
<copy todir="${dist.outdir}/@{dist.conf}/classes">
<fileset dir="${compile.outdir}" />
</copy>
<copy todir="${dist.outdir}/@{dist.conf}/classes" overwrite="true">
<fileset dir="${conf.dir}/@{dist.conf}/classes" />
</copy>
<copy file="${conf.dir}/@{dist.conf}/WebContent/signon.jsp"
todir="${web.dir}" overwrite="true" >
</copy>

<war destfile="${dist.outdir}/@{dist.conf}/@{webapp.name}.war" webxml="${conf.dir}/@{dist.conf}/WebContent/WEB-INF/web.xml">
<fileset dir="${web.dir}/" excludes="test*.jsp,/workqueue/reports/**/*.csv"/>
<classes dir="${dist.outdir}/@{dist.conf}/classes" excludes="" />
</war>

<delete dir="${dist.outdir}/@{dist.conf}/classes" />
</sequential>
</macrodef>

<target name="dist" depends="cleanall,rebuild">
<build-war dist.conf="${prod.dir}" webapp.name="${webapp.name.prod}" />
<build-war dist.conf="${test.dir}" webapp.name="${webapp.name.test}" />
</target>
</project>

No comments:

Post a Comment