Building J2EE apps with Ant

This chapter explains techniques for using Ant to build and deploy J2EE apps. The first example we use is a continuation of the Hello World app from . The Hello World example in this chapter includes an applet, a Web app, an app, 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 app 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 app 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 apps, 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 app: the sample pet store introduced in . We will build on the pet store app and refactor it at the end of every subsequent chapter in this section of the tutorial. The remaining chapters in this part of the tutorial 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 is set up so it talks to an enterprise session bean that in turn talks to an enterprise entity bean. The Web app 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 . 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

The Model directory holds the common code. The EJBeans directory holds the enterprise beans code and the deployment descriptor files. The app directory holds the Java app code including the manifest file that marks the deployment JAR as executable. The Applet directory holds the applet code. The Webapp 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 apps. 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 app buildfile—the heart of this example.