JaVa
   

The Pet Store Case Study

The Web app for the pet store baseline is much like the one in the Hello World app. For simplicity and ease of development, most of the presentation tier is implemented in JSP. We added a nonsensical servlet just for demonstration purposes. Note that in the real world, we use Struts, a framework from Apache, and we have many Java classes to deploy—we use Struts in several examples later in this tutorial. You may recall from the last case study that the main project buildfile for its build and clean target calls the webapp subproject buildfile's clean and deploy targets respectively. The webapp subproject buildfile's deploy target is defined as follows:

 <target depends="package">
 <copy file="${dist}/${pet_war}" todir="${deploy_resin}" />
 <copy file="${dist}/${pet_war}" todir="${deploy_tomcat}" />
 </target>


The deploy target copies the WAR file specified by ${pet_war} to both the Resin and Tomcat deployment directories. (Resin and Tomcat are both J2EE Web app servers.) The deploy target depends on the package target. The package target creates the WAR file that is deployed to Resin and Tomcat, which is defined as follows:

 <target depends="compile">
 <mkdir dir="${meta}" />
 <war warfile="${dist}/${pet_war}" webxml="${meta}/web.xml">
 <!-- Include the html and jsp files. Put the classes from the build into the classes directory of the war. Exclude web.xml file and WEB-INF directory.
 /-->
 <fileset dir="./public-html" >
 <exclude name="WEB-INF" />
 <exclude name="web.xml"/>
 </fileset>
 <classes dir="${build}" />
 <lib dir="${lib}" />
 </war>
 </target>


As you will notice, the package target is much like the one in the Hello World Web app sample. The package target uses the war task to create a WAR file. The WAR file includes all the libraries in the lib (${lib})—namely, the petmodel.jar file created in the model subproject. It also includes the web.xml Web app deployment descriptor using the "webxml" attribute. The following listing contains the complete Web app.

<project default="all" >
 <target unless="setProps" description="setup the properites.">
 <property value="/tmp/petstore" /> </target>
 <target depends="setProps" description="initialize the properties.">
 <tstamp/>
 <property name="local_outdir" value="${outdir}/webapps" />
 <property value="${outdir}/lib" />
 <property value="${outdir}/dist" />
 <property value="${local_outdir}/webclasses" />
 <property value="public-html/WEB-INF" />
 <property name="deploy_resin" value="/resin/webapps" />
 <property name="deploy_tomcat" value="/tomcat/webapps" />
 <property value="pet" />
 <property name="pet_war" value="${appstub}.war" />
 <property name="build_lib" value="./../lib" />
 </target>
 <target name="clean_deploy" >
 <delete file="${deploy_resin}/${pet_war}" />
 <delete dir="${deploy_resin}/${appstub}" />
 <delete file="${deploy_tomcat}/${pet_war}" />
 <delete dir="${deploy_tomcat}/${appstub}" />
 </target>
 <target depends="init,clean_deploy" description="clean up the output directories.">
 <delete dir="${local_outdir}" />
 <delete file="${dist}/${pet_war}" />
 </target>
 <target depends="init" description="prepare the output directory.">
 <mkdir dir="${build}" />
 <mkdir dir="${dist}" />
 </target>
 <target depends="prepare" description="compile the Java source.">
 <javac srcdir="./java" destdir="${build}">
 <classpath >
 <fileset dir="${lib}">
 <include name="**/*.jar"/>
 </fileset>
 <fileset dir="${build_lib}">
 <include name="**/*.jar"/>
 </fileset>
 </classpath>
 </javac>
 </target>
 <target depends="compile">
 <mkdir dir="${meta}" />
 <war warfile="${dist}/${pet_war}" webxml="${meta}/web.xml">
 <!-- Include the html and jsp files. Put the classes from the build into the classes directory of the war. Exclude web.xml file and WEB-INF directory.
 /-->
 <fileset dir="./public-html" >
 <exclude name="WEB-INF" />
 <exclude name="web.xml"/>
 </fileset>
 <classes dir="${build}" />
 <lib dir="${lib}" />
 </war>
 </target>
 <target depends="package">
 <copy file="${dist}/${pet_war}" todir="${deploy_resin}" />
 <copy file="${dist}/${pet_war}" todir="${deploy_tomcat}" />
 </target>
 <target depends="clean,deploy" description="perform all targets."/> </project>


As you can see, the standard targets are present in this buildfile as they are in the Hello World example, the model subproject, and the rest of the buildfiles in this tutorial. This makes it easy to tell what the buildfiles are doing at a glance. You also may notice that the optional "description" attribute is set for each target. This also helps to document your targets and can be shown when running Ant.

The next case study will include the Test subproject buildfile using junit and the junitreport tasks. Then, it will add EJBeans support with a new subproject, and rerun the tests to ensure that the refactored CategorySystem still functions.


JaVa
   
Comments