Creating an EAR with Maven

Posted by {"name"=>"Palash Ray", "email"=>"paawak@gmail.com", "url"=>"https://www.linkedin.com/in/palash-ray/"} on August 21, 2010 · 4 mins read

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):

@Remote
public interface MySessionBeanRemote {
    String sayHello();
}

And this is how I access it from the servlet:

...
MySessionBeanRemote remoteBean = InitialContext.doLookup(MySessionBeanRemote.class.getName());
...

Mavenising the EJB

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


    4.0.0
    com.swayam.eardemo
    swayam-ejb
    1.0.0
    ejb
    swayam-ejb
    
        ${basedir}/src
        
            
                org.apache.maven.plugins
                maven-jar-plugin
            
            
                maven-ejb-plugin
                
                    3.0
                    
                        
                            true
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.6
                    1.6
                
            
        
    
    
        
            javax
            javaee-api
            6.0
            provided
        
        
            com.swayam.eardemo
            swayam-shared
            1.0.0
        
    

Note that the packaging is ejb and not jar.

Mavenising the WAR


    4.0.0
    com.swayam.eardemo
    swayam-war
    1.0.0
    war
    swayam-war
    
        
            maven.java.net
            Java.net Maven2 Repository
            http://download.java.net/maven/2
        
    
    
        swayam-war
        ${basedir}/src/java
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.6
                    1.6
                
            
            
                maven-war-plugin
                2.0
                
                    
                        
                            ${basedir}/web
                            
                                CVS/**
                                WEB-INF/lib/*
                            
                        
                    
                
            
        
    
    
        
            com.swayam.eardemo
            swayam-shared
            1.0.0
        
        
            javax.servlet
            servlet-api
            2.4
            provided
        
    

Mavenising the EAR


    4.0.0
    com.swayam.eardemo
    swayam-ear
    1.0.0
    ear
    swayam-ear
    
        
            maven.java.net
            Java.net Maven2 Repository
            http://download.java.net/maven/2
        
    
    
        swayam-ear
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.6
                    1.6
                
            
            
                maven-resources-plugin
                2.3
                
                    UTF-8
                
            
            
                maven-ear-plugin
                2.4.1
                
                    
                        
                            com.swayam.eardemo
                            swayam-war
                            swayam-war.war
                            /swayam-war
                        
                        
                            com.swayam.eardemo
                            swayam-ejb
                            swayam-ejb.jar
                        
                    
                
            
        
    
    
        
            com.swayam.eardemo
            swayam-war
            ${swayam-version}
            war
        
        
            com.swayam.eardemo
            swayam-ejb
            ${swayam-version}
            ejb
        
    
    
        1.0.0
    

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.


  4.0.0
  com.swayam.eardemo
  swayam-ear-builder
  pom
  1.0.0
  swayam-ear-builder
  
    ../swayam-ear
    ../swayam-ejb
    ../swayam-war
    ../swayam-shared
  

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/