<!-- build.xml for BeerAdvisor, P. Conrad, for CISC474, Spring 2007 -->

<project name="BeerAdvisor" default="menu" basedir=".">

<!-- get the properties and load the environment variables -->

<property file="global.properties" />
<property environment="env"/>

<!-- The default target is a menu of options
     That helps us remember what the possibilities are -->

<target name="menu">
   <echo message="ant clean       (clear out everything but source files)" />
   <echo message="ant compile     (compile the Java source)" />
   <echo message="ant deploy-doc  (deploy the Javadocs)" />
   <echo message="ant doc         (compile the Javadocs)" />
   <echo message="ant test        (run JUnit tests)" />
   <echo message="ant run         (run text version of model)" />
   <echo message="ant create-war  (create a war file)" />
   <echo message="ant deploy-war  (deploy the war file)" />


</target>


<!-- define a custom classpath to use in compiling 
     that includes the servlet spec and junit files -->

<path id="custom-classpath">
    <fileset dir="${tomcat.dir}/common/lib">
        <include name="*.jar" />
    </fileset>
    <fileset dir="${tomcat.dir}/common/endorsed">
        <include name="*.jar" />
    </fileset>
    <fileset dir="${junit.dir}/">
        <include name="${junit.jar}" />
    </fileset>

</path>


<target name="makeBuildDirectory">
    <mkdir dir="${build}"/>
</target>




<target name="run" depends="compile">

  <!-- Note: if you don't use fork="true", and you do a System.exit(0) or 
       System.exit(1) inside the program that is executed, ant will 
       exit also!  Using fork means that a new JVM is started, rather than
       running the program in the same JVM as Ant.   Thus the
       exit() is exiting the forked JVM, not the one running Ant -->

  <echo message='Running with argument "amber"' />
  <java fork="true" classname="edu.udel.cisc474.pconrad.BeerAdvisor.model.BeerExpert">
      <arg value="amber"/>
      
      <classpath>
           <pathelement location="${build}"/>
       </classpath>
   </java>

  <echo message='Running with argument "dark"' />
  <java fork="true" classname="edu.udel.cisc474.pconrad.BeerAdvisor.model.BeerExpert">
      <arg value="dark"/>
      
      <classpath>
           <pathelement location="${build}"/>
       </classpath>
   </java>


</target>


<target name="test" depends="compile">

  <!-- We use haltonfailure no so that we can get a full report
       of all the tests that failed, not just the first test that fails.
 
       After all tests are done, we check the property SomeJunitTestsFailed
       and if it is set, we fail then

  -->

  <junit fork="yes" haltonfailure="no" failureproperty="SomeJUnitTestsFailed">
  
      <!-- The formatter element gives you a report if there is a failure.

           Take out usefile="false" if you want the report stored
           in a file called TEST-testname.txt 

           If you don't use formatter, you probably want printsummary="yes"
           as an attribute on the junit open tag -->
        
      <formatter type="plain" usefile="false" />
      
      <classpath>
        <pathelement location="${build}"/>
      </classpath>

      <test name="edu.udel.cisc474.pconrad.BeerAdvisor.model.BeerExpertTest"/>

  </junit>


  <fail>
     <condition>
         <isset property="SomeJUnitTestsFailed"/>
     </condition>
   </fail>


</target>




<target name="compile" depends="makeBuildDirectory">

    <echo message="Compiling the model classes ... "/>

    <javac debug="on" debuglevel="lines,vars,source"
           srcdir="${src}" destdir="${build}"
           includes="**/model/*.java">
         <compilerarg line="-Xlint:unchecked"/> 
    </javac>
            

    <echo message="Compiling the controller servlet classes ... "/>

    <javac debug="on" debuglevel="lines,vars,source" 
           srcdir="${src}" destdir="${build}"
           includes="**/controller/*.java" >
      <classpath refid="custom-classpath" />
    </javac>
 
 
</target>


<target name="doc">
 <javadoc 
   packagenames="edu.udel.cisc474.pconrad.BeerAdvisor.*"
   sourcepath="${src}"
   destdir="${javadoc.build}"
   author="true" version="true" use="true">
   <classpath refid="custom-classpath" />
 </javadoc>
</target>

<target name="deploy-doc" depends="doc">
   <delete dir="${javadoc.destination}" />

    <!-- copy the generated javadoc to where it should go on the web -->
   
    <copy todir="${javadoc.destination}">
      <fileset dir="${javadoc.build}"/>
    </copy>

    <!-- Change permissions on destination directory, and
         all files and subdirectories under it. -->

    <chmod dir="${javadoc.destination}" perm="755"  type="file"
       includes="**" verbose="true"/>

    <chmod dir="${javadoc.destination}" perm="755" type="dir"
       includes="**" verbose="true"/>


    <echo message="Javadoc deployed to ${javadoc.destination}" />
    <echo message="URL should be: ${javadoc.URL} " />

</target>



<target name="clean">
  <echo message="Deleting and remaking the output directories." />

  <delete dir="${build}" /> 
  <mkdir dir="${build}" /> 

  <delete dir="${dist}" /> 
  <mkdir dir="${dist}" /> 

  <delete dir="${javadoc.build}" /> 
  <mkdir dir="${javadoc.build}" /> 

</target>


<!--  Note: you may need to add   <lib dir="${lib}" /> 
      to the war task in some cases if you want to add libraries into
      your war file from the lib subdirectory (e.g. if you are depending
      on certain jar files that you downloaded into your development
      environment  -->

<target name="create-war" description="creates war file" depends="clean, compile">
  <echo message="Creating ${web-app-name}.war" />

  <war destfile="${dist}/${web-app-name}.war" 
       webxml="${etc}/web.xml">
       <classes dir="${build}" />
       <fileset dir="${web}" />
  </war>
</target>



<target name="deploy-war" depends="create-war">
    <copy todir="${war.destination}">
      <fileset dir="${dist}">
        <include name="${web-app-name}.war" />
      </fileset>
    </copy>
</target>


</project>





