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 EAR

With your newfound knowledge of building JARs, EJB-JARs, and WARs we are now ready to tackle building applications in the form of a packaged EAR. Let's build the Java Petstore's applications/waf project (Figure 10). The WAF is a reusable presentation framework but the Java Petstore also packages it as an application to demonstrate how it works.

figure 10
Figure 10. Project directory structure to generate the WAF EAR

This is really a minimal Maven project, but we don't need more to generate an EAR. We do this using the Maven EAR plugin. It can automatically generate an application.xml deployment descriptor. Note that we could also provide one by putting it in src/main/application/META-INF/application.xml and telling the EAR plugin to use it by setting the following in our project.properties file (Again this is required because by default the EAR plugin looks into src/application, which is no longer the recommended directory structure, as we saw earlier.):

maven.ear.src = ${maven.src.dir}/main/application

As we want to generate the application.xml file, the only things that we need to specify are the dependencies that will get included in the EAR (JARs, EJB-JARs, WARs, RARs, and SARs). For example we need to include the WAF WAR that we created earlier on in a previous lab. This is done in project.xml as follows:

<dependency>
  <groupId>com.sun.j2ee.blueprints</groupId>
  <artifactId>waf-web</artifactId>
  <version>1.4</version>
  <type>war</type>
  <properties>
    <ear.bundle>true</ear.bundle>
    <ear.appxml.war.context-root>
      /waf
    </ear.appxml.war.context-root>
  </properties>
</dependency>

This follows the same principle that we already used when building a WAR: we tag the dependencies we want to include with an ear.bundle property set to true. However, we have also used the ear.appxml.war.context-root property, which tells the EAR plugin what context root our WAR should have. This information will find its way into the generated application.xml:

<module>
  <web>
    <web-uri>waf-web-1.4.war</web-uri>
    <context-root>/waf</context-root>
  </web>
</module>

There are also three other useful properties:

  • ear.bundle.dir: decides where to put the dependency in the EAR (defaults to the top level are unspecified).
  • ear.bundle.name: decides how to name the dependency in the EAR (defaults to its artifact id including the version number).
  • ear.module: a boolean property (<ear.module>true</ear.module>) used in place of ear.bundle when you wish to include a client JAR in the EAR.

Now in our case we have no other resources to add to our EAR, but if we had any we could put them in src/main/applications, or we could put them in src/main/resources and specify them as resources in the project.xml:

<build>
  [...]
  <resources>
    <resource>
      <directory>src/main/resources</directory>
    </resource>
  </resources>

Running the build with "maven ear:install" generates the EAR file and installs it in your local repository:

C:\[...]\applications\waf>maven ear:install
[...]
ear:generate-ear-descriptor:
    [echo] Generating appxml file:
"C:\dev\maven-petstore1.4\applications\waf
/target/application.xml
version 1.3 encoding: UTF-8"

    [echo] Building EAR waf-ear-1.4.ear with appxml 
"C:\dev\maven-petstore1.4\applications\waf
/target/application.xml"
    [ear] Building ear: C:\dev\maven-petstore1.4\
applications\waf\target\waf-ear-1.4.ear

ear:install:
    [echo] Installing...
Uploading to com.sun.j2ee.blueprints/ears/
waf-ear-1.4.ear:
.................... (82K)
Uploading to com.sun.j2ee.blueprints/poms/
waf-ear-1.4.pom:
.................... (1K)

We have completed our tour of building J2EE applications with Maven. You should now be able to find your way around Maven and explore more by using the reference documentation for each of the Maven plugins we have touched upon.

Vincent Massol is the creator of the Jakarta Cactus framework and an active member of the Maven project since the early days. After having spent four years as a technical architect on several major projects (mostly J2EE), Vincent is now the co-founder and CTO of Pivolis, a company specializing in applying agile methodologies to offshore software development. He lives in the City of Light, Paris, France and can be found online at www.massol.net.


Return to ONJava.com