Build Management

I l @ ve RuBoard

You're going to be rebuilding and deploying the application frequently during the development of this application, so it behooves you to make that process as easy as possible.

Before you do, you should review the hierarchy of your various directories. On the source side, you have a top-level directory called bfg (Books for Geeks), which has the following structure:

 bfg/ ->   build.xml   javadoc/ ->   jsp/ ->     index.jsp   props/ ->     com/ ->       bfg/ ->         customer/ ->           SQLQueries.properties   src/ ->     bfg/ ->       customer/ ->         Customer.java 

build.xml is the build script that you'll be editing. javadoc is a placeholder for the javadoc that you will eventually be producing. jsp holds (you guessed it) the jsp source. Props will store any property files that you create, and src holds the Java sources.

When you deploy the application, you want all the properties and Java class files to be jarred up into a single jar in the Tomcat lib directory. In fact, they should all reside in the lib directory in the application, which, in this case, is C:\TOMCAT\WEBAPPS\BFG\WEB-INF\LIB . When you followed the instructions to install the sample application (see Appendix C,"A Books for Geeks Quickstart"), you added this directory to the Tomcat configuration file.

Using Ant to do Automated Builds

A few years ago, an automated build process would have meant putting together a set of GNUmake scripts to compile and distribute the application, restart servers, and otherwise automate the tasks that you need to take care of. But, as anyone who has ever used gnumake can attest, it is more than a bit cryptic to work with. A misplaced space can break the entire makefile.

NOTE

GNUMake is the Free Software Foundation replacement for the standard UNIX make utility, which allows a skilled makefile author to compile, link, or install hundreds of files with a single command.


Fortunately, there is a new tool available that uses XML syntax to reduce the chance of errors, and it is even more powerful than make. That tool is ANT.

Detailed instructions for installing ANT and a brief tutorial can be found in Appendix A,"Getting and Installing JDK, Ant, and Tomcat"; however, the important points are reviewed here.

Ant uses an XML build.xml file to describe build tasks to be performed. As with make, you can describe different "targets," allowing yourself, for example, to have a compilation target, a distribution target, a javadoc target, and so on. The invocation syntax is straightforward in all cases:

 ANT <targetname> 

For example, you would use ant dist or ant compile .

Listing 7.1 is a very basic ANT script that you'll use to create and administer the BFG application.

Listing 7.1 build.xml
 <project name="BfgWebsite" default="dist" basedir=".">   <property name="tomcatdir" value="/tomcat"/>   <property name="appdir" value="${tomcatdir} /webapps/bfg"/>   <property name="jarfile" value="bfgclasses.jar"/>   <target name="init">     <tstamp/>   </target>   <target name="compile" depends="init">      <javac srcdir="src">        <classpath>          <pathelement path="${classpath} "/>          <fileset dir="${tomcatdir} \lib">            <include name="**/*.jar"/>          </fileset>        </classpath>      </javac>   </target>   <target name="dist" depends="compile">      <mkdir dir="${appdir} /WEB-INF/lib"/>      <jar jarfile="${appdir} /WEB-INF/lib/${jarfile}">         <fileset dir="src" includes="**/*.class"/>         <fileset dir="props" includes="**/*.properties"/>       </jar>      <mkdir dir="${appdir} /jsp"/>      <copy todir="${appdir} /jsp">         <fileset dir="jsp"/>      </copy>   </target>   <target name="javadoc">      <mkdir dir="javadoc"/>      <javadoc sourcepath="src" destdir="javadoc" author="true"               version="true" use="true" packagenames="com.bfg.*">        <classpath>          <pathelement path="${classpath} "/>          <fileset dir="${tomcatdir} \lib">            <include name="**/*.jar"/>          </fileset>        </classpath>      </javadoc>   </target>   <target name="start">      <exec dir="${tomcatdir} /bin" executable="startup.bat"            os="Windows 2000" vmlauncher="false">      </exec>   </target>   <target name="stop">      <exec dir="${tomcatdir} /bin" executable="shutdown.bat"            os="Windows 2000" vmlauncher="false">      </exec>   </target>   <target name="restart" depends="stop,start">   </target> </project> 

After initializing a few properties that you will use for the script (the location of Tomcat and the name of the jar file that you will be creating in the Tomcat library directory), you tell ant to create a timestamp whenever you ask for any target that depends on initialization (basically, compiling).

The javac directive tells Ant to compile any out-of-date Java source files that it finds under the directory specified in the srcdir portion of the directive (in this case, ., the current directory).

The dist target asks Ant to jar up any .class or .property files that it finds under the basedir and to place them where specified.

Finally, create a few targets to stop, start, and restart the Tomcat executable. This saves having to change directories every time you want to restart Tomcat.

As simple as this script is, it's all that is needed to manage the entire project. In a more complex project, you might include directives to check files automatically out of a source repository system such as CVS, to run automated regression testing, or to keep several servers synchronized when you deploy software to the master. ANT includes dozens of premade directives that will handle many of these tasks for you.

I l @ ve RuBoard


MySQL and JSP Web Applications. Data-Driven Programming Using Tomcat and MySQL
MySQL and JSP Web Applications: Data-Driven Programming Using Tomcat and MySQL
ISBN: 0672323095
EAN: 2147483647
Year: 2002
Pages: 203
Authors: James Turner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net