Tomcat maven plugin example

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

It is very useful when you want to test your application in developer’s machines where actual tomcat installation is not available due to any reason.

Plugin’s latest version is 2.2. It has Apache Tomcat7 support.

How to add tomcat maven plugin

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>

Tomcat maven plugin configuration

You 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.

Run application with tomcat plugin

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

mvn tomcat7:run

When you run above maven goal, you 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"]

Drop me your questions in comments section.

Happy Learning !!

Reference : Tomcat Maven Plugin Documentation

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

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

  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?

Comments are closed.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.