<?xml version="1.0"?>
<!-- $Id: build.xml,v 1.8 2004/03/21 00:57:25 jnorman Exp $ -->

<!-- NOTES ON THIS BUILD SCRIPT
  -
  -  (0) This script is for a J2EE project with one ear in one war, deploying to
  -      the J2EE RI (1.3) or to JBoss (3.2.x).
  -
  -  (1) Things you might want to change
  -      (a) Names of Harvard libraries this project depends on (see item 2).
  -          Change these in the fileset harvard.libraries.
  -      (b) Names of off the shelf libraries that should be included in the war:
  -          See the target war-nocompile.
  -      (c) Classes that should be tested: See the target Test.
  -
  -  (2) More on Inclusion of libraries
  -      (a) Some libraries are pulled into the war's WEB-INF/lib from projects
  -          that are parallel to this one. These libraries are defined in the
  -          fileset with id harvard.libraries - if the project needs one of
  -          these, then it should be listed for that fileset. If the other
  -          projects are not parallel to this one, but are parallel to each
  -          other, you can define the environment variable HU to point to the
  -          parent directory of the supporting projects.
  -      (b) Some libraries are pulled into the war's WEB-INF/lib from various
  -          "off the shelf" projects such as JSTL, etc. These libraries are
  -          copied one by one in the target war-nocompile. In the default
  -          setup, these libraries are defined in build.properties. Again, in
  -          the default, all "off the shelf" libraries are under a directory
  -          defined by the env.OTS property (default is /OTS). If your "off the
  -          shelf" libraries are spread all over your system, then you should
  -          just redefine the appropriate library property in build.properties.
  -          For example, you might define jaxp.jar to be, say,
  -          D:/xerces-2_6_1/xml-apis.jar if that is where it lives on your system.
  -
  -  (3) Notes on targets:
  -        compile, war-nocompile:
  -          Classes with "Test" in the name are excluded from the jar of compiled
  -          classes, and are excluded from the war
  -
  -  (4) TODO: Support for EJB
  -->
<project name="DataSource Test" default="env" basedir=".">

	<description>A test for deploying an application and using a datasource to Tomcat</description>

	<property environment="env" />
	<property name="env.OTS" value="C:/ots" />
	<property name="env.JBOSS_SERVER" value="default" />
	<property file="build.properties" />
	
	<taskdef resource="net/sf/antcontrib/antcontrib.properties">
      <classpath>
        <pathelement location="${ant_contrib.jar}"/>
      </classpath>
    </taskdef>

	<taskdef name="tomcat-remove"   classname="org.apache.catalina.ant.RemoveTask"/>
	<taskdef name="tomcat-install"  classname="org.apache.catalina.ant.InstallTask"/>
	<taskdef name="tomcat-reload"   classname="org.apache.catalina.ant.ReloadTask"/>
	<taskdef name="tomcat-undeploy" classname="org.apache.catalina.ant.UndeployTask"/>
	<taskdef name="tomcat-deploy"   classname="org.apache.catalina.ant.DeployTask"/>
	<taskdef name="tomcat-stop"     classname="org.apache.catalina.ant.StopTask"/>
	<taskdef name="tomcat-start"     classname="org.apache.catalina.ant.StartTask"/>
	
	<!-- Local copy of libraries needed for compile AND for deployment in war/WEB-INF/lib -->
	<fileset id="supporting.libraries" dir=".">
		<include name="supporting-libraries/*.jar" />
		<include name="${jdbc.lib}/*.jar" />
	</fileset>

	<path id="build.classpath">
		<pathelement location="${servlet.jar}" />
		<pathelement location="${jdbc.jar}" />
	</path>

	<patternset id="no-sources">
		<exclude name="**/src/*" />		<!-- Exclude standard source directory -->
		<exclude name="**/*.java" />	<!-- Exclude any stray .java files -->
	</patternset>

	<!-- Initialization: Increment build number and display build start time -->
	<target name="-init">
		<tstamp>
			<format property="init.time" pattern="MM/dd/yyyy hh:mm" />
		</tstamp>
		<buildnumber file="antbuildnumber.txt" />
		<echo message="ant build #${build.number} for ${app.name} started at ${init.time}" />
	</target>

	<target name="clean" description="Delete files in ${java.classes} and ${dist} so new build is clean" depends="-init">
		<delete dir="${java.classes}" quiet="true" />
		<delete dir="${dist}" quiet="true" />
		<mkdir dir="${java.classes}" />
		<mkdir dir="${dist}" />
	</target>

	<target name="compile" description="Compile code from ${java.src} to ${java.classes}" depends="clean">
		<echo>${java.classes}</echo>
	    <!-- Compile all code, including both application classes and generated web service stubs -->
		<javac destdir="${java.classes}" debug="on">
			<src>
				<pathelement path="${java.src}" />
            </src>
			<classpath>
				<path refid="build.classpath" />
				<fileset refid="supporting.libraries" />
			</classpath>
		</javac>
		<rmic base="${java.classes}" includes="**/rmi/*.class" />
    	<jar destfile="${dist}/${app.name}.jar" basedir="${java.classes}" compress="false" excludes="**/*Test*.*" />
	</target>

	<target name="compile-tests" description="Compile JUnit tests from ${java.test} to ${java.classes}" depends="compile">
		<javac destdir="${java.classes}" debug="on">
			<src>
				<pathelement path="${java.test}" />
			</src>
			<classpath>
				<path refid="build.classpath" />
				<fileset refid="supporting.libraries" />
			</classpath>
		</javac>
	</target>

	<!-- This target will work with plain old ant -->
