Running a web project with Tomcat

Tomcat is a popular open source application server. The Maven Tomcat plugin supports the ability to build and deploy Maven projects in Tomcat. In fact, there are two Maven Tomcat plugins, one for Tomcat 6 and another for Tomcat 7.

Let us look at how to run a web project with Tomcat 7. The steps will be identical for Tomcat 6, except that the plugin would be tomcat6-maven-plugin instead of tomcat7-maven-plugin, and the plugin prefix would be tomcat6 instead of tomcat7.

How to do it...

  1. Open a simple web project (simple-web-project).
  2. Run the following Maven command:
    mvn org.apache.tomcat.maven:tomcat7-maven-plugin:run
    
  3. Observe the result:
    How to do it...
  4. Browse to the deployed webapp by visiting http://localhost:8080/simple-webapp:
    How to do it...

How it works...

The Maven Tomcat plugin allows web applications to be deployed and tested using Apache Tomcat. The run goal is bound to the package phase. Maven runs all the phases prior to it.

Tomcat uses default values to start the server.

Tip

As this is not an official Maven plugin, we have explicitly specified the groupId (org.apache.tomcat.maven) and the artifactId (tomcat7-maven-plugin) instead of the short plugin prefix. To use the short plugin prefix, add the following in the settings file:

<pluginGroup>org.apache.tomcat.maven</pluginGroup>

Then Maven can be invoked as follows:

mvn tomcat7:run

There's more...

The Maven Tomcat7 plugin also supports goals to start and stop Tomcat, which can be used when running integration tests.

It also supports the creation of an executable JAR using embedded Tomcat. Let us see how to do this:

  1. Open the web project for which you want to create an executable JAR (project-with-executable-webapp).
  2. Add the following plugin and configuration:
         <plugins>
           <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
            <executions>
              <execution>
                <id>tomcat-run</id>
                <goals>
                  <goal>exec-war-only</goal>
                </goals>
                <phase>package</phase>
                <configuration>
                  <path>/</path>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
  3. Run the following command:
    mvn clean package
    
  4. Run the JAR created in the target folder:
    java –jar project-with-executable-webapp-1.0-SNAPSHOT-war-exec.jar
    
  5. Observe the output:
    There's more...

What we have now is a distributable web application using embedded Tomcat.

Tip

There is a bug due to which we need to use version 2.1 of the plugin rather than 2.2 for this to work.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.137.58