Maven – Build Web Application And Deploy To Tomcat Server

 

Prerequisites

Step 1 Create New Maven-based Dynamic Web Project

  • Go to File->New->Others… Select Dynamic Web Project under Web category then
  • Click Next.
  • Choose any project name you want; then click Finish.
  • Right click to the project; select Configure->Convert to Maven Project.
  • Eclipse will generate Project Object Model (pom.xml) file. Open the file by Eclipse text editor then paste the content below.

Step 2 Servlet Class

Create a java class that extends javax.servlet.HttpServlet class.

  • Right click to the project or src folder, select New -> Class from the menu.
  • In Java package, enter info.java.tips.servlet.
  • In Class Name, type HttpServletDemo or any name you want.
  • Click Finish.
  • Add extends HttpServlet to the class.
  • Implement doGet() method.

Step 3 Configuring Servlet

From servlet 3.0 you can register servlet in web.xml or annotating the servlet with @WebServlet. In this example, I configure the servlet using web.xml which is located in the WEB-INF directory of your Web application.

For each servlet, servlet-name must be the same for <servlet> & <servlet-mapping> elements.

Step 4 Configure Log4j

Create an empty file named log4j.properties under WEB-INF\classes folder; and paste the content below:

Step 5 Configure Maven Runtime Environment

  • Right click to the project; select Run As -> Run Configurations
  • Right click to Maven Build; select New
  • Click Browse Workspace…
  • Select the project then click Ok.
  • Select JRE tab
  • Click Installed JREs… button to add/remove or edit JRE definition.
  • Click Add button.
  • Select Standard VM; then click Next.
  • Click Directory button to select the root folder of JDK installation. Select the folder then click Ok.
  • Click Finish.
  • Check the JDK then click Ok.
  • CLick Apply.
  • Click Close.

Note: you can see some compilation errors in your code as in this example; I don’t configure build path for the project. So you still can build the project using maven without any problem.

Step 5 Build The Project With Maven

  • Right click to the project; select Run As -> Maven build…
  • Enter clean package in goals field.
  • Click Run.

Console window output

Step 6 Deploy The Application

Step 6.1 Deploy the package to tomcat server

There’re 2 ways to do this:

  1. Rename the MavenWeb-0.0.1.war file to MavenWebDemo.war and copy the file to {tomcat home}/webapps folder. Tomcat will automatically unpack the web folder with name same as war package.
  2. Or copy all sub folders & files in MavenWeb-0.0.1 folder to {tomcat home}/webapps/MavenWebDemo folder.

Step 6.2 Configure Context

  • Open server.xml in {tomcat home}/conf
  • Under <Host> element, add context definition

docBase refers to the web folder name in {tomcat home}/webapps folder. And path is the context path to access from web browser.

Step 7 Start Tomcat Server

You can start tomcat server by running script or start tomcat service.

Step 8 Run The Servlet

We assume that you install tomcat on default port (8080) and context path is MavenWebDemo. Open web browser and type:

http://localhost:8080/MavenWebDemo/httpServletDemo

Output

Also check the log file that’s configure in log4j.properties for log information.

Note: you can see some compilation errors in your code as in this example; I don’t configure build path for the project. So you still can build the project using ant without any problem.

Project Structure

Deployment Package Structure

Note: you can use maven command:

or 

To generate the WAR file target/MavenWeb-0.0.1.war. Here are the contents of that WAR file