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 portscontextFile
– The path of the Tomcat context XML file.hostName
– configure host namehttpsPort
– 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 numberpath
– The webapp context path to use for the web applicationwarFile
– 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 !!
Leave a Reply