Running maven web project with maven tomcat7 plugin

With maven tomcat7 plugin you won't have download,install and configure Tomcat server seperately.Maven can do it for you.Just run:
mvn tomcat7:run
with a tomcat7 maven plugin enabled project and browse to localhost:port/path
In your pom.xml simply put this plugin:
  1. <plugin>
  2. <groupId>org.apache.tomcat.maven</groupId>
  3. <artifactId>tomcat7-maven-plugin</artifactId>
  4. <version>2.0</version>
  5. <configuration>
  6. <server>tomcat-development-server</server>
  7. <port>9966</port> <!--make sure port doesnt conflict with others -->
  8. <path>/SpringMVC</path><!--http://localhost:9966/SpringMVC/ -->
  9. </configuration>
  10. </plugin>

Example spring maven hello world web project pom.xml:
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3.     <modelVersion>4.0.0</modelVersion>
  4.     <groupId>com.mohi</groupId>
  5.     <artifactId>SpringMVC</artifactId>
  6.     <packaging>war</packaging>
  7.     <version>1.0-SNAPSHOT</version>
  8.     <name>SpringMVC Maven Webapp</name>
  9.     <url>http://maven.apache.org</url>
  10.     <dependencies>
  11.         <dependency>
  12.             <groupId>org.springframework</groupId>
  13.             <artifactId>spring-webmvc</artifactId>
  14.             <version>4.0.3.RELEASE</version>
  15.         </dependency>
  16.     </dependencies>
  17.     <build>
  18.         <plugins>
  19.             <plugin>
  20.                 <groupId>org.apache.maven.plugins</groupId>
  21.                 <artifactId>maven-compiler-plugin</artifactId>
  22.                 <version>2.3.2</version>
  23.             </plugin>
  24.             <plugin>
  25.                 <groupId>org.apache.tomcat.maven</groupId>
  26.                 <artifactId>tomcat7-maven-plugin</artifactId>
  27.                 <version>2.0</version>
  28.                 <configuration>
  29.                     <server>tomcat-development-server</server>
  30.                     <port>9966</port>
  31.                     <path>/SpringMVC</path>
  32.                 </configuration>
  33.             </plugin>
  34.         </plugins>
  35.     </build>
  36. </project>

Sample code:
Here is a Spring Helloworld maven with tomcat7 plugin example project SpringHelloworldMavenTomcat7PluginExample.zip
Download ,unzip and run:
mvn clean install tomcat7:run
Browse to localhost:9966/SpringMVC/welcome

No comments:

Post a Comment