|
Recipe 1.7. Using Ant to Build and DeployProblemYou want to be able to build and deploy your Struts web application in a repeatable and portable manner. SolutionCreate an Ant (http://ant.apache.org) build script and use Ant (or your IDE's Ant integration) to compile, test, package, and deploy your application. Example 1-8 is a boilerplate Ant build file that can compile, build, and deploy a Struts application. Example 1-8. Boilerplate Ant build file<project name="jsc-ch01-r02" default="dist" basedir="."> <description> Jakarta Struts Cookbook - Ant Template </description> <!-- Enable access to environment variables --> <property environment="env"/> <!-- Set to use JDK 1.4 --> <property name="build.compiler" value="javac1.4"/> <!-- set global properties for this build --> <property name="src.dir" location="src"/> <property name="build.dir" location="build"/> <property name="dist.dir" location="dist"/> <property name="server.dir" location="${env.CATALINA_HOME}"/> <property name="servlet.jar" location="${server.dir}/common/lib/servlet-api.jar"/> <property name="jsp.jar" location="${server.dir}/common/lib/jsp-api.jar"/> <property name="struts.dist.dir" location="c:/jakarta-struts-1.1/lib"/> <!-- Struts --> <fileset dir="${struts.dist.dir}"> <include name="**/*.jar"/> </fileset> <path > <fileset ref/> </path> <path > <pathelement location="${servlet.jar}"/> <pathelement location="${jsp.jar}"/> <path ref/> </path> <!-- Deployment Properties --> <property name="deploy.dir" location="${server.dir}/webapps"/> <target name="clean" description="clean up" > <!-- Delete the ${build.dir} and ${dist.dir} directory trees --> <delete dir="${build.dir}"/> <delete dir="${dist.dir}"/> </target> <target name="init"> <!-- Create the build directory structure used by compile --> <mkdir dir="${build.dir}"/> </target> <target name="compile" depends="init" description="compile the source " > <!-- Compile the java code from ${src.dir} into ${build.dir} --> <javac srcdir="${src.dir}" destdir="${build.dir}" debug="on"> <classpath> <path ref/> </classpath> </javac> <copy todir="${build.dir}"> <fileset dir="${src.dir}"> <include name="**/*.properties"/> </fileset> </copy> </target> <target name="dist" depends="compile" description="generate the distribution" > <!-- Create the distribution directory --> <mkdir dir="${dist.dir}"/> <!-- Copy the build dir to WEB-INF/classes --> <mkdir dir="web/WEB-INF/classes"/> <copy todir="web/WEB-INF/classes"> <fileset dir="${build.dir}"/> </copy> <!-- Put everything in ${build} into the war file --> <war destfile="${dist.dir}/${ant.project.name}.war" webxml="web/WEB-INF/web.xml"> <fileset dir="web" excludes="**/web.xml"/> <webinf dir="web/WEB-INF"> <include name="*.xml"/> <exclude name="web.xml"/> </webinf> <lib dir="web/WEB-INF/lib"> <include name="${struts.dist.dir}/**/*.jar"/> <include name="${struts.dist.dir}/**/*.tld"/> </lib> <classes dir="build"/> </war> </target> <!-- Deploy the application by copying it to the deployment directory --> <target name="deploy" depends="dist" description="deploy to server" > <unjar src="/books/3/113/1/html/2/${dist.dir}/${ant.project.name}.war" dest="${deploy.dir}/${ant.project.name}"/> </target> </project> DiscussionThe build file displayed in the Solution can be used with minor modifications for most Struts-based web applications. You should change the value for the name attribute of the project element to the name of your application. This project name will be used as the name of the WAR file that's created, as well as the name of the folder to which the application will be deployed. In addition, you should set the struts.dist.dir property to the lib directory for your particular installation of the Struts distribution. An Ant-based build will allow you to perform various development tasks:
By using a build script to create the WAR file, you can structure the physical location of your code as well as the Struts distribution however you see fit. Then you can use the Ant script to pull it all together. See AlsoThe main web site for Ant can be found at http://ant.apache.org. Also, Ant: The Definitive Guide by Jesse E. Tilly and Eric M. Burke (O'Reilly) is a great reference to use when working with Ant. Recipe 1.8 shows the use of the XDoclet tool for the generation of Struts-related files. |
|