Enterprise Java Development@TOPIC@
Useful when need to deploy EJB with dependent artifacts or without WAR
|-- ejb-basic-ejb.jar `-- META-INF `-- application.xml
Must supply a META-INF/application.xml with the EJB listed as an ejb module
May supply utility jars in library-directory (e.g., /lib)
$ more META-INF/application.xml
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd" version="7">
<application-name>ejb-basic-ear</application-name>
<description>This projects provides an example EAR for deploying
one or more EJB and other type modules.
This EAR is assembled here, but tested as a part of the
function/integration testing in a sibling project.</description>
<display-name>ejb-basic-ear</display-name>
<module>
<ejb>ejb-basic-ejb.jar</ejb>
</module>
<library-directory>lib</library-directory>
</application>
EJB module deployed within EAR
19:50:07,583 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-1) JNDI bindings for session bean named GreeterEJB in deployment unit subdeployment "ejb-basic-ejb.jar" of deployment "ejb-basic-ear-4.0.0-SNAPSHOT.ear" are as follows: java:global/ejb-basic-ear/ejb-basic-ejb/GreeterEJB!info.ejava.examples.ejb.basic.ejb.GreeterRemote java:app/ejb-basic-ejb/GreeterEJB!info.ejava.examples.ejb.basic.ejb.GreeterRemote java:module/GreeterEJB!info.ejava.examples.ejb.basic.ejb.GreeterRemote java:jboss/exported/ejb-basic-ear/ejb-basic-ejb/GreeterEJB!info.ejava.examples.ejb.basic.ejb.GreeterRemote java:global/ejb-basic-ear/ejb-basic-ejb/GreeterEJB java:app/ejb-basic-ejb/GreeterEJB java:module/GreeterEJB 19:50:07,588 INFO [org.jboss.weld.deployer] (MSC service thread 1-1) JBAS016005: Starting Services for CDI deployment: ejb-basic-ear-4.0.0-SNAPSHOT.ear 19:50:07,596 INFO [org.jboss.weld.deployer] (MSC service thread 1-2) JBAS016008: Starting weld service for deployment ejb-basic-ear-4.0.0-SNAPSHOT.ear
JBoss server prints out JNDI names to access EJB through...
@Remote
No interface
Globally within the server (java:global)
Within the deployed application (java:app)
Within the EJB module (java:module)
External to server (java:global/exported)
Derive remote client JNDI name from "exported" name
ejb-basic-ear/ejb-basic-ejb/GreeterEJB!info.ejava.examples.ejb.basic.ejb.GreeterRemote
Local and No-interface interfaces are not exposed to remote clients
packaging=ear
Configures plugin to perform EAR-like packaging of dependencies
ejb scope=compile dependencies
Name upstream Maven modules that should be deployed as EJBs
maven-ear-plugin
Can perform most actions be default/convention. Example shows how to clean up JNDI names so that version# is not part of the JNDI name exposed
<project>
<groupId>info.ejava.examples.ejb.basicejb</groupId>
<artifactId>ejb-basic-ear</artifactId>
<packaging>ear</packaging>
<dependencies>
<!-- The EAR must have a scope=compile dependency on the EJB -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ejb-basic-ejb</artifactId>
<version>${project.version}</version>
<type>ejb</type>
</dependency>
</dependencies>
<build>
<plugins>
<!-- provide properties here to impact the EAR packaging -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<!-- eliminates use of version in EAR JNDI name portion -->
<applicationName>${project.artifactId}</applicationName>
<modules>
<!-- eliminates use of the version in the EJB JNDI name -->
<ejbModule>
<groupId>${project.groupId}</groupId>
<artifactId>ejb-basic-ejb</artifactId>
<bundleFileName>ejb-basic-ejb.jar</bundleFileName>
</ejbModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
Add optional undeploy of EAR to clean lifecycle
Must be done in pre-clean phase before .ear artifact removed
Latch in profile (-Pundeploy) to prevent from running when .ear not exists
<profiles>
<profile>
<id>undeploy</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<executions>
<execution>
<id>undeploy-ear</id>
<phase>pre-clean</phase>
<goals>
<goal>undeploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Add EAR artifact as a scope=compile dependency when deploying from separate (test) module
<dependencies>
...
<!-- packages being deployed must be a dependency -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ejb-basic-ear</artifactId>
<version>${project.version}</version>
<type>ear</type>
<scope>compile</scope>
</dependency>
...
</dependencies>
Declare EAR artifact to be deployed
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<!-- artifacts to deploy to server -->
<deployables>
<deployable>
<groupId>${project.groupId}</groupId>
<artifactId>ejb-basic-ear</artifactId>
<type>ear</type>
</deployable>
</deployables>
</configuration>
</plugin>
</plugins>
</build>
Details of cargo-maven2-plugin are provided elsewhere
Deployed EJB using an EAR
Traditional JavaEE deployment
Permits deployment of dependency artifacts
Obtained JNDI name of EAR-based @Remote
(application-name)/(component-name)/(ejb-name)!(remote-interface-name)
ejb-basic-ear/ejb-basic-ejb/GreeterEJB!info.ejava.examples.ejb.basic.ejb.GreeterRemote