Open In App

Embedding Tomcat Server in Maven Project

Last Updated : 08 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Today, developing an application using different servers depending on the environment that is to be used is a tricky job. Applications may work fine on one server and may end up not working properly on the other server which is a problematic one. What if the developer had the privilege to use or configure a server for a development purpose without even downloading the same, which really reduces a lot of manual work and hence, time-saving can be noticed?

Tomcat Server

Tomcat is one of the best web servers and an open-source Java Servlet container developed by Apache Software Foundation (ASF), which is used by many developers for their project works. It has sublimed to such a greater extent just because of its classicality and its speed and implements J2EE specifications which majorly include servlets, JSP, and expression languages. With the availability of these vital features, the Tomcat server is still one of the best web servers available in the market. Consider a scenario of using Tomcat Server without even downloading and installing it. Yes, this is possible and is very easy to handle through the use of the powerful tool “Maven“. Maven provides a graceful plugin called tomcat7-maven-plugin through which the Tomcat Web Server can be embedded seamlessly into a Maven project.

Maven

Maven is one of the most famous build automation used for Java projects. The 2 key aspects of Maven are, first, how the project is built/developed, and second is, dependencies associated with the Java project which uses Maven build tool. The required libraries in the form of dependencies will be downloaded by the Maven tool from the central repository and the same will be persisted in the local cache. Maven is developed based on the plugin-based architecture and hence makes it easier to control the project through the utilization of standard inputs.

Steps to configure Tomcat Server in a Maven Project

Step 1: Create a simple maven project as shown below:

tomcat-1

Step 2: Open the pom.xml file and add the below-mentioned plugin entry.

XML
<plugin> 
    <groupId>org.apache.tomcat.maven</groupId> 
    <artifactId>tomcat7-maven-plugin</artifactId> 
    <version>2.2</version> 
    <configuration> 
        <port>9090</port> 
    </configuration>
</plugin>

Step 3: Now lets just start using it. Run the command mvn clean install in order to compile the project with the tomcat plugin.

tomcat-11

Step 4: Now run the application by executing the command mvn tomcat7:run, which will start the tomcat server.

tomcat-2

Step 5: Now click on the run button. Open the browser and enter the URL: http://localhost:9090/EmbeddedTomcat

tomcat-3



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads