How To Run Embedded Tomcat with Maven

4 years ago by in How To, Maven, Tomcat, Web Application Tagged: , , , , ,

Java web application require a web container, such as Tomcat (homepage), on which these can run. Installing and configure a web container on each developing machine may be time consuming. Furthermore, other developers need to manage the dependencies manually if they want to run the web application.

Maven has a tomcat plugin that allows us to run an embedded tomcat instance without the need of installing a local tomcat server.

<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>2.2</version>
</plugin>

Please note that this example makes use version 7 of Tomcat. Please update where necessary if using another version of Tomcat.

All code listed below is available at: http://java-creed-examples.googlecode.com/svn/maven/How To Run Embedded Tomcat with Maven/. Most of the examples will not contain the whole code and may omit fragments which are not relevant to the example being discussed. The readers can download or view all code from the above link.

Include the Tomcat Maven Plugin as shown in the following POM example.

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.javacreed.examples</groupId>
  <artifactId>how-to-run-embedded-tomcat-with-maven</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>How To Run Embedded Tomcat with Maven</name>
  <url>http://www.javacreed.com/how-to-run-embedded-tomcat-with-maven/</url>
  <packaging>war</packaging>
  <organization>
    <name>Java Creed</name>
    <url>http://www.javacreed.com/</url>
  </organization>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <port>9090</port>
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>
  
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
</project>

Please note that the above configuration will start the embedded Tomcat instance on port 9090. You can change the port as required.

Build the project

mvn clean install

Start the embedded Tomcat server.

mvn tomcat7:run

This will produce something similar to the following.

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building How To Run Embedded Tomcat with Maven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) @ how-to-run-embedded-tomcat-with-maven >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ how-to-run-embedded-tomcat-with-maven ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ how-to-run-embedded-tomcat-with-maven ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) @ how-to-run-embedded-tomcat-with-maven <<<
[INFO]
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ how-to-run-embedded-tomcat-with-maven ---
[INFO] Running war on http://localhost:9090/
[INFO] Creating Tomcat server configuration at C:\JavaCreed\examples\maven\How To Run Embedded Tomcat with Maven\target\tomcat
[INFO] create webapp with contextPath:
Mar 20, 2014 4:00:05 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-9090"]
Mar 20, 2014 4:00:05 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Mar 20, 2014 4:00:05 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Mar 20, 2014 4:00:07 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-9090"]

Please note that when executed for the first time, Maven may download any missing components and thus may produce more output than that shown above.

Open the browser.

http://localhost:9090/hello

You should see something similar to the following

Embedded Tomcat

Embedded Tomcat

We managed to run a Java web application without having to install a web server such as Tomcat.

Albert Attard

Albert Attard is a Java passionate and technical lead at a research group. You can find him on . Over the past years Albert worked on various Java projects including traditional server/client applications, modular applications, large data handling applications and concurrent data manipulation applications to name a few. He has a BSc degree from the University of London (Homepage) and an MSc Information Security with the same university. His MSc thesis (Book) received the 2012 SearchSecurity.co.UK award (Website).

10 Responses to “How To Run Embedded Tomcat with Maven”


Reddy
July 9, 2014 Reply

Nice post.. Thanks!

Pablo
September 6, 2014 Reply

Thank you!

RFCL
December 14, 2015 Reply

Thanks a lot, you saved my day!

amy
December 15, 2015 Reply

how do you configure the memory in here; say I want to run with a larger heap size of 1024m

amy
December 15, 2015 Reply

How do you change the memory settings? I don’t want to run with the default settings?

Albert Attard Albert Attard
January 5, 2016 Reply

Hi,

Never did that myself. I suggest you have a look at the plugin simple website: https://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/ for more information about how to further customise this plugin.

Albert

Kinjal
December 29, 2015 Reply

I added the same dependency but still tomcat is not starting. I am getting 404 error while I enter the url mentioned http://localhost:9090/hello

Albert Attard Albert Attard
January 5, 2016 Reply

Hi,

There can be several issues.

  1. Check that the path http://localhost:9090 (without the application context name hello) works
  2. Make sure that the application context name is actually hello
  3. Make sure that the application has a default/welcome page

Hope this helps you solve the problem.

Tribhuwan Kumar Yadav
May 27, 2016 Reply

Nice post! I have been wondering what’s the use of tomcat7:run, when already we have tomcat configured….and able to run the application on server…

Rocki Docki
March 13, 2018 Reply

Thanx, still helpfull in 2018!

Leave a Comment


Time limit is exhausted. Please reload the CAPTCHA.