How to embed Tomcat server in Maven application
This example is a basic web application to embed Tomcat server in you Maven
application. Basically use of tomcat plug in in your application . Tomcat7 plugin2.2
and higher version can be use for this . Here i have application StartWeb
1. Create Maven Project StartWeb .
2. configure pom.xml.
3. Download Maven setpath and run mvn
5. web.xml file configure
6. create you jsp page or html page
7. clean and install , build the project
8, Start tomcat
9, Run your application .

Pom file will look something like this . This is StartWeb pom.xml.
folder structure
---------------------
you can use java 7 lib if get compilation problem.
pom.xml
-------------
<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>
<artifactId>StartWeb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Servlet Library -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager</url>
<server>localhost</server>
<path>/${project.build.finalName}</path>
</configuration>
</plugin>
</plugins>
</build>
<groupId>StartWeb</groupId>
<name>StartWeb</name>
</project>
So need to configure web.xml . This needed when your create web using jsp , servlets ets
you can use simplest formate also for this application.
web.xml
--------
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>StartWeb</display-name>
<servlet>
<servlet-name>welcome</servlet-name>
<servlet-class>/welcome.jsp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
</web-app>
welcome.jsp
--------------
<%@ 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> Startweb</title>
</head>
<body>
<a><b>Welcome User!!!!</b></a>
</body>
</html>
Maven run tomcat
---------------------
Run status
==================
Running application
--------------------------
Shutdown server
-----------------------
For mac simply ctr+c then type command in terminal $ mvn tomcat:shutdown it give build
success.
Or eclipse go ---> run --> run confuguration --> then in goal tomcat:shutdown similarly you can start too.
How to embed Tomcat server in Maven application
Reviewed by Mukesh Jha
on
12:48 AM
Rating:

No comments:
Add your comment