Tomcat Maven Plugin Example

In this maven tutorial, learn to add and configure tomcat plugin to pom.xml and use it to deploy the web application without any tomcat installation in the machine.

It is very useful when we want to test the application in our local machine where the actual tomcat installation is not available due to any reason.

Check for the latest supported version of tomcat and the plugin version on this page.

1. Adding Tomcat Maven Plugin

At the time of writing this post, the latest plugin is tomcat7-maven-plugin.

There are other plugins tomcat8-maven-plugin and tomcat9-maven-plugin available in the maven repository but those are not from the official apache group.

Edit project’s pom.xml file and plugin entry inside build tag.

<build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
 
        <!-- Tomcat plugin-->
 
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <port>9000</port>   //Configure port number
                <path>/spring5-webmvc-demo</path>   //Configure application root URL
            </configuration>
        </plugin>
 
    </plugins>
</build>

2. Plugin Configuration Options

We can add the tomcat plugin in various ways within configuration tag. Some useful configuration options are:

  • address – This IP address will be used on all ports
  • contextFile – The path of the Tomcat context XML file.
  • hostName – configure host name
  • httpsPort – The https port to run the Tomcat server on.
  • keystoreFile – Override the default keystoreFile for the HTTPS connector (if enabled)
  • keystorePass – Override the default keystorePass for the HTTPS connector (if enabled)
  • mainClass – Main class to use for starting the standalone jar.
  • systemProperties – List of System properties to pass to the Tomcat Server.
  • port – Custom port number
  • path – The webapp context path to use for the web application
  • warFile – The path of the WAR file to deploy.

3. Running the Application with Tomcat Plugin

To run the application with tomcat maven plugin, use maven goal as –

mvn tomcat7:run

When we run above maven goal, we can see tomcat starting in console log with default port 8080.

[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ spring5-webmvc-demo >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ spring5-webmvc-demo ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ spring5-webmvc-demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) < process-classes @ spring5-webmvc-demo <<<
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ spring5-webmvc-demo ---
[INFO] Running war on http://localhost:8080/spring5-webmvc-demo
[INFO] Using existing Tomcat server configuration at D:\java9\workspace\spring5-webmvc-demo\target\tomcat
[INFO] create webapp with contextPath: /spring5-webmvc-demo
---
---
INFO: Starting ProtocolHandler ["http-bio-8080"]

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

3 thoughts on “Tomcat Maven Plugin Example”

  1. I am getting the following Error:
    Could not start Tomcat: Failed to start component [StandardServer[-1]]: Failed to start component [StandardService[Tomcat]]: Failed to start component [StandardEngine[Tomcat]]: A child container failed during start

    Reply
  2. How can i use this tomcat maven plugin in multi module project.

    Main pom.xml file 3 modules. main pom has packaging pom and in children modules: one has packaging war and other two has packaging jar. These jar modules are dependencies for war module.

    How can i use tomcat maven plugin to run this project locally?

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.