Building an EJB Project
Let’s create a build for the ejb module.

Figure 4-6: Directory structure for the DayTrader ejb module
Figure 4-6 shows a canonical directory structure for EJB projects:
- Runtime Java source code in
src/main/java. - Runtime classpath resources in
src/main/resources. More specifically, the standardejb-jar.xmldeployment descriptor is insrc/main/resources/META-INF/ejb-jar.xml. Any container-specific deployment descriptor should also be placed in this directory. - Unit tests in
src/test/javaand classpath resources for the unit tests insrc/test/resources. Unit tests are tests that execute in isolation from the container. Tests that require the container to run are called integration tests and are covered at the end of this chapter.
Now, take a look at the content of this project’s pom.xml file:
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.geronimo.samples.daytrader</groupId>
<artifactId>daytrader</artifactId>
<version>1.0</version>
</parent>
<artifactId>daytrader-ejb</artifactId>
<name>Apache Geronimo DayTrader EJB Module</name>
<packaging>ejb</packaging>
<description>DayTrader EJBs</description>
<dependencies>
<dependency>
<groupId>org.apache.geronimo.samples.daytrader</groupId>
<artifactId>daytrader-wsappclient</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-j2ee_1.4_spec</artifactId>
<version>1.0</version> <scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.0.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<configuration>
<generateClient>true</generateClient>
<clientExcludes>
<clientExclude>**/ejb/*Bean.class</clientExclude>
</clientExcludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
As you can see, you’re extending a parent POM using the parent element. This is because the DayTrader build is a multi-module build and you are gathering common POM elements in a parent daytrader/pom.xml file. If you look through all the dependencies you should see that we are ready to continue with building and installing this portion of the build.
The ejb/pom.xml file is is a standard POM file except for three items:
- You need to tell Maven that this project is an EJB project so that it generates an EJB JAR when the package phase is called. This is done by specifying:
<packaging>ejb</packaging>
- As you’re compiling J2EE code you need to have the J2EE specifications JAR in the project’s build classpath. This is achieved by specifying a dependency element on the J2EE JAR. You could instead specify a dependency on Sun’s J2EE JAR. However, this JAR is not redistributable and as such cannot be found on ibiblio. Fortunately, the Geronimo project has made the J2EE JAR available under an Apache license and this JAR can be found on ibiblio.
You should note that you’re using a provided scope instead of the default compile scope. The reason is that this dependency will already be present in the environment (being the J2EE application server) where your EJB will execute. You make this clear to Maven by using the provided scope; this prevents the EAR module from including the J2EE JAR when it is packaged. Even though this dependency is provided at runtime, it still needs to be listed in the POM so that the code can be compiled.
- Lastly, the
pom.xmlcontains a configuration to tell the Maven EJB plugin to generate a Client EJB JAR file whenmvn installis called. The Client will be used in a later examples when building the web module. By default the EJB plugin does not generate the client JAR, so you must explicitly tell it to do so:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<configuration>
<generateClient>true</generateClient>
<clientExcludes>
<clientExclude>**/ejb/*Bean.class</clientExclude>
</clientExcludes>
</configuration>
</plugin>
The EJB plugin has a default set of files to exclude from the client EJB JAR: **/*Bean.class, **/*CMP.class, **/*Session.class and **/package.html.
In this example, you need to override the defaults using a clientExclude element because it happens that there are some required non-EJB files matching the default **/*Bean.class pattern and which need to be present in the generated client EJB JAR. Thus you’re specifying a pattern that only excludes from the generated client EJB JAR all EJB implementation classes located in the ejb package (**/ejb/*Bean.class). Note that it’s also possible to specify a list of files to include using clientInclude elements.
You’re now ready to execute the build. Relax and type mvn install:
C:devm2bookcodej2eedaytraderejb>mvn install [INFO] Scanning for projects... [INFO] ----------------------------------------------------------- [INFO] Building DayTrader :: EJBs [INFO] task-segment: [install] [INFO] ----------------------------------------------------------- [INFO] [resources:resources] [INFO] Using default encoding to copy filtered resources. [INFO] [compiler:compile] Compiling 49 source files to C:devm2bookcodej2eedaytraderejbtargetclasses [INFO] [resources:testResources] [INFO] Using default encoding to copy filtered resources. [INFO] [compiler:testCompile] Compiling 1 source file to C:devm2bookcodej2eedaytraderejbtargettest-classes [INFO] [surefire:test] [INFO] Setting reports dir: C:devm2bookcodej2eedaytraderejbtarget/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- [surefire] Running org.apache.geronimo.samples.daytrader.FinancialUtilsTest [surefire] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0,02 sec Results : [surefire] Tests run: 1, Failures: 0, Errors: 0 [INFO] [ejb:ejb] [INFO] Building ejb daytrader-ejb-1.0 [INFO] Building jar: C:devm2bookcodej2eedaytraderejb targetdaytrader-ejb-1.0.jar [INFO] Building ejb client daytrader-ejb-1.0-client [INFO] Building jar: C:devm2bookcodej2eedaytraderejb targetdaytrader-ejb-1.0-client.jar [INFO] [install:install] [INFO] Installing C:devm2bookcodej2eedaytraderejb targetdaytrader-ejb-1.0.jar to C:[...].m2repositoryorgapachegeronimosamples daytraderdaytrader-ejb1.0daytrader-ejb-1.0.jar [INFO] Installing C:devm2bookcodej2eedaytraderejb targetdaytrader-ejb-1.0-client.jar to C:[...].m2repositoryorgapachegeronimosamples daytraderdaytrader-ejb1.0daytrader-ejb-1.0-client.jar
Maven has created both the EJB JAR and the client EJB JAR and installed them in your local repository.
The EJB plugin has several other configuration elements that you can use to suit your exact needs. Please refer to the EJB plugin documentation on this site.