Date Created: Fri 08-Oct-2010

Related Document Categories:

Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/


Sample Ant build script that is designed to take an Eclipse based WAR project and manually create a WAR file for distribution by other systems to WebSphere. It is only one way of doing things, but the good thing about this simple approach is that a developer can set up an Ant build project that is independent from the development of the WAR. With a few extra lines this could be made to build an EAR, which is better for enterprise deployment readiness.


<?xml version="1.0" encoding="UTF-8"?>
<project name="Log View EAR build script" default="makewar" basedir=".">
<tstamp>
<format property="buildtimestamp" pattern="yyyy-MM-dd'T'HH:mm:ss" />
</tstamp>

<condition property="isWindows">
<os family="windows"/>
</condition>

<condition property="isMac">
<os family="unix"/>
</condition>

<echo message="==== Eclipse Workdspace Properties ===="/>
<echo message="${workspace_loc}"/>

<property name="root" value=".." />
<echo message="root=${root}"/>

<property name="EARBasePath" value="${root}/LogViewEAR" />
<echo message="EARBasePath=${EARBasePath}"/>

<property name="WARBasePath" value="${root}/LogViewWAR" />
<echo message="WARBasePath=${WARBasePath}"/>

<property name="lib.dir" value="${WARBasePath}/WebContent/WEB-INF/lib" />
<echo message="lib.dir=${lib.dir}"/>

<property name="src.dir" value="${WARBasePath}/src" />
<echo message="src.dir=${src.dir}"/>

<property name="pages.dir" value="${WARBasePath}/WebContent" />
<echo message="pages.dir=${pages.dir}"/>

<echo message="==== Temp Staging Area &amp; Build Properties ===="/>

<property name="package.file" value="LogViewWAR.war" />
<property name="temp.dir" value="c:/temp/antbuild/temp" />
<property name="temp.dir.webcontent" value="${temp.dir}/WebContent" />
<property name="temp.dir.web-inf" value="${temp.dir.webcontent}/WEB-INF" />
<property name="temp.dir.lib" value="${temp.dir.web-inf}/lib" />
<property name="temp.dir.classes" value="${temp.dir.web-inf}/classes" />
<property name="temp.dir.meta-inf" value="${temp.dir.webcontent}/META-INF" />

<property name="dest.dir" value="c:/temp/antbuild/dist/" />


<echo message="===="/>

<target name="mac" if="isMac">
<property file="build.properties.mac" />
<echo message="${os.name}, using ${props.file}" />
</target>

<target name="windows" if="isWindows">
<property file="build.properties.windows" />
<echo message="${os.name}, using ${props.file}" />
</target>

<target name="os" depends="windows,mac">
<path id="classpath">
<fileset dir="lib">
<include name="*.jar" />
</fileset>

</path>
</target>

<!-- CLEAN LAST BUILD ATTEMP -->
<target name="clean" depends="">
<echo>=== CLEAN ===</echo>
<delete dir="${temp.dir}" />
<delete dir="${dest.dir}" />
</target>

<!-- PREPARE FOLDERS FOR DISTRIBUTION -->
<target name="prepare" depends="clean">
<echo>=== PREPARE ===</echo>
<mkdir dir="${dest.dir}" />
<mkdir dir="${temp.dir}" />
<mkdir dir="${temp.dir.lib}" />
<mkdir dir="${temp.dir.webcontent}" />
<mkdir dir="${temp.dir.meta-inf}" />
<mkdir dir="${temp.dir.web-inf}" />
<mkdir dir="${temp.dir.classes}" />
</target>


<!-- BUILD ALL -->
<target name="all" depends="makeear">
<echo>=== BUILD ALL ===</echo>
<delete dir="${build}" />
</target>


<!-- Set up a path id for external JARS reference during compilation of WAR Java files-->
<path id="build.classpath">
<fileset dir="C:/WAS7/AppServer/lib/">
<include name="j2ee.jar" />
</fileset>

<fileset dir="${lib.dir}">
<include name="simple-log.jar" />
</fileset>

</path>

<!-- COMPILE -->
<target name="compile" depends="prepare">
<echo>=== COMPILE ===</echo>
<javac debug="on" srcdir="${src.dir}" destdir="${temp.dir.classes}">
<classpath refid="build.classpath" />

<compilerarg value="-Xlint:unchecked" />

</javac>
</target>

<!-- PACKAGE WAR File -->
<target name="makewar" depends="compile">
<echo>=== PACKAGE WAR ===</echo>

<echo>copy the config files</echo>
<copy file="${WARBasePath}/WebContent/META-INF/MANIFEST.MF" tofile="${temp.dir.meta-inf}/MANIFEST.MF" overwrite="true" />
<!--<copy file="${WARBasePath}/WebContent/WEB-INF/web.xml" tofile="${temp.dir.web-inf}/web.xml" overwrite="true" />-->

<echo>copy the JAR files</echo>
<copy todir="${temp.dir.lib}">
<fileset dir="${WARBasePath}/WebContent/WEB-INF/lib">
<include name="**/*.jar"/>
</fileset>
</copy>

<echo>copy the web content</echo>
<copy todir="${temp.dir.webcontent}">
<fileset dir="${WARBasePath}/WebContent/">
<include name="**/*.zul"/>
<include name="**/*.html"/>
</fileset>
</copy>

<copy todir="${temp.dir.web-inf}">
<fileset dir="${WARBasePath}/WebContent/WEB-INF">
<include name="**/*.xml"/>
<include name="**/*.properties"/>
</fileset>
</copy>

<echo>Build the WAR file...with all resources in place, create the war file</echo>
<war destfile="${dest.dir}/${package.file}" webxml="${temp.dir.web-inf}/web.xml" basedir="${temp.dir}">

<fileset dir="${temp.dir.webcontent}">
<include name="**/*.xml"/>
<include name="**/*.properties"/>
</fileset>

<classes dir="${temp.dir.classes}" />

</war>
</target>

<!-- PACKAGE EAR FILE -->
<target name="makeear" depends="makewar">
<echo>=== MAKE EAR ===</echo>
<ear earfile="${dest.dir}/HRApplicationEAR.ear" appxml="${EARBasePath}/EarContent/META-INF/application.xml">
<fileset dir="${dest.dir}">
<include name="*.war"/>
</fileset>
</ear>

</target>

<!--<target name="all">
<delete dir="${build}" />
</target>-->
</project>

Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/

Steve Robinson - IBM Champion 2013

About Me

Steve Robinson has been working in IT for over 20 years and has provided solutions for many large-enterprise corporate companies across the world. Steve specialises in Java and Middleware.

In January 2013, I was awarded the prestigous 'IBM Champion' accolade.


  • Linked In
  • Twitter
  • About Me
  • My Blog
  • Contact Me

Read my books?

IBM WebSphere Application Server 8.0 Administration Guide

IBM WebSphere Application Server 8.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Categories

Other Categories