Andrei Pall

Linux Software Engineering

Using an embedded Tomcat with Maven Tomcat plugin

Group ID - overall organization / top-level package
Artifact ID - project / library name
Version - unique release number

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-webapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-webapp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>my-webapp</finalName>
    <plugins>
      <plugin>
         <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
      </plugin>
    </plugins>
  </build>
</project>

Note the packaging element - this tells Maven to build as a WAR. Change into the webapp project's directory and try:

mvn clean package
mvn clean install tomcat7:run

You'll see target/my-webapp.war is built, and that all the normal steps were executed.

Now you can modify this webapp project and turn it into anything you need!

Goal Description
tomcat:deployDeploy a WAR to Tomcat.
tomcat:deploy-onlyDeploy a WAR to Tomcat witjout forking the package lifecycle
tomcat:explodedDeploy an exploded WAR to Tomcat.
tomcat:helpDisplay help information on tomcat-maven-plugin.
Call
 mvn tomcat:help -Ddetail=true -Dgoal=<goal-name>
to display parameter details.
tomcat:infoLists information about the Tomcat version, OS, and JVM properties.
tomcat:inplaceDeploy a WAR in-place to Tomcat.
tomcat:listLists all the currently deployed web applications in Tomcat.
tomcat:redeployRedeploy a WAR in Tomcat. Deploy with forcing update flag to true
tomcat:reloadReload a WAR in Tomcat.
tomcat:resourcesLists JNDI resources in Tomcat.
tomcat:rolesLists security roles in Tomcat.
tomcat:runRuns the current project as a dynamic web application using an embedded Tomcat server.
tomcat:run-warRuns the current project as a packaged web application using an embedded Tomcat server.
tomcat:run-war-onlyRuns the current project as a packaged web application using an embedded Tomcat server without forking the package cycle.
tomcat:sessionsLists session information for a WAR in Tomcat.
tomcat:shutdown

Shuts down all possibly started embedded tomcat servers. This will be automatically down through a shutdown hook or you may call this Mojo to shut them down explictly.

By default the shutdown goal is not bound to any phase. For integration tests you might want to bind it to post-integration-test.

tomcat:startStart a WAR in Tomcat.
tomcat:stopStop a WAR in Tomcat.
tomcat:undeployUndeploy a WAR from Tomcat.