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

One interesting feature of the EJB plugin is that it generates a Manifest file for your EJB JAR automatically. When your EJB JAR needs to add a dependency on an external library (as it is the case above for xmldocuments) you simply specify it by "tagging" the corresponding dependency with an ejb.manifest.classpath property. In our case the generated Manifest.mf file contains the Class-Path entry specifying the dependent JAR:



Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: Apache Maven
Built-By: Vincent Massol
Package: com.sun.j2ee.blueprints.address
Class-Path:  xmldocuments-1.4.jar
[...]

Just before executing the build, let's have a look at the project.properties file. This is where you customize Maven plugins. It has three entries:

  • maven.ejb.client.generate = true: tells the Maven EJB plugin to generate an EJB client jar.

  • maven.ejb.client.excludes = **/*EJB.class: tells the Maven EJB plugin to exclude all java classes named *EJB from the EJB client JAR. The default exclusions done by the EJB plugin are **/*Bean.class,**/*CMP.class,**/*Session.class. Thus, if you name your EJB implementation jar using the *Bean convention, you won't need to specify a special exclusion.

  • maven.multiproject.type = ejb: tells the Maven Multiproject plugin that this project is an EJB project type. This allows you to build all your subprojects in one go from a top-level directory using the "maven multiproject:install" goal for example.

Let's now build the address project by issuing the "maven" command in the address/ directory (note: code lines have been truncated to 50 characters):

C:\dev\maven-petstore1.4\modules\address>maven 
[...]

ejb:ejb:
ejb:ejb-jar-internal:
    [echo] Building ejb address-1.4
    [jar] Building jar: C:\dev\maven-petstore1.4\
modules\address\target\address-1.4.jar

ejb:ejb-client-internal:
    [jar] Building jar: C:\dev\maven-petstore1.4\
modules\address\target\address-1.4-client.jar

ejb:install:
    [echo] Installing...
Uploading to com.sun.j2ee.blueprints/ejbs/
address-1.4.jar:
.................... (9K)
Uploading to com.sun.j2ee.blueprints/poms/
address-1.4.pom:
.................... (1K)
    [echo] Installing...
Uploading to com.sun.j2ee.blueprints/ejbs/
address-1.4-client.jar:
.................... (8K)
Uploading to com.sun.j2ee.blueprints/poms/
address-1.4.pom:
.................... (1K)

Maven has run the ejb:install goal (specified in our defaultGoal element in the POM) and built both the EJB-JAR and the client jar. It has also published them to your local Maven repository so that they are now available as dependencies for other projects. If you simply want to build the artifacts without installing them, simply specify the ejb:ejb goal (or ejb for short). If you wish to upload them to a Maven remote repository, use ejb:deploy (in that case you'll also need to specify some other properties, which we'll see later on in this article). See the EJB plugin reference documentation for more options.

Note: You may wonder how to include the generated EJB client jar in another project, say a WAR. This is a new feature of Maven 1.1, which has added a new dependency type called "ejb-client." For example, to include the address 1.4 EJB client jar you would add the dependency:

<dependency>
  <groupId>com.sun.j2ee.blueprints</groupId>
  <artifactId>address</artifactId>
  <version>1.4</version>
  <type>ejb-client</type>
</dependency>

We'll demonstrate this later in this article when we build the Java Petstore WAF WAR.

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

Next Pagearrow