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.
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).
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/.