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 an EJB-JAR Using XDoclet

Using XDoclet javadoc annotations is a very good way to reduce EJB 2.0 clutter and to keep source and configuration all together. The xPetstore project is a port of the Java Petstore project using XDoclet annotations. Let's find out how to build an EJB-JAR project using XDoclet with Maven. More specifically, we'll build the business/module introduced earlier with Maven (see Figure 3). Figure 5 shows the directory structure of the business/project.

figure 5
Figure 5. A typical directory structure for an EJB-JAR project using XDoclet

As you can see, the main difference from Figure 4 is that there is only one file for the Customer EJB. This is because this CustomerEJB.java file contains the XDoclet annotations that will generate at build time the CustomerLocal, CustomerLocalHome, CustomerCMP and even CustomerValue and CustomerUtil classes, as shown in Figure 6.

figure 6
Figure 6. Example of classes and descriptors produced by XDoclet at build time

So how do we tell Maven to run XDoclet in our sources? The XDoclet project has a nice Maven XDoclet plugin that can be used for this.

Tip: The XDoclet plugin is not part of the Maven distribution so it needs to be installed in the Maven installation of everyone wanting to build your EJB-JAR project. Luckily Maven provides a nice way to automatically install a plugin as part of your project's build. It is achieved by defining a dependency on the XDoclet plugin. Add the following to the business/project.xml file:


Now when you execute Maven it'll automatically download and install the XDoclet plugin if it's not already present in your local Maven installation. Neat, no?

To use the XDoclet plugin we need to tell it to run before Maven compiles the source code. This is achieved by declaring a preGoal on the java:compile goal in a business/maven.xml file:

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

Then, of course, we need to configure the XDoclet plugin to tell it what to generate. This is achieved by setting properties in the business/project.properties file. Here's a portion of the configuration we need for the xPetstore:








All those properties are actually directly derived from the <ejbdoclet> Ant task. For example, the above is the equivalent of:

<ejbdoclet [...]>
  <fileset [...] includes="**/*EJB.java"/>
  <remoteinterface pattern="{0}Remote"/>
  <homeinterface  pattern="{0}RemoteHome"/>

Note: This is where we realize that XML is a much more concise syntax than the properties file for configuration. This is a lesson that has been learned in Maven 2 as there is no more properties file. All the project definition and plugin configuration is now done in the POM as one XML file.

Make sure you have a <defaultGoal> of ejb:install defined in business/project.xml. You can now build the EJB-JAR by simply typing "maven":

    [ejbdoclet] Generating EJB deployment 
descriptor (ejb-jar.xml).
    [ejbdoclet] Generating CMP class for 
    [ejbdoclet] Generating Local Home interface 
for 'xpetstore.domain.customer.ejb.CustomerEJB'.
    [ejbdoclet] Generating Local interface for
    [ejbdoclet] Generating Util class for 
    [ejbdoclet] Generating Value Object class:

    [echo] Building ejb xpetstore-business-3.1.3

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

Next Pagearrow