ZOFTINO.COM android and web dev tutorials

Running Maven Web Application on Tomcat in Eclipse

This tutorial explains how to create a Maven web application and build war in eclipse and deploy it to Tomcat from Eclipse. For this, we need to install latest version of JDK, eclipse, eclipse maven plug-in, and eclipse tomcat plug-in.

You can use maven within the eclipse using eclipse maven plug-in so that you can get full benefits of using maven for your web applications.

Tomcat eclipse plug-in is used to integrate tomcat and eclipse. This plug-in is used, so you can use latest version of tomcat, for example current version tomcat 9, from eclipse.

Table of Contents

Eclipse and Maven Setup

For Java, eclipse and eclipse maven plug-in setup, please see eclipse installation and using eclipse maven plug-in tutorial.

Creating Maven Web Application in Eclipse

To create maven web application project in eclipse, click file > new > other.

create maven web application project in eclipse

Then select maven project option.

select maven project option

Then click next.

create maven project eclipse project name location

Then choose maven archetype webapp.

eclipse maven select archetype webapp

Enter group id, artifact id, and package and click finish.

enter artificat id group id package

Missing main and test Maven Web Application Folders

If created web app project doesn’t have main/java and test/java folders like for the web app project structure shown in the following image, that means project is using wrong version of JRE system library.

eclipse maven

Go to project properties and build path, you will see two build path entries missing message as shown in the following image.

eclipse maven webapp missing main test java folders

To fix this issue, you need to add JRE library from JDK by clicking libraries tab, removing JRE system library and adding the JRE system library by choosing the latest JDK.

eclipse maven webapp missing main test java folders jre system library

After adding latest JDK, you will see the main and test java folders in the project structure.

eclipse maven webapp project structure

Install Tomcat Eclipse Plug-in

To install Tomcat eclipse plug-in, click Help menu, then click Eclipse Marketplace menu item and search for tomcat. In the search results, you will get Eclipse Tomcat plug-in item with install option which you need to click.

eclipse marketplace install eclipse tomcat plugin

Download Tomcat

Download latest version of Tomcat from apache tomcat download page.

After downloading tomcat zip file, extract it to a folder.

Configuring Eclipse Tomcat Plug-in

To configure eclipse tomcat plug-in, go to Window > Preferences and select Tomcat option.

eclipse preferences tomcat

Then select tomcat version, set tomcat home to the directory where tomcat is installed and then click apply.

eclipse preferences tomcat select version 9 and set tomcat home

Build Web Application

To compile, test and package your web application, first go to run configuration as shown in the following picture.

eclipse run configurations

In the run configurations window, double click maven.

eclipse run configurations maven build

Then enter run configuration name, select project base directory by clicking work space, enter maven goal and click apply.

eclipse run configurations maven build configuration

Then click run to package your project to create a war file. Here is the console output from the maven build.

eclipse maven web app build output

Deploying War to Tomcat

Once build is done, you need to refresh target folder in your web application project to see the war file which is created after running the maven build.

start stop tomcat from eclipse

Right click the war file, select export menu item, select file system option in the window and click next. Then in the next window select to-directory to point to webapps directory of your tomcat installation and click finish.

export war or exploded war folder from maven build to tomcat webapps directory

After exporting war file to tomcat webapps directory, start tomcat from eclipse by clicking start tomcat menu icon shown in the previous image.

start tomcat from eclipse and deploy war

Form the console, you can get the port number and then access your web application.

eclipse tomcat maven web app example