03 June 2015

Introduction

In real life maven projects, most of the cases you will see multi modules build done by Maven. In this example, I have created one child web project (ChildWebProject), one java project (ChildJavaProject) and a parent project (ParentMavenProject). Parent project will include the web project which will include the jar (ChildJavaProject.jar) of the java project. In the ChildJavaProject.jar the servlet class will be packaged to handle web request in this example. So there will be no servlet class in the ChildWebProject.

Steps to write the code

Create all three projects in the workspace at same level as shown below

Parent Maven Project

  • Create ParentMavenProject project and convert into maven project (Right click on the project->Configure->Convert into Maven project) in eclipse
  • pom.xml file should be as shown below. Please go through the inline comments for better understanding. The parent project includes the child projects. All dependencies (jars) to be added in this parent POM and child projects will use the required dependencies without specifying version in the child POM. Whenever version needs to be changed, just change in the parent project. Child projects will automatically inherit them during compilation and packaging.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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.ashish.maven</groupId>
	<artifactId>ParentMavenProject</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<!-- packaging should be "pom" in the parent POM to include child modules -->
	<packaging>pom</packaging>

	<pluginRepositories>
		<pluginRepository>
			<id>maven-repository.dev.java.net</id>
			<name>Java.net Maven 2 Repository</name>
			<url>http://download.java.net/maven/2</url>
		</pluginRepository>
	</pluginRepositories>
	<dependencyManagement>
		<dependencies>
			<!-- All dependencies to be added in the parent pom.xml. Version to be 
				specified here. Child projects will add the required dependencies without 
				version -->
			<dependency>
				<groupId>javax.servlet</groupId>
				<artifactId>servlet-api</artifactId>
				<version>2.3</version>
				<!-- scope "provided" means the jar will be used during compile time 
					but will not get packaged -->
				<scope>provided</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<!-- Included both the child projects to build. Order of dependency should 
		be maintained -->
	<modules>
		<module>../ChildJavaProject</module>
		<module>../ChildWebProject</module>
	</modules>
</project>

Child Java Project

  • Create ChildJavaProject project and convert into maven project (Right click on the project->Configure->Convert into Maven project) in eclipse. Project structure is shown below.
  • pom.xml file should be as shown below. Please go through the inline comments for better understanding.

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

	<!-- Parent pom.xml has to be specified to inherit configuration from parent 
		pom.xml -->
	<parent>
		<groupId>com.ashish.maven</groupId>
		<artifactId>ParentMavenProject</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../ParentMavenProject/pom.xml</relativePath>
	</parent>

	<modelVersion>4.0.0</modelVersion>
	<artifactId>ChildJavaProject</artifactId>
	<pluginRepositories>
		<pluginRepository>
			<id>maven-repository.dev.java.net</id>
			<name>Java.net Maven 2 Repository</name>
			<url>http://download.java.net/maven/2</url>
		</pluginRepository>
	</pluginRepositories>
	<dependencies>
		<!-- Only dependency has to be mentioned. Version not required to specify 
			here. Version will get inherited from pom.xml -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
  • com.ashish.servlet.ServletInOtherModule

package com.ashish.servlet;

import java.io.IOException;

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

public class ServletInOtherModule extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ServletInOtherModule() {
        super();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.sendRedirect("newJSP.jsp");
	}

}

Child Web project

  • Create ChildWebProject project and convert into maven project (Right click on the project->Configure->Convert into Maven project) in eclipse. You can follow this Web Application Build And Deployment Using Maven post. Below is the project structure
  • Create a index.jsp file with the below content. Once you click on the button it will call a servlet (com.ashish.servlet.ServletInOtherModule) which is present in another project (ChildJavaProject.jar)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

	<h3>This is a web project includes a jar from another java project.</h3>
	<form action="ServletInOtherModule" method="post">
		<div>
			Click this <button>Button</button> to call servlet method in another module
		</div>
	</form>
</body>
</html>
  • Create another jsp file called newJSP.jsp which will get called from the above mentioned servlet.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h1>Landed to another page</h1>
</body>
</html>
  • web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
	id="WebApp_ID" version="3.0">
	<display-name>ChildWebProject</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<servlet>
		<servlet-name>ServletInOtherModule</servlet-name>
		<!-- This servlet class belongs to another project which will get packaged 
			into this war -->
		<servlet-class>com.ashish.servlet.ServletInOtherModule</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>ServletInOtherModule</servlet-name>
		<url-pattern>/ServletInOtherModule</url-pattern>
	</servlet-mapping>
</web-app>
  • Your pom.xml should be like this. Please go through the inline comments for better understanding.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<!-- Parent pom.xml has to be specified to inherit configurations from parent 
		pom.xml -->
	<parent>
		<groupId>com.ashish.maven</groupId>
		<artifactId>ParentMavenProject</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../ParentMavenProject/pom.xml</relativePath>
	</parent>

	<modelVersion>4.0.0</modelVersion>
	<artifactId>ChildWebProject</artifactId>
	<packaging>war</packaging>

	<pluginRepositories>
		<pluginRepository>
			<id>maven-repository.dev.java.net</id>
			<name>Java.net Maven 2 Repository</name>
			<url>http://download.java.net/maven/2</url>
		</pluginRepository>
	</pluginRepositories>
	<dependencies>
		<!-- Add ChildJavaProject.jar as the dependency of this project -->
		<dependency>
			<groupId>com.ashish.maven</groupId>
			<artifactId>ChildJavaProject</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.3</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
			<!-- Below plugin is used to deploy the WAR in the tomcat server -->
			<!-- Note the goal for this maven build is tomcat7:deploy -->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.0</version>
				<configuration>
					<url>http://localhost:8080/manager/text</url>
					<server>TomcatServer</server>
					<path>/ChildWebProject</path>
					<update>true</update> <!-- This is to redeploy the WAR -->
					<username>root</username>
					<password>root</password>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Output

Once you deploy the ChildWebProject in tomcat then you can see the below output.



blog comments powered by Disqus
J2EE,SOAP,RESTful,SVN,PMD,SONAR,JaCoCo,HTTP,API,MAVEN,AngularJS,GitHub,LDAP,AOP,ORM,JMS,MVC,AWS,SQL,PHP,H2DB,JDBC