Deploying EJBS
Now that you know how to build an EJB project, you will learn how to deploy it. Later, you will also learn how to test it automatically, in the Testing J2EE Applications section of this chapter. Let’s discover how you can automatically start a container and deploy your EJBs into it.
First, you will need to have Maven start the container automatically. To do so you’re going to use the Maven plugin for Cargo. Cargo is a framework for manipulating containers. It offers generic APIs (Java, Ant, Maven 1, Maven 2, Netbeans, IntelliJ IDEA, etc.) for performing various actions on containers such as starting, stopping, configuring them and deploying modules to them. In this example, the JBoss container will be used.
The ejb/pom.xml file has been edited adding following Cargo plugin configuration:
<build>
<plugins>
[...]
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<container>
<containerId>jboss4x</containerId>
<zipUrlInstaller>
<url>http://internap.dl.sourceforge.net/
sourceforge/jboss/jboss-4.0.2.zip</url>
<installDir>${installDir}</installDir>
</zipUrlInstaller>
</container>
</configuration>
</plugin>
</plugins>
</build>
If you want to debug Cargo’s execution, you can use the log element to specify a file where Cargo logs will go and you can also use the output element to specify a file where the container’s output will be dumped. For example:
<container>
<containerId>jboss4x</containerId>
<output>${project.build.directory}/jboss4x.log</output>
<log>${project.build.directory}/cargo.log</log>
[...]
See http://cargo.codehaus.org/Debugging for full details.
In the container element you tell the Cargo plugin that you want to use JBoss 4.x ( containerId element) and that you want Cargo to download the JBoss 4.0.2 distribution from the specified URL and install it in ${installDir}. The location where Cargo should install JBoss is a user-dependent choice and this is why the ${installDir} property was introduced. In order to build this project you need to create a Profile where you define the ${installDir property’s value.
As explained in the previous chapter, you can define a profile in the POM, in a profiles.xml file, or in a settings.xml file. In this case, as the content of the Profile is user-dependent you wouldn’t want to define it in the POM. Nor should the content be shared with other Maven projects at large, in a settings.xml file. Thus the best place is to create a profiles.xml file in the ejb/ directory:
<profilesXml>
<profiles>
<profile>
<id>vmassol</id>
<properties>
<installDir>c:/apps/cargo-installs</installDir>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>vmassol</activeProfile>
</activeProfiles>
</profilesXml>
This sample profiles.xml file defines a profile named vmassol, activated by default and in which the ${installDir} property points to c:/apps/cargo-installs.
It’s also possible to tell Cargo that you already have JBoss installed locally. In that case replace the zipURLInstaller element with a home element. For example:
<home>c:/apps/jboss-4.0.2</home>
That’s all you need to have a working build and to deploy the EJB JAR into JBoss. The Cargo plugin does all the work: it provides a default JBoss configuration (using port 8080 for example), it detects that the Maven project is producing an EJB from the packaging element and it automatically deploys it when the container is started.
Of course, the EJB JAR should first be created, so run mvn package to generate it, then start JBoss and deploy the EJB JAR by running mvn cargo:start (or mvn package cargo:start to do it all at once):
C:\dev\m2book\code\j2ee\daytrader\ejb>mvn cargo:start
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'cargo'.
[INFO] -----------------------------------------------------------------------
[INFO] Building DayTrader :: EJBs
[INFO] task-segment: [cargo:start]
[INFO] -----------------------------------------------------------------------
[INFO] [cargo:start]
[INFO] [talledLocalContainer] Parsed JBoss version = [4.0.2]
[INFO] [talledLocalContainer] JBoss 4.0.2 starting...
[INFO] [talledLocalContainer] JBoss 4.0.2 started on port [8080]
[INFO] Press Ctrl-C to stop the container..
That’s it! JBoss is running, and the EJB JAR has been deployed.
Note: As you have told Cargo to download and install JBoss, the first time you execute cargo:start it will take some time, especially if you are on a slow connection. Subsequent calls will be fast as Cargo will not download JBoss again.
If the container was already started and you wanted to just deploy the EJB, you would run the cargo:deploy goal. Finally, to stop the container call mvn cargo:stop.
Cargo has many other configuration options such as the possibility of using an existing container installation, modifying various container parameters, deploying on a remote machine, and more. Check the documentation at http://cargo.codehaus.org/Maven2+plugin.

