Organizing Your Eclipse Web Tools Platform Development Project
- All right, guys! It's time to clean up this town!
In this book we describe how to build applications that are defined by the J2EE specification. When you build an application, you create one or more projects that correspond to J2EE modules. You also use these same projects to organize your development work; that is, you use these projects
- to manage the source code and files that make up the application,
- to divide the work between the teams, and
- to set up an automated process that builds the application, runs tests, and creates project reports.
This chapter starts with a basic description of the types of applications and projects that are supported in WTP. We will show you how to create different kinds of projects to build applications.
In the second part of the chapter, we will describe some of the advanced project features that are available with WTP. There is very little available in terms of standards to guide you in the organization of project artifacts and source code for Web projects. Project best practices achieve a balance between the concerns that drive a particular development project:
- How many teams and developers are there?
- What are the subsystems?
- What components are tested, and how are they tested?
- Who builds the code?
- How is it integrated?
- How is it released?
Naturally, each concern is a different dimension of the project. We will use advanced WTP features to create project templates and apply best practices that are helpful to organize your development work. We use the generic term Web project to describe the WTP project types that are provided for J2EE development.
Web Project Types and J2EE Applications
A project is used to develop modules such as J2EE Web applications and EJBs. Typically, each module is a project, but this is not a strict requirement (see Figure 6.1).
Figure 6.1 J2EE Applications and Web Projects
For example, in a complete J2EE enterprise application, one project might consist of a Web application module for the presentation logic while another would be used to develop the EJB module for the business components. In this case, the complete application consists of three projects for the modules: one for the enterprise application, one for the Web application, and one for the EJBs. It is also possible to split the development of a single module into multiple projects. For example, a basic module like a Web application might be built from utility modules built in other projects. You will learn how to organize your projects and modules using similar patterns later in this chapter.
Projects organize your source code and modules. WTP provides Web projects that are sophisticated Eclipse projects that know about J2EE artifacts. In addition to having basic Java project capabilities, a Web project can be used to organize J2EE artifacts into buildable, reusable units (see Figure 6.2).
Figure 6.2 Web Projects
An Eclipse simple project (or general project) provides the basic infrastructure to organize and build resources. The structure of a general project is very open; resources such as files and directories can be organized in any arbitrary form that makes sense for a particular purpose.
A JDT Java project contains Java elements such as packages, types, methods, fields, and property files for creating Java programs. A Java project knows how to build and run Java programs. Each Java project has a Java builder that can incrementally compile Java source files as they are edited.
You can change the properties of a Java project, such as the Java build path. The build path is the classpath that is used for building the project. There are alternative ways of structuring the sources in a Java project; examples include using a single source folder that is the project root or multiple source folders for organizing complex Java projects.
A WTP Web project has more than just Java code. It contains sources that are used to build Web applications, EJBs, and enterprise applications. A Web application can be as simple as a bunch of HTML files, or it can have servlets, JSPs, tag libraries, and Web services. These artifacts make the Web application. A Web project knows how to build, publish, and run J2EE modules and artifacts on application servers.
Web projects have builders, validators, and code generators. Builders produce standard publishable modules from complex development layouts. Validators help identify and catch coding errors at development time. J2EE validators are very valuable, because the sooner you find a problem the easier it is to fix. In J2EE, there are many deployment descriptors that have references to Java code and each other. These are interrelated in complex ways. Failure to catch a problem at development time could lead to a runtime error that might be very difficult to diagnose and fix. Generators create components from annotations in source code (for example, using XDoclet or JSR 175).
The output of the development activities are discrete J2EE components (EJBs, servlets, application clients), which are packaged with component-level deployment descriptors and assembled into J2EE modules. Web application modules, EJB modules, enterprise application modules, and Java 2 Connector Architecture (J2CA) resource modules are typical J2EE modules. A module contains code, resources, and deployment descriptors. A J2EE module forms a stand-alone unit, which can be deployed and run on a J2EE application server. Figure 6.3 provides an overview of the J2EE structure associated with common J2EE modules, such as Web, EJB, and EAR, as described by the specification.
Figure 6.3 J2EE Modules
WTP provides projects and wizards to help you get started quickly with different types of Web and J2EE applications. You can use these wizards to create most standard Web and J2EE artifacts. Additional tools will help you create, build, validate, and run your applications on servers.
To get started, we will review the steps involved in creating different types of applications. The simple steps provided in this section will help you acquire the skills you will need to work with the examples in this book. More specifically, you will learn how to create these types of projects:
- Dynamic Web project, where the output artifact is a WAR file
- EJB project, where the output artifact is an EJB JAR file
- EJB client project, where the output artifact is a JAR file that contains client-side classes for accessing an EJB module
- Enterprise application project, where the output artifact is an EAR file containing Web, EJB, and other modules
Creating Web Applications
To build a Web application you need a project that contains a Web module. There are two types of Web projects: static and dynamic.
Static Web projects contain resources that provide static content. You can use a static Web project to develop Web applications that contain many of the standard Web resources, such as HTML, images, CSS, and XML, and test them using a Web browser. These projects can be deployed to a conventional Web server, such as the Apache HTTP Server, that has no J2EE capabilities.
Dynamic Web projects are for J2EE Web applications that contain servlets, JSPs, and filters, in addition to static content. A dynamic Web project can be used as a stand-alone Web application, or it can be combined with other modules to create a J2EE enterprise application.
The J2EE specification defines a standard for Web application directory structure. It specifies the location of static Web files, JSPs, Java class files, Java libraries, deployment descriptors, and supporting metadata. The default dynamic Web project layout resembles the structure of a J2EE Web application module. In the workbench, you can use the New Web Project wizard to create a new Web project. WTP has support for other types of project layouts and can automatically build a J2EE Web application archive (WAR) structure defined by the standard.
When you want to create a dynamic Web project, you will typically do the following:
- Invoke the Dynamic Web Project wizard.
- Provide parameters such as project name and locations for Web artifacts.
- Choose a target runtime.
- Choose project facets.
You can try these steps by repeating the following:
- Switch to the J2EE perspective. In the Project Explorer view, right click, and invoke the New Dynamic Web Project menu item (see Figure 6.4).
Figure 6.4 Select Wizard
Figure 6.5 New Dynamic Web Project
Enter LeaguePlanetWebProject for the project name. A dynamic Web project contains J2EE components such as JSPs and servlets. It is necessary for J2EE APIs to be a part of the project classpath. This is done for you automatically when you associate a J2EE server runtime with the project. The runtime provides a set of libraries that will also contain JARs such as the servlet.jar. If you switch the runtime at a later time, the classpath is also updated. If your prefer not to use a runtime to provide these libraries, you can create a folder that contains the J2EE libraries and point to it as your runtime library. However, this method will require you to obtain appropriate libraries for the J2EE APIs from
Assuming you have defined a server runtime such as Tomcat, select it as the target runtime. We will revisit servers and runtimes in other chapters.
Configurations allow you to choose a set of project facets for common styles of Web projects. For example, if you choose the WebDoclet configuration, WTP will set up the project to enable XDoclet.
Click the Next button. The Project Facets selection page is displayed (see Figure 6.6).
Figure 6.6 Select Project Facets
- A project facet describes some runtime aspect of the Web module. For Tomcat 5.0, you can specify the J2EE version, the Java version, and, optionally, the XDoclet version. Each server defines a set of supported facets and their allowed values. WTP configures the Web module and sets up the classpath for the project so that it matches the specified facets. Accept the defaults here and click the Next button. The Web Module page is displayed (see Figure 6.7).
Figure 6.7 Web Module
- The Web Module page lets you specify its context root name and the directories for its Web and Java resources. The context root is the name that appears in the URL for the Web application. Specify LeaguePlanetWebProject as the context root and accept the defaults for the directory names. Click Finish. WTP creates the project and populates it with configuration files such as the J2EE Web deployment descriptor, web.xml (see Figure 6.8).
Figure 6.8 Dynamic Web Project—LeaguePlanetWebProject
You have now created a dynamic Web project named LeaguePlanetWebProject and targeted it to Tomcat.
The Dynamic Web Project wizard creates folders and files under the project (see Figure 6.9). Open the project you have just created and browse its contents. For example, the WebContent folder contains a special folder named WEB-INF, which holds items that are defined by the J2EE specification and are not accessible by a Web browser. The WEB-INF/classes folder is where compiled Java code goes. It also contains a special file, web.xml, which is the J2EE Web deployment descriptor.
The WebContent folder contains Web resources such as JSP and HTML files, and other types of supporting resources (see Figure 6.9). The contents of WebContent will be accessible from the Web application context root.
Figure 6.9 Elements of a Dynamic Web Project
The following default elements are created with a dynamic Web project:
- WebContent/WEB-INF/web.xml: This is the Web deployment descriptor.
- src: This is the Java source code for classes, beans, and servlets. The publisher will copy the compiled class files into the WEB-INF/classes folder of the final application.
- WebContent: This is the Web application root. All Web artifacts placed in this folder will be available to the client. The publisher will copy the complete contents of this folder into the root of the final WAR file. It is possible to choose a different name for the WebContent folder or rename it.
- WebContent/WEB-INF/classes: Sometimes code and libraries will be delivered to you in the form of class files (in comparison to those that are provided to you as JAR files, which you would put into the WEB-IF/lib folder). To add them to the classpath of the final Web application, you can place them in this folder.
- WebContent/WEB-INF/lib: We will place all libraries that are provided to use in the form of JAR files here. They will be added to the build path of the project. The publisher will copy them into the WAR file, and they will be available to the class loader of the Web application.
A dynamic Web project can publish its contents as a Java Web application archive (WAR) file (see Figure 6.10). Publishers assemble the artifacts in a Web project, such as Java sources; Web content, such as JSPs, HTML, and images; and metadata, such as Web deployment descriptors, in a form that can run on a J2EE application server.
Figure 6.10 Publisher
WTP wizards simplify the tasks involved in creating J2EE modules. We have just shown how to create a Web module. WTP online documentation at
provides detailed information about these wizards and the project structure. The process of creating an EJB application is equally simple. The next section describes how to create an EJB project that contains an EJB module.
Creating EJB Applications
An EJB project contains an EJB module. This project can be used to assemble one or more enterprise beans in a single deployable unit. EJBs are deployed in a standard Java archive (JAR) file. An EJB project can be used to build stand-alone components, or it can be combined with other modules in a J2EE enterprise application (EAR).
Recall the structure of an EJB module (see Figure 6.3 earlier). EJB modules have a simple structure that contains EJB classes and deployment descriptors. In the workbench, we can use the New EJB Project wizard to create a new EJB project with an EJB module in it.
When you want to create an EJB project, you will typically do the following:
- Switch to the J2EE perspective. In the Project Explorer view, right click, and invoke the New EJB Project menu item (see Figure 6.11).
Figure 6.11 Select Wizard
Figure 6.12 New EJB Project
- Project facets describe aspects of J2EE modules (see Figure 6.13). For an EJB module, you can specify the J2EE version, the Java version, and, optionally, the XDoclet version. Each server defines a set of supported facets and their allowed values. For example, you will not be able to set an EJB facet using a Tomcat server because it does not have an EJB container. WTP configures the EJB module and sets up the classpath for the project so that it matches the specified facets. Here, you will use XDoclet to develop EJBs. Add the XDoclet facet by checking it. Accept the defaults for the EJB and Java facets and click the Next button to proceed to the EJB module settings.
Figure 6.13 EJB Project Facets
- The EJB Module page (see Figure 6.14) lets you specify the directory for Java resources. Optionally, you can create a Java utility module that will contain EJB classes and interfaces, which will be required by EJB clients. Click Finish.
Figure 6.14 EJB Module
- WTP creates the EJB project and populates it with configuration files such as the EJB deployment descriptor, ejb-jar.xml (see Figure 6.15).
Figure 6.15 Project Explorer—EJB Project
You may notice some errors in the new EJB project. For example, if your EJB project does not contain any EJB components, this is considered an error according to the J2EE specification. If you chose the XDoclet facet and an XDoclet runtime is not yet configured, this will show up in the problem markers. These errors are normal and will be removed when you fix the preferences and add EJBs to the project.
The ejbModule folder contains Java and EJB resources such as the deployment descriptor (see Figure 6.16).
Figure 6.16 Elements of an EJB Project
Similar to Web application modules, an EJB project has a publisher for EJB applications (see Figure 6.17). This publisher creates a deployable EJB module from the contents of the project with all the classes and deployment descriptors.
Figure 6.17 EJB Publisher
This completes the process of creating an EJB project. The next section describes how to create an enterprise application project that can combine EJB and Web modules in a J2EE Enterprise Application (EAR) module.
Creating Enterprise Applications
The most interesting J2EE enterprise applications have more than one module. They have several Web applications and EJB modules. The J2EE specification provides a basic application packaging structure called an enterprise application. Enterprise application archives are packaged as Java archives with the .ear suffix. Therefore, they are also known as EARs. An EAR can contain one or more
- EJB modules
- Web application modules
- J2CA resource adapter modules
- Application client modules
An enterprise application project contains the hierarchy of resources that are required to deploy these modules as a J2EE enterprise application.
An enterprise application module contains a set of references to the other J2EE modules that are combined to compose an EAR. In addition to the modules, an enterprise application module also includes a deployment descriptor, application.xml.
Publishers for enterprise application projects consume the output of the publishers from their component modules (see Figure 6.18). For example, the builder of an EAR that contains a Web application module and an EJB module waits until the builder for the Web and EJB projects creates the deployable structures for these modules, and then it assembles these artifacts in the EAR.
Figure 6.18 EAR Publisher
WTP has wizards and tools to create and edit EARs. They are described in the following use cases.
Create a New Web or EJB Module in an EAR
When a new J2EE module project is created, such as a dynamic Web project or an EJB project, it can be associated with an enterprise application project (see Figure 6.19). The project wizards let you specify a new or existing enterprise application. You can also choose the project in which you would create the enterprise application module. Finally, the EAR is updated to include the new J2EE module in it.
Figure 6.19 Adding a Module to an EAR
Adding Existing Web and EJB Modules to an EAR
In the second scenario there are existing J2EE modules, which are to be added to a new enterprise application. You create a new EAR project and add your existing modules to it. The Enterprise Application wizard creates a new project and allows you to choose the modules to be included in it.
When you want to create an EAR project, you will typically do the following:
- Switch to the J2EE perspective. In the Project Explorer view, right click, and invoke the New Enterprise Application Project menu item (see Figure 6.20).
Figure 6.20 Select Wizard
- Click Next. The New Enterprise Application Project wizard opens (see Figure 6.21).
Figure 6.21 New Ear Project
- Enter LeaguePlanetEar for the Project name. Click the Next button to proceed to the Project Facets selection page.
- Project facets describe aspects of enterprise applications (see Figure 6.22). For the EAR module, there is only the EAR facet. Each server defines a set of supported facets and their allowed values. For example, you will not be able to set an EAR facet using a Tomcat server because it does not support EARs. Click the Next button to proceed to the EAR module settings.
Figure 6.22 EAR Project Facets
- The J2EE Module page (see Figure 6.23) lets you select the modules that will be included in the application. Select the LeaguePlanetEJB and LeaguePlanetWebProject modules. Note that you can also make the wizard generate new empty modules by clicking the New Modules button. Click Finish.
Figure 6.23 J2EE Modules
- WTP creates the EAR project and its deployment descriptor, application.xml (see Figure 6.24).
Figure 6.24 Project Explorer—EAR Project
In the final scenario, you modify the modules in an EAR. You can add new modules to an EAR or remove existing ones by using the J2EE Module Dependencies property page.
When you want to modify an EAR project, you will typically do the following: In the Project Explorer, highlight the enterprise application LeaguePlanetEar, right click, and select Properties. As Figure 6.25 shows, you can then choose the modules to be included in the EAR.
Figure 6.25 J2EE Module Dependencies
EAR modules have a simple structure. When modules are added or removed from an EAR, WTP automatically updates the module and the contents of the EAR deployment descriptor, application.xml, which is stored in the META-INF directory.