Chapter 6: Building J2EE Applications with Ant


This chapter explains techniques for using Ant to build and deploy J2EE applications. The first example we use is a continuation of the Hello World application from Chapter 5. The Hello World example in this chapter includes an applet, a Web application, an application, enterprise beans, support libraries, and other components . This may be the only Hello World example that has an applet, servlet, session bean, entity bean, and JSP and attempts to be Model 2.

You're probably thinking, Why should I implement the most complex Hello World application in the world? This example, although a bit of overkill to say "Hello World," is as simple as possible while demonstrating how to build and deploy a J2EE application and its components with Ant. By working through this example, you will understand how to use Ant to build these different types of components and applications, and how to combine them by nesting buildfiles . In the last section of this chapter, we use these techniques to begin implementing a more realistic application: the sample pet store introduced in Chapter 5. We will build on the pet store application and refactor it at the end of every subsequent chapter in this section of the book. The remaining chapters in this part of the book will build on the pet store case study (see Appendix A for a full description and the complete code listing).

Hello World

The Model 2 HelloWorld example for this chapter is the simplest example of Model 2 architecture ”also known as Model-View-Controller (MVC) ”for JSP servlets. In this example, the applet and JSP are the view; the servlet and enterprise session bean are the controller; and the object model is a Java class and later an entity bean.

The GreetingFactory from Chapter 5 is set up so it talks to an enterprise session bean that in turn talks to an enterprise entity bean. The Web application buildfile is set up so that if we add one property file, it can talk to either the enterprise beans or to the local implementation in the original model library (common code) defined in Chapter 5.

The source code for the Model 2 Hello World example is divided into several directories for each component, and each directory has its own Ant buildfile. A master Ant buildfile in the root directory calls the other buildfiles. The directory structure is as follows :

 Model 2 Hello World root +---Model +---EJBeans +---Application +---Applet +---WebApplication 

The Model directory holds the common code. The EJBeans directory holds the enterprise beans code and the deployment descriptor files. The Application directory holds the Java application code including the manifest file that marks the deployment JAR as executable. The Applet directory holds the applet code. The WebApplication directory holds HTML files, deployment descriptors, JSP files, and servlet Java source.

Because each component has its own set of deployment descriptors and configuration files, it makes sense to separate components into their own directories; this practice also makes it easier to reuse the components in other projects and applications. Each directory has its own Ant buildfile, which knows how to compile the components, package the binaries, and include the requisite configuration files (deployment descriptors and manifest files).

In the next section, we cover the Web application buildfile ”the heart of this example.




Professional Java Tools for Extreme Programming
Professional Java Tools for Extreme Programming: Ant, XDoclet, JUnit, Cactus, and Maven (Programmer to Programmer)
ISBN: 0764556177
EAN: 2147483647
Year: 2003
Pages: 228

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