2016-06-15

Tomcat is a developers choice container. The most popular and loved Java container that everyone loves because of its simplicity and speed. This can be attributed to the ease of installation and configuration while running a tomcat server. Once the server is installed, it is like piece of cake running it through Eclipse or as a windows service.

Wouldn’t it be great to use tomcat without installing tomcat all together? This is exactly what you can achieve thanks to Maven. Maven has a wonderful plugin tomcat7-maven-plugin that we can use to embed tomcat within our Java web application. The idea is to create standalone web project that can start tomcat server and run on its own. All we need to do is the add following maven plugin in your pom.xml plugins list.

And once this is done all we have to do is to clean build the project using maven.

And then running maven tomcat7:run goal which will download the dependencies and trigger the tomcat7-maven-plugin.

And that’s it. Once we fire this tomcat7:run goal, maven will start its magic. First all related dependencies will be downloaded, the webapp will be compiled and packaged and then tomcat7 maven plugin will deploy the webapp and start the embedded server.

You will have something similar to following.

And then just open your web applications link http://localhost:8080/ in your favorite browser.



Change the Port number

By default the embedded tomcat will try to initialize at port 8080. We can change this by passing port number in configuration.

Using different context path

Also, by default the web context for the application will be our maven projects artifact id. We can change the path or web context by setting new path in configuration.

Following is the sample pom.xml for reference.

pom.xml

The post How to Embed Tomcat within Maven Project – Run Tomcat with Maven appeared first on ViralPatel.net.

Related Articles

Creating Dynamic Web Project using Maven in Eclipse

Run Multiple Instance of Apache Tomcat in Single Server

Setting Tomcat Heap Size (JVM Heap) in Eclipse

Introduction to Apache Maven: A build framework & build automation tool

Maven: How to Remove/Ignore Transitive Dependency

Implement LDAP authentication in Tomcat & JBoss server for Java app

Database Connection Pooling in Tomcat using Eclipse

Show more