Getting Started with Maven | Miscellaneous | Running Tomcat to Deploy an App
This article explains how to run Tomcat for a Maven project with the Apache Tomcat Maven Plugin.
Goals
| Goal | Description |
|---|---|
| tomcat7:deploy | Deploy a WAR to Tomcat. |
| tomcat7:run | Runs the current project as a dynamic web application using an embedded Tomcat server. |
| tomcat7:help | Display help information on tomcat7-maven-plugin. Call mvn tomcat7:help -Ddetail=true -Dgoal=<goal-name> to display parameter details. |
Settings
If no explicit configuration is provided, tomcat7 runs with the following default settings.
- Tomcat manager URL of http://localhost:8080/manager
- Authentication details of username admin and no password
- Context path of /${project.artifactId}
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
...
</plugins>
</build>
...
</project>
If you changed the artifact file name with finalName, change the path in configuration as follows.
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/${project.build.finalName}</path>
</configuration>
</plugin>
Usage
Run the following goal to deploy the current project through the embedded Tomcat.
$ mvn tomcat7:run
Context URL
The context URL is the same as the artifactId. For example, if it is testApp, you can access http://localhost:8080/testApp to check the deployed content. If you changed the artifact with finalName, for example to webApp.war, access it with the same context as finalName, such as http://localhost:8080/webApp. To map the context to /, refer to the configuration below.
Configuration
You can change the HTTP port or AJPPort used by Tomcat.
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path> <!-- default artifactId -->
<port>18080</port> <!-- default 8080 -->
<ajpPort>18009</ajpPort> <!-- default 8009 -->
<httpsPort>18443</httpsPort> <!-- default 8443 -->
<contextReloadable>true</contextReloadable>
<keystoreFile>${user.home}/.keystore</keystoreFile>
<keystorePass>changeit</keystorePass>
</configuration>
</plugin>
You can also configure it at runtime with properties in the form maven.tomcat.xxxx. For configurable properties, refer to http://mojo.codehaus.org/tomcat-maven-plugin/run-war-only-mojo.html.
| property | Default value | Notes |
|---|---|---|
| port | 8080 | |
| ajpPort | 8009 | |
| httpsPort | 8443 | |
| contextReloadable | true | |
| keystoreFile | none | Keystore containing the certificate for HTTPS |
| keystorePass | none | Keystore containing the private key for HTTPS |
| path | artifactId | Context path |
Pass the above settings as properties at runtime.
$ mvn tomcat7:run -Dmaven.tomcat.port=18080 -Dmaven.tomcat.ajp.port=18009 -Dmaven.tomcat.httpsPort=18443 -Dmaven.tomcat.contextReloadable=true -Dmaven.tomcat.path="/"
Loading Required Libraries
To automatically load libraries that must be placed in TOMCAT_HOME/lib, such as JDBC or JCE providers, add a <dependency> to the <plugin> element as follows.
(http://stackoverflow.com/questions/9928829/tomcat7-maven-plugin-extradependency-seems-not-being-loaded)
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<!-- Write libraries that must be placed in Tomcat's lib folder -->
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.4-701.jdbc4</version>
</dependency>
</dependencies>
</plugin>
Reference
https://www.lesstif.com/pages/viewpage.action?pageId=14090451