How to Embed Tomcat within Maven Project – Run Tomcat with Maven

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.

mvn clean install

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

mvn tomcat7:run

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.

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building HelloWorld Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ HelloWorld >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HelloWorld ---
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ HelloWorld ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) < process-classes @ HelloWorld <<<
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ HelloWorld ---
[INFO] Running war on http://localhost:8080/HelloWorld
[INFO] Using existing Tomcat server configuration at C:\workspace\test\HelloWorld\target\tomcat
[INFO] create webapp with contextPath: /HelloWorld
Jun 15, 2016 3:10:40 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jun 15, 2016 3:10:40 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Jun 15, 2016 3:10:40 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Jun 15, 2016 3:10:52 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]

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

embedded-tomcat-maven-plugin

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
Get our Articles via Email. Enter your email address.

You may also like...

3 Comments

  1. Dinesh Krishnan says:

    Thank you for sharing its very good.

  2. devops online training says:

    Wow. That is so elegant and logical and clearly explained. Keep it up! I follow up your blog for future post.

  3. Rajesh K says:

    I followed the same, after running mvn tomcat7:run i got the screen as shown in the example. But when i try to run in browser i’m getting the following message. “This site can’t be reached
    localhost refused to connect.” Please let me know what might be the issue here.

Leave a Reply

Your email address will not be published. Required fields are marked *