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

No comments:

Book notes: Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems, by Martin Kleppmann

My notes from the excellent book on how software has evolved to handle data from hierarchical databases to the NoSQL -  https://www.goodrea...