Building a Web Application Project

Now, let’s focus on building the DayTrader web module. The layout is the same as for a JAR module (see the first two chapters of this book), except that there is an additional src/main/webapp directory for locating Web application resources such as HTML pages, JSPs, WEB-INF configuration files, etc. (see Figure 4-8).


Figure 4-8: Directory structure for the DayTrader web module showing some Web application resources

As usual everything is specified in the pom.xml file:

<project>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.apache.geronimo.samples.daytrader</groupId>
        <artifactId>daytrader</artifactId>
        <version>1.0</version>
    </parent>
    <artifactId>daytrader-web</artifactId>
    <name>DayTrader :: Web Application</name>
    <packaging>war</packaging>
    <description>DayTrader Web</description>
    <dependencies>
        <dependency>
            <groupId>org.apache.geronimo.samples.daytrader</groupId>
            <artifactId>daytrader-ejb</artifactId>
            <version>1.0</version>
            <type>ejb-client</type>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-j2ee_1.4_spec</artifactId>
            <version>1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

You start by telling Maven that it’s building a project generating a WAR:

<packaging>war</packaging>

Next, you specify the required dependencies. The reason you are building this web module after the ejb module is because the web module’s servlets call the EJBs. Therefore, a dependency has been added on the ejb module in web/pom.xml:

<dependency>
    <groupId>org.apache.geronimo.samples.daytrader</groupId>
    <artifactId>daytrader-ejb</artifactId>
    <version>1.0</version>
    <type>ejb-client</type>
</dependency>

Note that you’re specifying a type of ejb-client and not ejb. This is because the servlets are a client of the EJBs. Therefore, the servlets only need the EJB client JAR in their classpath to be able to call the EJBs. This is why you told the EJB plugin to generate a client JAR earlier on in ejb/pom.xml.

Depending on the main EJB JAR would also work, but it’s not necessary and would increase the size of the WAR file. It’s always cleaner to depend on the minimum set of required classes, for example to prevent coupling.

Note: If you add a dependency on a WAR, then the WAR you generate will be overlaid with the content of that dependent WAR, allowing the aggregation of multiple WAR files. However, only files not in the existing Web application will be added, and files such as web.xml won’t be merged. An alternative is to use the uberwar goal from the Cargo Maven Plugin (see http://cargo.codehaus.org/Merging+WAR+files).

The final dependency listed is the J2EE JAR as your web module uses servlets and calls EJBs. Again, the Geronimo J2EE specifications JAR is used with a provided scope (as seen previously when building the EJB).

Note: As you know, Maven 2 supports transitive dependencies. When it generates your WAR, it recursively adds your module’s dependencies, unless their scope is test or provided. This is why we defined the J2EE JAR using a provided scope in the web module’s pom.xml. Otherwise, it would have surfaced in the WEB-INF/lib directory of the generated WAR.

The configuration is very simple because the defaults from the WAR plugin are being used. As seen in the introduction, it’s a good practice to use the default conventions as much as possible, as it reduces the size of the pom.xml file and reduces maintenance.

Running mvn install generates the WAR and installs it in your local repository:

C:devm2bookcodej2eedaytraderweb>mvn install
[...]
[INFO] [war:war]
[INFO] Exploding webapp...
[INFO] Copy webapp resources to
C:devm2bookcodej2eedaytraderwebtargetdaytrader-web-1.0
[INFO] Assembling webapp daytrader-web in
C:devm2bookcodej2eedaytraderwebtargetdaytrader-web-1.0
[INFO] Generating war
C:devm2bookcodej2eedaytraderwebtargetdaytrader-web-1.0.war
[INFO] Building war:
C:devm2bookcodej2eedaytraderwebtargetdaytrader-web-1.0.war
[INFO] [install:install]
[INFO] Installing
C:devm2bookcodej2eedaytraderwebtargetdaytrader-web-1.0.war to
C:[...].m2repositoryorgapachegeronimosamplesdaytrader
daytrader-web1.0daytrader-web-1.0.war

The table below lists some other parameters of the WAR plugin that you may wish to configure:

Table 4-2: WAR plugin configuration properties

Configuration property Default value Description
warSourceDirectory ${basedir}/src/main/webapp Location of Web application resources to include in the WAR.
webXml The web.xml file found in ${warSourceDirectory}/WEB-INF/web.xml Specify where to find the web.xml file.
warSourceIncludes/warSourceExcludes All files are included Specify the files to include/exclude from the generated WAR.
warName ${project.build.finalName} Name of the generated WAR.

For the full list, see the reference documentation for the WAR plugin at http://maven.apache.org/plugins/maven-war-plugin/.

Thank you for requesting a Maestro evaluation! This is our passion, and we want you to be successful. Please let us know how we may help!

Please enter your name, company email address and phone, and we will send you a link to your pre-built hosted evaluation within minutes.






I have read and agree to the Terms and Conditions.