Friday, October 04, 2013

Ant build script for a simple Java Web App

<?xml version="1.0"?> 
<project name="MyWebApp" default="dist" basedir=".">
 <property name="build" location="bin" />
 <property name="src" location="src" />
 <property name="dist" location="dist" />
 <property name="web" location="web" />
 <!-- Set up the 'env' prefix for environment variables -->
 <property environment="env" />
 <property name="lib" location="lib" />
 <property name="version.num" value="1.0" />
 <path id="classpath">
  <fileset dir="${lib}">
   <include name="**/*.jar" />
  <fileset dir="${env.TOMCAT_HOME}/lib">
   <include name="**/*.jar" />
 <!-- get the source compile classpath in a printable forms -->
 <pathconvert pathsep="${line.separator}|   |-- " property="echo.path.compile" refid="classpath">
 <!-- 0. Abort the build if TOMCAT_HOME is not set -->
 <target name="checkTomcatHome" unless="env.TOMCAT_HOME">
  <fail message="TOMCAT_HOME must be set!" />
 <!-- 0. PRINT DEFAULTS. -->
 <target name="print_default_properties">
  <echo message="          ${}" />
  <echo message="basdir:           ${basedir}" />
  <echo message="ant.file:         ${ant.file}" />
  <echo message=" ${}" />
  <echo message=" ${}" />
  <echo message="tomcat_base:      ${env.TOMCAT_HOME}" />
  <echo message="|-- compile classpath" />
  <echo message="|   |" />
  <echo message="|   |-- ${echo.path.compile}" />
 <!-- 1. CLEAN the build generated artifacts. -->
 <target name="clean">
  <delete dir="${build}" />
  <delete dir="${dist}" />
 <!-- 2. INITIALIZE the build. -->
 <target name="init" depends="print_default_properties, checkTomcatHome">
  <mkdir dir="${build}" />
 <!-- 3. COMPILE the source files. -->
 <target name="compile" depends="init">
  <!-- Compile the java code -->
  <javac srcdir="${src}" destdir="${build}" classpathref="classpath" debug="on" />
 <!-- 4. Create the DISTRIBUTION. -->
 <target name="dist" depends="compile">
  <mkdir dir="${dist}" />
  <copy todir="${build}">
   <fileset dir="${basedir}">
    <include name="*.properties" />
    <include name="*.xml" />
    <include name="*.xsd" />
  <war destfile="${dist}/${}.war" webxml="${web}/WEB-INF/web.xml">
    <attribute name="Built-By" value="${}" />
    <attribute name="Implementation-Version" value="${version.num}" />
    <attribute name="Built-Date" value="${TODAY}" />
   <fileset dir="${web}" />
   <lib dir="${lib}">
    <include name="**/*.jar"/>
   <classes dir="${build}" />
 <!-- 5. DEPLOY the generated WAR to tomcat. -->
 <target name="deploy" depends="dist">
  <echo>Deploying to ${env.TOMCAT_HOME}</echo>
  <copy todir="${env.TOMCAT_HOME}/webapps">
   <fileset dir="${dist}">
    <include name="${}.war" />
 <!-- HELP -->
 <target name="help">
  <echo message="help         - Displays help menu">
  <echo message="compile      - (default) Builds Java classes">
  <echo message="dist         - Creates web applicaiton .war ">
  <echo message="clean        - Delete built .class and .war files, leave directories">
  <echo message="deploy       - Deploy to local tomcat installation">

No comments:

Book Review: Superhuman by Habit: A Guide to Becoming the Best Possible Version of Yourself, One Tiny Habit at a Time by Tynan

Superhuman by Habit: A Guide to Becoming the Best Possible Version of Yourself, One Tiny Habit at a Time by Tynan My rating: 5 of 5 star...