<!--
	<target name="test" description="Test with JUnit">
		<java fork="yes" classname="junit.textui.TestRunner" taskname="junit" failonerror="true">
			<arg value="AllTests" />
			<classpath>
				<pathelement path="${java.classes}" />
				<path refid="build.classpath" />
				<fileset refid="supporting.libraries" />
			</classpath>
		</java>
	</target>
-->

	<!-- To use this target, put the junit.jar in the ant lib/ directory -->
	<target name="test" description="Test with JUnit" depends="compile-tests">
		<junit fork="on" printsummary="true" haltonfailure="on">
			<test name="AllTests" />
			<formatter type="plain" usefile="false" />
			<classpath>
				<pathelement path="${java.classes}" />
				<path refid="build.classpath" />
				<fileset refid="supporting.libraries" />
			</classpath>
		</junit>
	</target>

	<target name="env" description="Show environment properties">
	    <echo message="Key properties; for a complete list, do ant -diagnostics" />
	    <echo message="" />
	    <echo message="OTS directory      ${env.OTS}" />
	    <echo message="HU directory       ${env.HU}" />
	    <echo message="JBOSS_HOME         ${env.JBOSS_HOME}" />
	    <echo message="J2EE_HOME          ${env.J2EE_HOME}" />
	    <echo message="JBOSS_SERVER       ${env.JBOSS_SERVER}" />
	    <echo message="java source        ${java.src} " />
	    <echo message="java classes       ${java.classes} " />
	    <echo message="user home          ${user.home}" />
	    <echo message="Xerces jar         ${xerces.jar}" />
	    <echo message="J2EE jar           ${j2ee.jar}" />
	    <echo message="JAXP jar           ${jaxp.jar}" />
	    <echo message="JSTL core jar      ${jstl_core.jar}" />
	    <echo message="JSTL standard jar  ${jstl_standard.jar}" />
	    <echo message="java version       ${ant.java.version} "/>
	    <echo message="java vendor        ${java.vendor} " />
	</target>

	<target name="-war-include-struts" if="includeStruts">
		<copy file="${struts.jar}"                     todir="war-temp" />
		<copy file="${struts-commons-digester.jar}"    todir="war-temp" />
		<copy file="${struts-commons-logging.jar}"     todir="war-temp" />
		<copy file="${struts-commons-beanutils.jar}"   todir="war-temp" />
		<copy file="${struts-commons-collections.jar}" todir="war-temp" />
		<copy file="${struts-commons-lang.jar}"        todir="war-temp" />
		<copy file="${struts-commons-validator.jar}"   todir="war-temp" />
		<copy file="${struts-jakarta-oro.jar}"         todir="war-temp" />
	</target>

	<target name="-war-include-jstl" if="includeJSTL">
		<copy file="${jstl_core.jar}"                  todir="war-temp" />
		<copy file="${jstl_standard.jar}"              todir="war-temp" />
	</target>
	
	<target name="-war-include-tapestry" if="includeTapestry">
		<copy file="${tapestry.jar}"                  todir="war-temp" />
		<copy file="${tapestry_contrib.jar}"              todir="war-temp" />
	</target>


    <target name="war-nocompile" description="Build the Web Application Archive (war)">
    	<echo>${java.classes}</echo>
		<delete dir="war-temp" quiet="true" />
		<mkdir dir="war-temp" />
		<copy todir="war-temp">
			<mapper type="flatten" />
			<fileset refid="supporting.libraries" />
		</copy>
		<antcall target="-war-include-jstl"/>
		<antcall target="-war-include-struts"/>
		<antcall target="-war-include-tapestry"/>
    	<war warfile="${dist}/${app.name}.war" webxml="${config}/web.xml" compress="false">
    		<webinf dir="${config}" includes="*.tld" />			
    		<webinf dir="${env.OTS}/Tapestry-3.0/lib" includes="*.tld" />
    		<webinf dir="${config}" includes="jboss-web.xml" />
    		<webinf dir="${config}" includes="struts-config.xml" />
    		<metainf dir="${config}" includes="context.xml" />
      		<fileset dir="${web}" includes="**/*" />
			<classes dir="${java.classes}" includes="com/**/*.class" excludes="**/*Test*.*" />
			<lib dir="war-temp" includes="*.jar" />
   		</war>
		<delete dir="war-temp" quiet="true" />
    </target>

    <target name="ear-nocompile" description="Build the Enterprise Application Archive" depends="war-nocompile">
       	<ear earfile="${dist}/${app.name}.ear" appxml="${config}/application.xml" compress="false">
        	<fileset dir="${dist}" includes="*.war" />
        	<fileset dir="${dist}" includes="ejb*.jar" />
        	<metainf dir="${config}" includes="sun-j2ee-ri.xml,jboss-app.xml" />
       	</ear>
  	</target>

	<target name="verify-ear" description="Verify the EAR" depends="ear-nocompile">
		<exec os="Windows 2000,Windows XP" executable="cmd.exe">
			<arg line="/c ${env.J2EE_HOME}/bin/verifier ${dist}/${app.name}.ear" />
		</exec>
		<exec os="Linux,Mac OS X" executable="bash">
			<arg line="${env.J2EE_HOME}/bin/verifier ${dist}/${app.name}.ear" />
		</exec>
	</target>

	<!-- Helper for exec'ing commands -->
	<target name="-exec-cmd">
		<exec os="Windows 2000,Windows XP" executable="cmd.exe">
			<arg line="/c ${cmd}" />
		</exec>
		<exec os="Linux,Mac OS X" executable="bash">
			<arg line="${cmd}" />
		</exec>
	</target>

	<!-- Helper to deploy ${app.name} to JBoss -->
	<target name="-deploy-jboss">
		<copy file="${dist}/${app.name}.ear"
		      todir="${env.JBOSS_HOME}/server/${env.JBOSS_SERVER}/deploy" />
	</target>
	
	<!-- Helper to deploy ${app.name} to Tomcat -->
	<target name="-deploy-tomcat" depends="-undeploy-tomcat" >
        <echo message="Deploying ${app.name} ..."/>
        <tomcat-deploy url="${manager.url}" 
        			   username="${tomcat.admin.username}" 
        			   password="${tomcat.admin.password}" 
        			   path="/${app.name}"
            war="file:${dist}/${app.name}.war"/>
    </target> 
    
    <!-- <target name="-deploy-tomcat">
		<copy file="${dist}/${app.name}.war"
		      todir="${env.CATALINA_HOME}/webapps" />
	</target>  -->
    
    <!-- Helper to ascertain whether app is deployed -->
    <target name="-check-tomcat-app-deployed">
        <condition property="tomcat.app.deployed">
            <http url="${manager.url}/${app.name}" />
        </condition>
    </target>
    
    <target name="-undeploy-tomcat">
	    <trycatch>
	      <try>
	        <tomcat-remove
	          url="${manager.url}"
	          username="${tomcat.admin.username}"
	          password="${tomcat.admin.password}"
              path="/${app.name}"/>
          </try>
          <catch/>
        </trycatch>
		<sleep seconds="1" />
	</target>
    
    <target name="start" description="Start Tomcat application">
        <tomcat-start url="${manager.url}"
            username="${tomcat.username}"
            password="${tomcat.password}"
            path="/${app.name}"/>
    </target>

    <target name="stop" description="Stop Tomcat application">
        <tomcat-stop url="${manager.url}"
            username="${tomcat.username}"
            password="${tomcat.password}"
            path="/${app.name}"/>
    </target>
    

	<target name="deploy-nocompile" description="Deploy ${app.name} to ${deploy.target}" depends="war-nocompile">
		<antcall target="-deploy-${deploy.target}" />
	</target>

	<target name="deploy" description="Deploy ${app.name} to the J2EE RI" depends="compile,deploy-nocompile" />

	<target name="launch" description="Launch web browser pointing to app">
		<exec os="Windows 2000,Windows XP" executable="cmd.exe">
			<arg line="/c start http://${deploy.servername}:${deploy.serverport}/${app.name}/" />
		</exec>
		<exec os="Linux,Mac OS X" executable="bash">
			<arg line='-c "netscape http://${deploy.servername}:${deploy.serverport}/${app.name}/"' />
		</exec>
	</target>

	<target name="deploy-and-launch" description="Deploy and then launch browser pointing to app">
		<antcall target="deploy" />
		<sleep seconds="7" />
		<antcall target="launch" />
	</target>

	<target name="dbsetup" description="Create initial tables and data for the RDB" >
		<sql src="dbsetup.sql" driver="${jdbc_driver}" url="${jdbc_url}" userid="${jdbc_userid}" password="${jdbc_password}" print="true" onerror="continue">
		<classpath>
			<pathelement location="${jdbc.jar}"/>
			<pathelement location="${jdbc_extra.jar}"/>
		</classpath>
		</sql>
	</target>

	<target name="zip-all" description="ZIP entire project to parent directory">
		<zip destfile="../${developer.name}-${assignment.number}-${app.name}.zip" basedir="." excludes="**/*.war,**/*.jar" />
	</target>

	<target name="zip-no-source" description="ZIP entire project to parent directory (without sources)">
		<zip destfile="../${developer.name}-${assignment.number}-${app.name}-no-source.zip" basedir="." excludes="**/*.war,**/*.jar">
			<patternset refid="no-sources" />
		</zip>
	</target>

</project>
