Sample J2EE application using maven

In this post I will show you how to set up a sample J2EE application using maven which contains WAR, EJB-JAR and one dependent utility JAR.

  • Create a directory MavenTestProject (You can name it whatever you like) somewhere in your file system. Now onwards I will refer it as $PROJECT_ROOT_FOLDER. Now follow the below steps to create sample j2ee project using maven.

package com.elitejavacoder.common;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

	public static String getFormattedDateString(Date date) {
		String formattedDate = null;

		try {
			if (date != null) {
				formattedDate = DATE_FORMAT.format(date);
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		return formattedDate;
	}
}

  • Create pom.xml file inside $PROJECT_ROOT_FOLDERcommons.

	<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0
	http://maven.apache.org/xsd/maven-4.0.0.xsd">

		<modelversion>4.0.0</modelversion>

		<groupid>com.elitejavacoder</groupid>
		<artifactid>commons</artifactid>
		<packaging>jar</packaging>
		<name>commons</name>
		<version>1.0-SNAPSHOT</version>

	</project>

  • Create stateless session bean TestSessionBean.java inside $PROJECT_ROOT_FOLDERenterprisesrcmainjavacomelitejavacoderejb.

package com.elitejavacoder.ejb;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import java.util.Date;

import com.elitejavacoder.common.DateUtil;

@Stateless
@Local(TestSessionLocal.class)
@Remote(TestSessionRemote.class)
	public class TestSessionBean {

		public TestSessionBean() {
		}

		public String getGreetings(String name) {
			return "Hello " + name + "! Current time is " + DateUtil.getFormattedDateString(new Date());
		}
	}

  • Create remote interface TestSessionRemote.java inside $PROJECT_ROOT_FOLDERenterprisesrcmainjavacomelitejavacoderejb.

	package com.elitejavacoder.ejb;

	public interface TestSessionRemote {
		public String getGreetings(String name);
	}

  • Create local interface TestSessionLocal.java inside $PROJECT_ROOT_FOLDERenterprisesrcmainjavacomelitejavacoderejb.

	package com.elitejavacoder.ejb;

	public interface TestSessionLocal {
		public String getGreetings(String name);
	}

  • Create pom.xml file inside $PROJECT_ROOT_FOLDERenterprise.

	<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0
	http://maven.apache.org/xsd/maven-4.0.0.xsd">

		<modelversion>4.0.0</modelversion>

		<groupid>com.elitejavacoder</groupid>
		<artifactid>enterprise</artifactid>
		<packaging>jar</packaging>
		<name>enterprise</name>
		<version>1.0-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupid>javax</groupid>
			<artifactid>javaee-api</artifactid>
			<version>6.0</version>
		</dependency>

		<dependency>
			<groupid>com.elitejavacoder</groupid>
			<artifactid>commons</artifactid>
			<version>1.0-SNAPSHOT</version>
			<type>jar</type>
		</dependency>
	</dependencies>

		<build>
			<defaultgoal>package</defaultgoal>
			<plugins>
				<plugin>
					<artifactid>maven-ejb-plugin</artifactid>
					<configuration>
						<archive>
						<manifest>
							<addclasspath>true</addclasspath>
						</manifest>
						</archive>
					</configuration>
				</plugin>
			</plugins>  
		</build>
	</project>

  • Create one simple servlet inside $PROJECT_ROOT_FOLDERwebsrcmainjavacomelitejavacoderweb directory.

package com.elitejavacoder.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.elitejavacoder.ejb.TestSessionRemote;

public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String name = "Atul";
			if (request.getParameter("name") != null) {
				name = request.getParameter("name");
			}

			response.setContentType("text/html");
			PrintWriter out = response.getWriter();

			out.println("");
			out.println("");
			out.println(" Greetings ");
			out.println("");
			out.println("");
			out.println("<h1>" + getGreetings(name) + "</h1>");
			out.println("");
			out.println("");
			out.close();
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public String getGreetings(String name) {
		try {
			TestSessionRemote testSessionRemote = (TestSessionRemote)new InitialContext().lookup("java:app/enterprise-1.0-SNAPSHOT/TestSessionBean!com.elitejavacoder.ejb.TestSessionRemote");
			return testSessionRemote.getGreetings(name);
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		return name;
	}
}

  • Create index.jsp inside $PROJECT_ROOT_FOLDERwebsrcmainwebapp directory.

<h2>
	Sample J2EE project using Maven</h2>
	<form action="/testservlet" method="POST">
		Your name: <input name="name" type="text">

		<input value="Submit" type="submit">
	</form>

  • Create web.xml inside $PROJECT_ROOT_FOLDERwebsrcmainwebappWEB-INF directory.

	<web -app="-app" version="3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
		<servlet>
			<servlet -name="-name">TestServlet</servlet>
			<servlet -class="-class">com.elitejavacoder.web.TestServlet</servlet>
		</servlet>

		<servlet -mapping="-mapping">
			<servlet -name="-name">TestServlet</servlet>
			<url -pattern="-pattern">/testservlet</url>
		</servlet>
	</web>

  • Create pom.xml file inside $PROJECT_ROOT_FOLDERweb directory.

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0
	 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	   
	<modelversion>4.0.0</modelversion>

	<groupid>com.elitejavacoder</groupid>
	<artifactid>web</artifactid>
	<packaging>war</packaging>
	<name>web</name>
	<version>1.0-SNAPSHOT</version>
	 
	<dependencies>
		<dependency>
			<groupid>javax</groupid>
			<artifactid>javaee-api</artifactid>
			<version>6.0</version>
		</dependency>
	   
		<dependency>
			<groupid>com.elitejavacoder</groupid>
			<artifactid>enterprise</artifactid>
			<version>1.0-SNAPSHOT</version>
			<type>jar</type>
		</dependency>
	</dependencies>
	 
	<build>
		<defaultgoal>package</defaultgoal>

		<plugins>
			<plugin>
				<groupid>org.apache.maven.plugins</groupid>
				<artifactid>maven-war-plugin</artifactid>
				<version>2.1-alpha-2</version>
				<configuration>
					<webresources>
						<resource>
							<directory>./src/main/webapp</directory>
						</resource>
					</webresources>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

  • Create pom.xml inside $PROJECT_ROOT_FOLDERdeploy directory create deployable EAR file by packaging all the above artifacts.

	<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0
		http://maven.apache.org/xsd/maven-4.0.0.xsd">

		<modelversion>4.0.0</modelversion>

		<groupid>com.elitejavacoder</groupid>
		<artifactid>deploy</artifactid>
		<packaging>ear</packaging>
		<name>deploy</name>
		<version>1.0-SNAPSHOT</version>

		<dependencies>
			<dependency>
				<groupid>com.elitejavacoder</groupid>
				<artifactid>enterprise</artifactid>
				<version>1.0-SNAPSHOT</version>
				<type>ejb</type>
			</dependency>

			<dependency>
				<groupid>com.elitejavacoder</groupid>
				<artifactid>web</artifactid>
				<version>1.0-SNAPSHOT</version>
				<type>war</type>
			</dependency>
		</dependencies>

		<build>
			<defaultgoal>package</defaultgoal>
			<plugins>
				<plugin>
					<artifactid>maven-ear-plugin</artifactid>
					<version>2.4.1</version>
					<configuration>
						<version>6</version>
						<finalname>MavenTestProject</finalname>
						<defaultlibbundledir>lib/</defaultlibbundledir>
						<modules>
							<ejbmodule>
								<groupid>com.elitejavacoder</groupid>
								<artifactid>enterprise</artifactid>
								<bundledir>/</bundledir>
							</ejbmodule>
							<webmodule>
								<groupid>com.elitejavacoder</groupid>
								<artifactid>web</artifactid>
								<contextroot>/</contextroot>
							</webmodule>
						</modules>
					</configuration>
				</plugin>
			</plugins>  
		</build>
	</project>

  • Now create parent pom.xml file inside $PROJECT_ROOT_FOLDER.

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0
	http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelversion>4.0.0</modelversion>

	<groupid>com.elitejavacoder</groupid>
	<artifactid>parent</artifactid>
	<packaging>pom</packaging>
	<name>Maven Test Project</name>
	<version>1.0-SNAPSHOT</version>

	<modules>
		<module>commons</module>
		<module>enterprise</module>
		<module>web</module>
		<module>deploy</module>
	</modules>
</project>

  • Now open command prompt and go to $PROJECT_ROOT_FOLDER. Type mvn clean install and press enter. Once the build is completed successfully project EAR file MavenTestProject.ear will be available inside $PROJECT_ROOT_FOLDERdeploytarget.

    Deploy this MavenTestProject.ear file. Open http://localhost:8080/index.jsp url in you browser, type your name and hit the submit. In response you will get a greetings message with current date and time from application.