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

advertisement

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:

<dependencies>
  <dependency>
    <groupId>xdoclet</groupId>
    <artifactId>maven-xdoclet-plugin</artifactId>
    <version>1.2.3</version>
    <type>plugin</type>
  </dependency>
</dependencies>

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:

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

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:

maven.xdoclet.ejbdoclet.fileset.0=true
maven.xdoclet.ejbdoclet.fileset.0
  .include=**/*EJB.java

maven.xdoclet.ejbdoclet.entitybmp.0=true
maven.xdoclet.ejbdoclet.entitycmp.0=true
maven.xdoclet.ejbdoclet.entitypk.0=true

maven.xdoclet.ejbdoclet.remoteinterface.0=true
maven.xdoclet.ejbdoclet.remoteinterface.0
  .pattern={0}Remote

maven.xdoclet.ejbdoclet.homeinterface.0=true
maven.xdoclet.ejbdoclet.homeinterface.0
  .pattern={0}RemoteHome

maven.xdoclet.ejbdoclet.localhomeinterface.0=true
maven.xdoclet.ejbdoclet.localinterface.0=true

maven.xdoclet.ejbdoclet.session.0=true

maven.xdoclet.ejbdoclet.packageSubstitution.0=
  true
maven.xdoclet.ejbdoclet.packageSubstitution.0
  .packages=ejb
maven.xdoclet.ejbdoclet.packageSubstitution.0
  .substituteWith=interfaces
[...]

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"/>
  <packageSubstitution 
      packages="ejb" 
      substituteWith="interfaces"/>
  <remoteinterface pattern="{0}Remote"/>
  <homeinterface  pattern="{0}RemoteHome"/>
  <localinterface/>
  <localhomeinterface/>
  <entitypk/>
  <entitycmp/>
  <session/>
[...]
</ejbdoclet>

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":

C:\[...]\modules\business>maven
[...]
java:compile:
xdoclet:ejbdoclet:
    [ejbdoclet] Generating EJB deployment 
descriptor (ejb-jar.xml).
[...]
    [ejbdoclet] Generating CMP class for 
'xpetstore.domain.customer.ejb.CustomerEJB'.
[...]
    [ejbdoclet] Generating Local Home interface 
for 'xpetstore.domain.customer.ejb.CustomerEJB'.
[...]
    [ejbdoclet] Generating Local interface for
'xpetstore.domain.customer.ejb.CustomerEJB'.
[...]
    [ejbdoclet] Generating Util class for 
'xpetstore.domain.customer.ejb.CustomerEJB'.
[...]
    [ejbdoclet] Generating Value Object class:
'xpetstore.domain.customer.ejb.CustomerEJB-->
 xpetstore.domain.customer.model.CustomerValue'.
[...]

ejb:ejb:
ejb:ejb-jar-internal:
    [echo] Building ejb xpetstore-business-3.1.3
[...]

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

Next Pagearrow