Building an EAR Project

You have now built all the individual modules. It’s time to package the server module artifacts (EJB and WAR) into an EAR for convenient deployment. The ear module’s directory structure can’t be any simpler… it solely consists of a pom.xml file (see figure below).


Figure 4-11: Directory structure of the ear module

As usual the magic happens in the pom.xml file. The POM has defined that this is an EAR project by using the packaging element:

<project>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.apache.geronimo.samples.daytrader</groupId>
        <artifactId>daytrader</artifactId> <version>1.0</version>
    </parent>
    <artifactId>daytrader-ear</artifactId>
    <name>DayTrader :: Enterprise Application</name>
    <packaging>ear</packaging>
    <description>DayTrader EAR</description>

Next, the pom.xml file defines all of the dependencies that need to be included in the generated EAR:

<dependencies>
    <dependency>
        <groupId>org.apache.geronimo.samples.daytrader</groupId>
        <artifactId>daytrader-wsappclient</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.geronimo.samples.daytrader</groupId>
        <artifactId>daytrader-web</artifactId>
        <version>1.0</version>
        <type>war</type>
    </dependency>
    <dependency>
        <groupId>org.apache.geronimo.samples.daytrader</groupId>
        <artifactId>daytrader-ejb</artifactId>
        <version>1.0</version>
        <type>ejb</type>
    </dependency>
    <dependency>
        <groupId>org.apache.geronimo.samples.daytrader</groupId>
        <artifactId>daytrader-streamer</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

Finally, you need to configure the Maven EAR plugin by giving it the information it needs to automatically generate the application.xml deployment descriptor file. This includes the display name to use, the description to use, and the J2EE version to use. It is also necessary to tell the EAR plugin which of the dependencies are Java modules, Web modules, and EJB modules. At the time of writing, the EAR plugin supports the following module types: ejb, war, jar, ejb-client, rar, ejb3, par, sar, and wsr.

By default, all dependencies are included, with the exception of those that are optional, or those with a scope of test or provided. However, it is often necessary to customize the inclusion of some dependencies such as shown in this example:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <configuration>
                <displayName>Trade</displayName>
                <description> DayTrader Stock Trading Performance Benchmark Sample </description>
                <version>1.4</version>
                <modules>
                    <javaModule>
                        <groupId>org.apache.geronimo.samples.daytrader</groupId>
                        <artifactId>daytrader-streamer</artifactId>
                        <includeInApplicationXml>true</includeInApplicationXml>
                    </javaModule>
                    <javaModule>
                        <groupId>org.apache.geronimo.samples.daytrader</groupId>
                        <artifactId>daytrader-wsappclient</artifactId>
                        <includeInApplicationXml>true</includeInApplicationXml>
                    </javaModule>
                    <webModule>
                        <groupId>org.apache.geronimo.samples.daytrader</groupId>
                        <artifactId>daytrader-web</artifactId>
                        <contextRoot>/daytrader</contextRoot>
                    </webModule>
                </modules>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

Here, the contextRoot element is used for the daytrader-web module definition to tell the EAR plugin to use that context root in the generated application.xml file.

You should also notice that you have to specify the includeInApplicationXml element in order to include the streamer and wsappclient libraries into the EAR. By default, only EJB client JARs are included when specified in the Java modules list.

It is also possible to configure where the JARs’ Java modules will be located inside the generated EAR. For example, if you wanted to put the libraries inside a lib subdirectory of the EAR you would use the bundleDir element:

<javaModule>
    <groupId>org.apache.geronimo.samples.daytrader</groupId>
    <artifactId>daytrader-streamer</artifactId>
    <includeInApplicationXml>true</includeInApplicationXml>
    <bundleDir>lib</bundleDir>
</javaModule>
<javaModule>
    <groupId>org.apache.geronimo.samples.daytrader</groupId>
    <artifactId>daytrader-wsappclient</artifactId>
    <includeInApplicationXml>true</includeInApplicationXml>
    <bundleDir>lib</bundleDir>
</javaModule>

In order not to have to repeat the bundleDir element for each Java module definition you can instead use the defaultJavaBundleDir element:

[...]
<defaultBundleDir>lib</defaultBundleDir>
<modules>
    <javaModule> ... </javaModule>
[...]

There are some other configuration elements available in the EAR plugin which you can find out by checking the reference documentation on http://maven.apache.org/plugins/maven-ear-plugin.

Note: The streamer module’s build is not described in this chapter because it’s a standard build generating a JAR. However the ear module depends on it and thus you’ll need to have the Streamer JAR available in your local repository before you’re able to run the ear module’s build. Run mvn install in daytrader/streamer.

To generate the EAR, run mvn install:

C:devm2bookcodej2eedaytraderear>mvn install
[…]
[INFO] [ear:generate-application-xml]
[INFO] Generating application.xml
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [ear:ear]
[INFO] Copying artifact [jar:org.apache.geronimo.samples.daytrader:
aytrader-streamer:1.0] to [daytrader-streamer-1.0.jar]
[INFO] Copying artifact [jar:org.apache.geronimo.samples.daytrader:
daytrader-wsappclient:1.0] to [daytrader-wsappclient-1.0.jar]
[INFO] Copying artifact [war:org.apache.geronimo.samples.daytrader:
daytrader-web:1.0] to [daytrader-web-1.0.war]
[INFO] Copying artifact [ejb:org.apache.geronimo.samples.daytrader:
daytrader-ejb:1.0] to[daytrader-ejb-1.0.jar]
[INFO] Copying artifact [ejb-client:org.apache.geronimo.samples.daytrader:
daytrader-ejb:1.0] to [daytrader-ejb-1.0-client.jar]
[INFO] Could not find manifest file:
C:devm2bookcodej2eedaytraderearsrcmainapplication META-INFMANIFEST.MF - Generating one
[INFO] Building jar: C:devm2bookcodej2eedaytraderear targetdaytrader-ear-1.0.ear
[INFO] [install:install]
[INFO] Installing C:devm2bookcodej2eedaytraderear targetdaytrader-ear-1.0.ear to
C:[...].m2repositoryorgapachegeronimosamples daytraderdaytrader-ear1.0daytrader-ear-1.0.ear

You should review the generated application.xml to prove that it has everything you need:

<?xml version="1.0" encoding="UTF-8"?>
    <application xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/application_1_4.xsd" version="1.4">
    <description> DayTrader Stock Trading Performance Benchmark Sample </description>
    <display-name>Trade</display-name>
    <module>
        <java>daytrader-streamer-1.0.jar</java>
    </module>
    <module>
        <java>daytrader-wsappclient-1.0.jar</java>
    </module>
    <module>
        <web>
            <web-uri>daytrader-web-1.0.war</web-uri>
            <context-root>/daytrader</context-root>
        </web>
    </module>
    <module>
        <ejb>daytrader-ejb-1.0.jar</ejb>
    </module>
</application>

This looks good. The next section will demonstrate how to deploy this EAR into a container.

Thank you for requesting a Maestro evaluation! This is our passion, and we want you to be successful. Please let us know how we may help!

Please enter your name, company email address and phone, and we will send you a link to your pre-built hosted evaluation within minutes.






I have read and agree to the Terms and Conditions.