How do I deploy a maven web application to Tomcat?
Author: Deron Eriksson
Description: This tutorial describes how to deploy a maven web application to Tomcat using the maven tomcat plugin.
Tutorial created using: Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1) || Tomcat 6.0.14


Page:    1 2 >

The TomcatSW MavenSW Plugin (homepage at http://mojo.codehaus.org/tomcat-maven-plugin/introduction.html ), can be used to perform tasks such as deploying, reploying, and undeploying a warW file to Tomcat using the Tomcat Manager application.

After it has been set up, all you need to do is the following command to build and deploy a project to Tomcat using the mavenSW tomcatSW plugin:

mvn tomcat:deploy

Now, let's see how to set up a maven project to use the maven tomcat plugin.

First off, to access the Tomcat Web Application Manager, you must use a name/password with a manager role in Tomcat's tomcat-users.xml file. In my Tomcat's tomcat-users.xml file, the name "test" with password "test" fulfills this requirement.

tomcat-users.xml

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="manager"/>
  <user username="test" password="test" roles="manager"/>
</tomcat-users>

In my settings.xml file, I need to create a server entry specifying a name for my tomcat server and the manager name and password. I called my tomcat "mytomcat" and used "test" for username and "test" for password.

settings.xml server entry

<server>
        <id>mytomcat</id>
        <username>test</username>
        <password>test</password>
</server>

Next, I added a tomcat-maven-plugin entry to the "mywebapp" project's pom.xml. I specified the url to my Tomcat manager (by default this is http://localhost:8080/manager). I specified the server to be "mytomcat" so that the "mytomcat" server name and password in server.xml would be used to connect to Tomcat. I specified a path element to be "/mywebapp", although this wasn't really necessary since the build finalName is the same in this case.

			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>tomcat-maven-plugin</artifactId>
				<configuration>
					<url>http://192.168.1.7:8080/manager</url>
					<server>mytomcat</server>
					<path>/mywebapp</path>
				</configuration>
			</plugin>

My finished pom.xml file is shown here:

pom.xml

<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.maventest</groupId>
	<artifactId>mywebapp</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>mywebapp 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>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	<build>
		<finalName>mywebapp</finalName>
		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>tomcat-maven-plugin</artifactId>
				<configuration>
					<url>http://192.168.1.7:8080/manager</url>
					<server>mytomcat</server>
					<path>/mywebapp</path>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-eclipse-plugin</artifactId>
				<inherited>true</inherited>
				<configuration>
					<classpathContainers>
						<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
						<classpathContainer>org.eclipse.jdt.USER_LIBRARY/TOMCAT_6.0.14_LIBRARY</classpathContainer>
					</classpathContainers>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Now, before using the tomcat maven plugin, I'll log on to my manager application manually to see what happens. I log on with username "test" and password "test". In this list of applications, the "mywebapp" application isn't present yet.

Tomcat Manager

(Continued on page 2)

Page:    1 2 >