How to Deploy and Redeploy a Maven Web Application in Tomcat7

First, we need to set up a simple Maven web application as an example.

mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.0 -DgroupId=id.web.el -DartifactId=mywebapp -Dversion=1.0-SNAPSHOT

By using above command, we have created a very simple Maven web application, named mywebapp.

Then we need to edit the pom.xml file, located inside the mywebapp directory. Add these lines below, right under the <finalName>mywebapp</finalName>

<plugins>
	<plugin>
		<groupId>org.codehaus.mojo</groupId>
		<artifactId>tomcat-maven-plugin</artifactId>
		<configuration>
			<url>http://localhost:8080/manager/text</url>
			<server>mytomcat</server>
			<path>/${project.build.finalName}</path>
		</configuration>
	</plugin>
</plugins>

Pay attention to the url and server sections. You need to adjust its value according to your server environment. Now we head into our Maven settings.xml file. You need to add these lines.

<servers>
  <server>
    <id>mytomcat</id>
    <username>myusername</username>
    <password>mypassword</password>
  </server>
</servers>

Set the username and password value based on your Tomcat7 tomcat-users.xml file. You can find it in [TOMCAT_HOME]/conf directory. Edit your tomcat-users.xml, and add these lines. What we basically need is a user that has a manager-script role.

<role rolename="manager-script"/>
<user username="myusername" password="mypassword" roles="manager-script"/>

That’s it. The configuration we need is completed. Now it’s time to try it. For deploy a Maven web application, run this command:

mvn tomcat:deploy

And if you need to redeploy, just run this command:

mvn tomcat:redeploy

Here’s the example output of redeploy command. Yours may be vary.

wks-401@wks-401:mywebapp$ mvn tomcat:redeploy
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building mywebapp Maven Webapp
[INFO]    task-segment: [tomcat:redeploy]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing tomcat:redeploy
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] [compiler:compile {execution: default-compile}]
[INFO] No sources to compile
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/wks-401/workspace/mywebapp/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] No sources to compile
[INFO] [surefire:test {execution: default-test}]
[INFO] No tests to run.
[INFO] [war:war {execution: default-war}]
[INFO] Packaging webapp
[INFO] Assembling webapp[mywebapp] in [/home/wks-401/workspace/mywebapp/target/mywebapp]
[INFO] Processing war project
[INFO] Copying webapp resources[/home/wks-401/workspace/mywebapp/src/main/webapp]
[INFO] Webapp assembled in [26 msecs]
[INFO] Building war: /home/wks-401/workspace/mywebapp/target/mywebapp.war
[INFO] [tomcat:redeploy {execution: default-cli}]
[INFO] Deploying war to http://localhost:8080/mywebapp  
[INFO] OK - Undeployed application at context path /mywebapp
[INFO] OK - Deployed application at context path /mywebapp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Thu Jun 21 17:38:28 CIT 2012
[INFO] Final Memory: 17M/247M
[INFO] ------------------------------------------------------------------------