Start a Java EE application with Maven 

Joined:
08/13/2009
Posts:
172

September 07, 2009 20:44:15    Last update: November 03, 2011 14:43:19
Step 1: Repackage a web app as EAR
A Java EE application is a multimodule Maven project. At the very least you'll need to package a WAR and an EAR. To get started, I'll simply re-package the simple webapp as an EAR.

  1. Create a directory named javaee-app

  2. Copy the webapp from here to javaee-app. Rename struts1app to webapp.

  3. Create pom.xml under javaee-app:
    <project>
       <modelVersion>4.0.0</modelVersion>
       <groupId>maven-tutorial</groupId>
       <version>1.0</version>
       <artifactId>java-ee</artifactId>
       <packaging>pom</packaging>
       <name>Java EE Example</name>
    
       <modules>
          <module>ear</module>
          <module>webapp</module>
       </modules>
    </project>
    


  4. Create a directory named ear under javaee-app. Create pom.xml under ear:
    <project>
       <modelVersion>4.0.0</modelVersion>
       <groupId>maven-tutorial.java-ee</groupId>
       <artifactId>ear</artifactId>
       <packaging>ear</packaging>
       <version>1.0</version>
       <name>Java EE EAR Example</name>
    
       <parent>
          <groupId>maven-tutorial</groupId>
          <artifactId>java-ee</artifactId>
          <version>1.0</version>
       </parent>
    
       <dependencies>
          <dependency>
             <groupId>maven-tutorial.java-ee</groupId>
             <artifactId>webapp</artifactId>
    	 <version>1.0</version>
             <type>war</type>
          </dependency>
       </dependencies>
    </project>
    


  5. Modify pom.xml in the webapp directory so that it looks like this:
    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>maven-tutorial.java-ee</groupId>
      <artifactId>webapp</artifactId>
      <packaging>war</packaging>
      <version>1.0</version>
      <name>Java EE webapp</name>
    
      <parent>
        <groupId>maven-tutorial</groupId>
        <artifactId>java-ee</artifactId>
        <version>1.0</version>
      </parent>
    
      <dependencies>
        <dependency>
    	<groupId>javax</groupId>
    	<artifactId>javaee-api</artifactId>
    	<version>6.0-SNAPSHOT</version>
    	<scope>provided</scope>
        </dependency>
    
        <dependency>
    	<groupId>struts</groupId>
    	<artifactId>struts</artifactId>
    	<version>1.2.9</version>
        </dependency>
      </dependencies>
    </project>
    


  6. Build with "mvn package" in the javaee-app directory. You can see that ear-1.0.ear is successfully generated in javaee-app/ear/target.

    Maven successfully resolves dependencies between the sub-projects. However, if you run "mvn package" from inside javaee-app/ear, the dependency on webapp cannot be resolved!

  7. As part of the packaging process, Maven generates application.xml with default values for application "display name" and web context root, etc. You can add the following section to javaee-app/ear/pom.xml to change the values:
    <build>
          <plugins>
             <plugin>
    	    <groupId>org.apache.maven.plugins</groupId>
    	    <artifactId>maven-ear-plugin</artifactId>
    	    <configuration>
    	       <displayName>Java EE Application</displayName>
    	       <modules>
    	           <webModule>
    		      <groupId>maven-tutorial.java-ee</groupId>
    		      <artifactId>webapp</artifactId>
    		      <contextRoot>/the-context-root</contextRoot>
    		   </webModule>
    	       </modules>
    	    </configuration>
    	 </plugin>
          </plugins>
       </build>
    

    More details are available from http://maven.apache.org/plugins/maven-ear-plugin/.

    Alternatively, you can create directory javaee-app/ear/src/main/application/META-INF and put your custom application.xml file there.


