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

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



via ViralPatel.net http://ift.tt/1VYXqsm

No comments:

Post a Comment

If you have any question please let me know

Microsoft 365 (Office) Insider Preview Build 17715.20000 (Version 2406) Released, Here is What’s New and Fixed

UPDATE: Microsoft 365 Insider (previously known as Office Insider) Preview build 17715.20000 (version 2406) is available for download and in...