Creating an EAR with Maven

Maven is the build tool of choice for many people due to the simplicity and flexibility of use. The real strength of Maven is vetted when handling real complex projetcs consisting of tens of modules, each requiring elaborate build requirements.

I had faced an uphill task of Mavenising one of our EAR projects. As is the typical case, our project consisted of a EJB module and a WAR module, package together in a EAR module. It was a bit complex as we had to deploy the same application in JBoss and Glassfish. I will write some of that experience here.

Let us consider an enterprise application having a structure as show below:

Project Structure

I will briefly explain what the modules stand for:

  1. swayam-ear: This is the enterprise application
  2. swayam-ejb: Is the EJB module
  3. swayam-war: Is the web module
  4. swayam-shared: Has the ejb remote interfaces. As the name indicates, its shared by the sawaym-ejb and the swayam-war modules
  5. swayam-ear-builder: Used for building all the modules

I am using Netbeans (6.8) and Glassfish for convinience. But you can pretty much use anything.

I have kept things pretty simple. This is how the Remote interface looks like (its a Stateless Session Bean):

And this is how I access it from the servlet:

Mavenising the EJB

We will use the maven-ejb-plugin for this. This is how the pom looks like:

Note that the packaging is ejb and not jar.

Mavenising the WAR

Mavenising the EAR

The most important thing here is the type tag inside the dependency tag. Without this, it will not work.

Putting it all together

Its often very cumbersome to build these modules one by one manually when one of them changes. This is more so in a development environment. So, I will conclude with one pom for building all the modules at one go. This pom is present in the swayam-ear-builder module.

Note that the path of the modules are relative to the pom.
You can build the EAR project and deploy it on Glassfish or JBoss. Once successfully deployed, you can open http://localhost:8080/swayam-war/EjbInvoker This is how it looks like:

screenshot-servlet-ejbinvoker-google-chrome

Resources

  1. Sources: https://github.com/paawak/blog/tree/master/code/swayam-ear-example
  2. Binaries: https://github.com/paawak/blog/blob/master/code/swayam-ear-example/swayam-ear/dist/

One thought on “Creating an EAR with Maven

  1. Deployment to Glassfish 3.1 fails with java.lang.RuntimeException: EJB Container initialization error.
    Full stacktrace:
    java.lang.RuntimeException: EJB Container initialization error
    at org.glassfish.ejb.startup.EjbApplication.loadContainers(EjbApplication.java:242)
    at org.glassfish.ejb.startup.EjbDeployer.load(EjbDeployer.java:305)
    at org.glassfish.ejb.startup.EjbDeployer.load(EjbDeployer.java:108)
    [#|2013-05-28T16:54:09.593+0300|SEVERE|glassfish3.1.2|javax.enterprise.system.tools.admin.org.glassfish.deployment.admin|_ThreadID=17;_ThreadName=Thread-2;|Exception while loading the app : EJB Container initialization error
    java.lang.RuntimeException: java.lang.RuntimeException: java.lang.ClassNotFoundException: com.swayam.eardemo.shared.MySessionBeanRemote
    at org.glassfish.ejb.security.factory.EJBSecurityManagerFactory.createManager(EJBSecurityManagerFactory.java:219)
    at org.glassfish.ejb.startup.EjbApplication.loadContainers(EjbApplication.java:229)
    at org.glassfish.ejb.startup.EjbDeployer.load(EjbDeployer.java:305)
    at org.glassfish.ejb.startup.EjbDeployer.load(EjbDeployer.java:108)

Leave a Reply

Your email address will not be published. Required fields are marked *