Step 2: Add an EJB
  1. Create a new directory named ejb under javaee-app. Create pom.xml under ear:
    <project>
       <modelVersion>4.0.0</modelVersion>
       <groupId>maven-tutorial.java-ee</groupId>
       <artifactId>ejb</artifactId>
       <packaging>ejb</packaging>
       <version>1.0</version>
       <name>Java EE ejb</name>
    
       <parent>
          <groupId>maven-tutorial</groupId>
          <artifactId>java-ee</artifactId>
          <version>1.0</version>
       </parent>
    </project>
    


  2. Create directory src/main/java/org/xinotes/ejb under javaee-app. Create a stateless session bean interface and implementation under the newly created directory:
    Hello.java
    package org.xinotes.ejb;
    
    public interface Hello {
        public String sayHello(String name);
    }
    


    HelloBean.java
    package org.xinotes.ejb;
    
    import javax.ejb.Stateless;
    import javax.ejb.Local;
    
    @Stateless
    @Local({Hello.class})
    public class HelloBean implements Hello {
        public HelloBean() {
        }
    
        public String sayHello(String name) {
    	return "Hello " + name + "!";
        }
    }
    


  3. Modify the pom.xml under javaee-app to include the EJB module (note that I also moved the Java EE API dependency here and added some options for the compiler and ejb plugins):
    <project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>maven-tutorial</groupId>
        <version>1.0</version>
        <artifactId>java-ee</artifactId>
        <packaging>pom</packaging>
        <name>Java EE pom</name>
    
        <modules>
    	<module>ear</module>
    	<module>webapp</module>
    	<module>ejb</module>
        </modules>
    
        <dependencies>
    	<dependency>
    	    <groupId>javaee</groupId>
    	    <artifactId>javaee-api</artifactId>
    	    <version>5</version>
    	    <scope>provided</scope>
    	</dependency>
        </dependencies>
    
        <build>  
          	<plugins>  
    	    <plugin>  
    	      	<artifactId>maven-compiler-plugin</artifactId>  
    	    	<configuration>  
    	    	    <source>1.5</source>  
    	    	    <target>1.5</target>  
    	    	</configuration>  
    	    </plugin>           
    
    	    <plugin>
    		<groupId>org.apache.maven.plugins</groupId>
    		<artifactId>maven-ejb-plugin</artifactId>
    		<configuration>
    		    <ejbVersion>3.0</ejbVersion>
    		</configuration>
    	    </plugin>
    	</plugins>
        </build>
    </project>
    


  4. Add the ejb as a dependency for the ear (pom.xml under javaee-app/ear):
    <project>
       <modelVersion>4.0.0</modelVersion>
       <groupId>maven-tutorial.java-ee</groupId>
       <artifactId>ear</artifactId>
       <packaging>ear</packaging>
       <version>1.0</version>
       <name>Java EE ear</name>
    
       <parent>
          <groupId>maven-tutorial</groupId>
          <artifactId>java-ee</artifactId>
          <version>1.0</version>
       </parent>
    
       <dependencies>
          <dependency>
             <groupId>maven-tutorial.java-ee</groupId>
             <artifactId>webapp</artifactId>
    	 <version>1.0</version>
             <type>war</type>
          </dependency>
    
          <dependency>
    	 <groupId>maven-tutorial.java-ee</groupId>
             <artifactId>ejb</artifactId>
             <version>1.0</version>
             <type>ejb</type>
          </dependency>
       </dependencies>
    </project>
    


  5. Add the ejb as a dependency for the webapp (pom.xml under javaee-app/webapp):
    <project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>maven-tutorial.java-ee</groupId>
        <artifactId>webapp</artifactId>
        <packaging>war</packaging>
        <version>1.0</version>
        <name>Java EE webapp</name>
    
        <parent>
    	<groupId>maven-tutorial</groupId>
    	<artifactId>java-ee</artifactId>
    	<version>1.0</version>
        </parent>
    
        <dependencies>
            <dependency>
    	    <groupId>maven-tutorial.java-ee</groupId>
    	    <artifactId>ejb</artifactId>
    	    <version>1.0</version>
    	    <type>ejb</type>
    	</dependency>
    
    	<dependency>
    	    <groupId>struts</groupId>
    	    <artifactId>struts</artifactId>
    	    <version>1.2.9</version>
    	</dependency>
        </dependencies>
    </project>
    


  6. Modify the struts action (under webapp/src/main/java/org/xinotes/) to make use of the stateless session bean:
    HelloAction.java
    package org.xinotes;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.ejb.EJB;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    
    import org.apache.struts.action.*; 
    
    import org.xinotes.ejb.Hello;
    
    public class HelloAction extends Action {
        Hello helloEjb;
    
        public HelloAction() throws NamingException {
            Context ctx = new InitialContext();
            helloEjb = (Hello) ctx.lookup("HelloBeanLocal");
        }
    
        public ActionForward execute(ActionMapping map,
    				 ActionForm form,
    				 HttpServletRequest req,
    				 HttpServletResponse resp)
    			 throws IOException, 
                                    ServletException {
    	HelloForm f = (HelloForm) form;
    	String message = helloEjb.sayHello(f.getName());
    	req.setAttribute("message", message);
    	return map.findForward("hello");
        }
    }
    


  7. Build with "mvn package" in the javaee-app directory.

  8. Note that the struts action code above assumes that the JNDI name for the Hello bean is HelloBeanLocal. You may need to add a proprietary deployment descriptor to bind the EJB to that name. Simply create the directory ejb/src/main/resources/META-INF and put the deployment descriptor there. For OC4J the file is orion-ejb-jar.xml, and here's the content:
    <?xml version="1.0" encoding="utf-8"?>
    
    <orion-ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://xmlns.oracle.com/oracleas/schema/orion-ejb-jar-10_0.xsd" deployment-version="10.1.3.3.0" deployment-time="123d598f9ca" schema-major-version="10" schema-minor-version="0" >
          <enterprise-beans>
                <session-deployment name="HelloBean" location="HelloBean" local-location="HelloBeanLocal" persistence-filename="HelloBean.test_default_group_1">
                </session-deployment>
          </enterprise-beans>
          <assembly-descriptor>
                <default-method-access>
                      <security-role-mapping name="&lt;default-ejb-caller-role&gt;" impliesAll="true" />
                </default-method-access>
          </assembly-descriptor>
    </orion-ejb-jar>
    

Share |
| Comment  | Tags