<?xml version="1.0"?>

<project name="LAMS Central" basedir="." default="usage">

	<!-- import properties from the specified file -->
	<property file="build.properties"/>
	<property file="../lams_build/common.properties"/>
	<property file="../lams_build/${osPropertiesName}.properties" />

	<path id="all-libs">
		<fileset dir="${sharedlib}">
			<include name="**/*.jar"/>
		</fileset>
		<fileset dir="${j2eelibs}">
			<include name="**/*.jar"/>
		</fileset>
	</path>
	
	<path id="project.classpath">
		<path refid="all-libs"/>
		<!-- Java CLASSPATH should be added as the last item -->
		<!-- This property is supposed to be set in eclipse  -->
		<pathelement location="${java.class.path}" />
	</path>

	<target name="usage">
		<echo message=""/>
		<echo message="${project} build file"/>
		<echo message="------------------------------------------------------"/>
		<echo message=""/>
		<echo message="Among the available targets are:"/>
		<echo message=""/>
		<echo message="build-war  --> Make war archive for deployment"/>
		<echo message="build-jar  --> Make jar archive for deployment"/>
		<echo message="clean-build  --> Make war archive for deployment"/>
		<echo message=""/>
	</target>

	<target name="init">
	</target>

	<target name="print-classpath">
		<echo message="java.class.path = ${java.class.path}"/>
		<property name="path.string" refid="project.classpath"/>
		<echo message="project.classpath = ${path.string}"/>
	</target>

	
	<!-- ================================================================ -->
	<!-- Preparations									                  -->
	<!-- ================================================================ -->
	<target name="preparedirs">
		<mkdir dir="${build}"/>
		<mkdir dir="${build.lib}"/>
		<mkdir dir="${build.classes.java}"/>
		<mkdir dir="${build.classes.test}"/>
		<mkdir dir="${build.web}"/>				
	</target>


	<target name="clean" depends="preparedirs" description="removes all class files">
		<delete>
			<fileset dir="${build.classes.java}"/>
			<fileset dir="${build.classes.test}"/>
			<fileset dir="${build.lib}"/>
		</delete>
		<mkdir dir="${build.classes.java}"/>	
		<mkdir dir="${build.classes.test}"/>	
		<mkdir dir="${build.lib}"/>
	</target>

	
	<!-- ================================================================ -->
	<!-- Preparations									                  -->
	<!-- ================================================================ -->
	<target name="compile" depends="clean" description="compile java sources">
 	   <!--<property name="cp" refid="project.classpath"/>
	   <echo message="Classpath is ${cp}"/> -->
		<javac srcdir="${src.java.dir}" compiler="modern"
	         	 	destdir="${build.classes.java}" deprecation="on" debug="on">
			<classpath>
				<path refid="project.classpath"/>
			</classpath>
		</javac>
		<!-- Spring files -->
		<copy overwrite="yes" todir="${build.classes.java}/org/lamsfoundation/lams/">
				<fileset dir="${src.java.dir}/org/lamsfoundation/lams/">
						<include name="**/*.xml"/>
						<include name="**/*.properties"/>
				</fileset>
		</copy>
	</target>

	<target name="compile.test" depends="compile">
		<javac destdir="${build.classes.test}" compiler="modern">
			<src path="${src.test.dir}"/>
			<classpath>
				<path refid="project.classpath"/>
			</classpath>
		</javac>
	</target>
	
	<target name="copyContext">
		<copy overwrite="yes" todir="${build.web}">
			<fileset dir="${conf.web.dir}">
				<include name="**/*.xml"/>
				<include name="**/*.tld"/>
				<include name="**/*.tag"/>
			</fileset>
		</copy>
	</target>
	
	<!-- = = = = = = = = = = = = = = = = = = = = = = = =  -->
	<!-- Make the war archive  for deployment             -->
	<!-- = = = = = = = = = = = = = = = = = = = = = = = =  -->
	<target name="build-war" depends="webdoclet,build-jar" description="creates war file">
		<delete file="${build.lib}/${product}.war"/>
		<war warfile="${build.lib}/${product}.war" manifest="${conf.dir}/war/META-INF/MANIFEST.MF"
  				webxml="${conf.webinf.dir}/web.xml">
			<webinf dir="${conf.webinf.dir}">
				<include name="*.xml"/>
				<include name="*.tld"/>
				<include name="*.wsdd"/>
				<include name="conf/**"/>
				<include name="modules/**"/>
				<include name="services/**"/>
				<include name="tags/**"/>
				<include name="*.properties"/>
				<include name="struts/**"/>
				<include name="jstl/**"/>
				<include name="tiles/**"/>
				<exclude name="web.xml"/>
			</webinf>
			<fileset dir="${conf.web.dir}">
				<include name="*"/>
				<include name="css/*"/>
				<include name="includes/**"/>
				<include name="images/**"/>
				<include name="fckeditor/**"/>
				<include name="development/**"/>
				<include name="flashxml/**"/>
				<include name="toolcontent/**"/>
				<include name="**/*.jsp"/>
				<exclude name="classes"/>
			</fileset>
		</war>
	</target>

	<!-- ================================================================ -->
	<!-- Make jar archive for deployment        		    	          -->
	<!-- ================================================================ -->
	<target name="build-jar" depends="compile,copyContext" description="creates jar file">
		<delete file="${build.lib}/${product}.jar"/>
		<jar jarfile="${build.lib}/${product}.jar"  manifest="${conf.dir}/jar/META-INF/MANIFEST.MF">
			<fileset dir="${build.classes.java}"/>
			<fileset dir="${build.classes.test}"/>
		</jar>
		<copy overwrite="yes" todir="${build.lib}/language/org/lamsfoundation/lams/central">
			<fileset dir="${conf.language.dir}">
				<include name="**/*.properties"/>
				<include name="**/*.txt"/>
			</fileset>
		</copy>
	</target>
	<!-- =================================================================== -->
	<!-- Run JUnit Tests                                                     -->
	<!-- =================================================================== -->
	<target name="test" depends="compile.test"> 
			<mkdir dir="${build.report}"/>
			<junit printsummary="yes" haltonerror="no" haltonfailure="no" fork="yes">
				<jvmarg value="-Xms512M"/>
				<jvmarg value="-Xmx1024M"/>
				<formatter type="plain" usefile="false"/>
				<formatter type="xml"/>
				<batchtest todir="${build.report}">
					<fileset dir="${src.test.dir}">
						<include name="**/Test*.java"/>
					</fileset>
				</batchtest>			
				<classpath>
					<pathelement location="${build.classes.java}"/>
					<pathelement location="${build.classes.test}"/>
					<pathelement location="${build.web}"/>
					<path refid="project.classpath"/>
				</classpath>			
			</junit>
	</target>
		
	<target name="test-report" depends="test">
		<mkdir dir="${build.report}/html"/> 
		<junitreport todir="${build.report}"> 
			<fileset dir="${build.report}">
				<include name="TEST-*.xml"/>
			</fileset>
			<report todir="${build.report}/html"/> 
		</junitreport>
	</target>

	<!-- =================================================================== -->
	<!-- Run xdoclet for hibernate                                           -->
	<!-- =================================================================== -->
	<taskdef name="webdoclet" classname="xdoclet.modules.web.WebDocletTask">
	    <classpath refid="all-libs"/>
	</taskdef>

	<target name="webdoclet" depends="preparedirs">
		<!-- dump the classpath to the log for debugging -->
		<pathconvert pathsep="${line.separator}|   |-- "
		             property="echo-classpath"
		             refid="project.classpath">
		</pathconvert>
		<echo message="Classpath for lams-central build ${echo-classpath}"/>
		<!-- end dump -->
		
		<webdoclet destdir="${generated.java}" force="${xdoclet.force}">
	         <fileset dir="${src.java.dir}">
	            <include name="**/*Servlet.java" />
	            <include name="**/*Action.java" />
	            <include name="**/*ActionForm.java" />
	            <include name="**/*Tag.java" />
	         </fileset>

	         <deploymentdescriptor 
				         	servletspec="2.4" 
	         				distributable="false"
	         				destdir="${conf.webinf.dir}"
				         	mergedir="${conf.xdoclet.dir}"
	         				sessiontimeout="${sessiontimeout}"/>
			
	         <strutsconfigxml 
	         				destdir="${conf.struts.dir}"
	         				version="1.2" 
	         				mergedir="${conf.xdoclet.dir}"/>
			 
			 <jsptaglib jspversion="2.0"
						taglibversion="1.0"
					 	shortname="lams"
			 			displayname="LAMSTags"
					 	description="LAMS custom tags"
			 			filename="lams.tld"
					 	validatexml="true"
						destdir="${conf.webinf.dir}"
        				mergedir="${conf.xdoclet.dir}"/>
	     </webdoclet>
	</target>
		
	<!-- deploy-war : for convenience during development. Should be deployed via the master build -->
	<target name="deploy-war" depends="build-war">
		<copy file="${build.lib}/${product}.jar"
				todir="${jboss.deploy}"/>
		<copy file="${build.lib}/${product}.war"
				todir="${jboss.deploy}"/>
		<copy overwrite="yes" todir="${jboss.deploy}/lams-dictionary.jar/org/lamsfoundation/lams/central">
			<fileset dir="${conf.language.dir}">
				<include name="**/*.properties"/>
			</fileset>
		</copy>
	</target>
	
	<!-- deploy-war : for convenience during development. Should be deployed via the master build -->
	<target name="deploy-jar" depends="build-jar">
		<copy file="${build.lib}/${product}.jar"
				todir="${jboss.deploy}"/>
		<copy overwrite="yes" todir="${jboss.deploy}/lams-dictionary.jar/org/lamsfoundation/lams/central">
			<fileset dir="${conf.language.dir}">
				<include name="**/*.properties"/>
			</fileset>
		</copy>
	</target>

	
	<!-- Debug target: Explode tool to ear folder for debug use  -->
	<target name="delete-exploded-folder" description="delete old tool exploded tool folder">
		<!-- delete old war folder tree -->
		<delete quiet="true" includeemptydirs="true">
			<fileset dir="${jboss.deploy}/${product}.war">
			    <include name="**"/>
			</fileset>
		</delete>
		<!-- if use explode tool target when jboss is running, explode may be failed and left the the temp folder. Here try to delete the temp folder -->
		<delete quiet="true" includeemptydirs="true">
			<fileset dir="${jboss.deploy}/temp-${product}.war">
			    <include name="**"/>
			</fileset>
		</delete>
	</target>
	
	<target name="explode-war" depends="delete-exploded-folder,deploy-war" description="explode tool jar and war package to a war folder">
		<!-- rename war file to avoid name conflict -->
		<move tofile="${jboss.deploy}/temp-${product}.war" file="${jboss.deploy}/${product}.war"/>
		<!-- explode the ear -->
		<!-- Don't explode jar file, becuase classes in jar will conflict with those same classes files in war -->
		<!--<unjar src="${jboss.deploy}/${product}.jar" dest="${jboss.deploy}/${product}.war" />-->
		<unjar src="${jboss.deploy}/temp-${product}.war" dest="${jboss.deploy}/${product}.war" />
		<!-- delete war and jar files -->
		<delete>
			<fileset dir="${jboss.deploy}">
			    <include name="temp-${product}.war"/>
			 <!--   <include name="${product}.jar"/> -->
			</fileset>
		</delete>
	
	</target>
	
	<!-- ================================= -->
	<!--  target: synchronize-deploy          -->            
	<!--  ================================= -->
	<target name="synchronize-deploy" description="copy non-java file into deploy folder - war must be expanded. Do not overwrite the web.xml">
		<copy verbose="yes" todir="${jboss.deploy}/${product}.war">
			<fileset dir="${conf.web.dir}">
				<include name="**/*.*" />
				<exclude name="WEB-INF/web.xml"/>
			</fileset>
		</copy>
	</target>

</project>

