ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Building J2EE Projects with Maven
Pages: 1, 2, 3, 4, 5, 6

Building a WAR Using XDoclet

Let's now build the xPetstore's modules/presentation WAR using XDoclet. If you're not using XDoclet you can safely skip this section. The directory structure is shown in Figure 9:

figure 9
Figure 9. xPetstore's presentation WAR subproject directory structure

The principle is the same as what we saw in the section about building an EJB with XDoclet: you create a custom maven.xml file in which you create a preGoal on java:compile, where you call the xdoclet:webdoclet goal:

  <preGoal name="java:compile">
    <attainGoal name="xdoclet:webdoclet"/>

Notice that we have java sources located in src/main/java. When we build the project they'll find their way to the generated WAR's WEB-INF/classes directory. Web files are located in src/main/webapp, as seen in the previous lab. Again this is configured in project.properties:

maven.war.src = ${maven.src.dir}/main/webapp

XDoclet can generate several files useful for a web application. More specifically it can generate the following:

  • Webapp deployment descriptor (web.xml)
  • Taglib descriptors (*.tld files)
  • Struts config file (struts-config.xml)
  • Container-specific deployment descriptors
  • And more. Check the reference documentation for all the details.

As with the EJB project above, you need to provide the information contained in these generate file in your source code: in your Servlet classes, Filter classes, Tag classes, Struts Action classes, Struts Form classes, etc. For example, the following kind of javadoc annotation can be found in the SignOnFilter.java file:

 * @web.filter
 *  name="signon"
 *  display-name="xPetstore Struts Signon Filter"
 * @web.filter-mapping
 *  servlet-name="action"

Then we need to configure the XDoclet Maven plugin so that it generates what we want. This is done by setting properties in the project.properties file:


This tells the plugin what files to parse. By default it is already preconfigured to parse Servlet, Tag, Filter, and Listener classes, which is why we start the fileset index at 4 and add Struts Actions and Forms:


This configures the generation of the web.xml file, including the Servlet specification to use, the taglibs to include, and so on.


This tells the plugin to generate the strusts-config.xml file.


This tells the plugin where to find custom file snippets that it will merge with the files it generates. In our case here, we have put these files in src/xdoclet (see Figure 9). We have three files: filter-mappings.xml, filers.xml, and welcomefiles.xml. The content of these three files will be merged with the generated web.xml file. The reason we need to do this is because we want to add a filter definition and mapping for the Sitemesh PageFilter filter. This is a file provided in an external library and thus we can't add javadoc annotations to automatically generate its definition and mapping. The welcomefiles.xml contains the name of the page to call when the user types the root URL for the webapp. As this information cannot be associated with any java source file we need to define it externally from the source code. Note that we could also have defined it as a configuration property in project.properties. The full list of files that can be merged to generate the web.xml file can be found in the xdoclet reference documentation for the deploymentdescriptor element.

The project.xml file is similar to the one shown in the previous section on building a WAR. It contains dependencies tagged with the <war.bundle>true</war.bundle> property.

Running the presentation project's build with "maven war:install" (or simply "maven" if you've defined a default goal) generates the WAR file.

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow