JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

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"]

.