Deploy war-file with maven to remote (standalone) tomcat

Vansu bridge (Riga), by Christoph Burmeister (own photo)

Vansu bridge (Riga), by Christoph Burmeister (own photo)

Let’s assume you write classes, code or JSP-scripts for a java-project which should be deployed into an application server like Tomcat from ASF. Setting up a maven webapp-project in eclipse is meanwhile nothing more then 5 clicks, a bit of fantasy with the artifactId and that’s it… thanks to predefined archetypes. Now the devteam is ready for some real-world-tests of the code and you wanna like to run the war. Sure, you can do it by using embedded jetty, but in this case we wanna have it up and running in a „real“ app-server: the Tomcat.
So first of all download a fresh version of tomcat and just unzip it. It’s just for demonstration, so you don’t have to touch the config-files at all… except one: the tomcat-users.xml. Change it to following content:

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

After having this configured, just hit the startup.bat (Yes! I’m on a WinOS) and cross your fingers that it will boot successfull. Maybe you have to change the port in server.xml from 8080 to something that’s still free on your machine.

Now you can set up your webapp-maven-project. Simply use the webapp-archetype and you will get the right structure. I’m using Maven 3.0.3 and in this case I called my application „nice-app“. After having created the skeleton, we don’t wanna write classes or code. For the moment, just replace the src/main/webapp/index.jsp with something you want to see after starting up.

	<h2>this is nice-app, the first try</h2>

Now take the pom.xml, the center of our attention and add the maven-cargo-plugin to build-plugins with the following configuration:

<project xmlns="" xmlns:xsi=""

	<name>nice-app Maven Webapp</name>

							<!-- keep in mind that for tomcat7 the url changed to /manager/html -->

Note the fact, that we bind the cargo-execution to the install-phase. Now open up a shell and type „mvn clen install“ and the output should show something similiar to this:

[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ nice-app ---
[INFO] Packaging webapp
[INFO] Assembling webapp [nice-app] in [D:\dev\eclipse-jee-indigo-workspace\remotedeploy\target\nice-app]
[INFO] Processing war project
[INFO] Copying webapp resources [D:\dev\eclipse-jee-indigo-workspace\remotedeploy\src\main\webapp]
[INFO] Webapp assembled in [72 msecs]
[INFO] Building war: D:\dev\eclipse-jee-indigo-workspace\remotedeploy\target\nice-app.war
[WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored
(webxml attribute is missing from war task, or ignoreWebxml attribute is specified as 'true')
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ nice-app ---
[INFO] Installing D:\dev\eclipse-jee-indigo-workspace\remotedeploy\target\nice-app.war to d:\dev\apache-maven-repo\eu\christophburmeister\nice-app\0.0.1-SNAPSHOT\nice-app-0.0.1-SNAPSHOT.war
[INFO] Installing D:\dev\eclipse-jee-indigo-workspace\remotedeploy\pom.xml to d:\dev\apache-maven-repo\eu\christophburmeister\nice-app\0.0.1-SNAPSHOT\nice-app-0.0.1-SNAPSHOT.pom
[INFO] --- cargo-maven2-plugin:1.0.6:redeploy (redeploy-war-file-to-tomcat) @ nice-app ---
[INFO] [mcat6xRemoteDeployer] Redeploying [D:\dev\eclipse-jee-indigo-workspace\remotedeploy\target\nice-app.war]
[INFO] [mcat6xRemoteDeployer] Undeploying [D:\dev\eclipse-jee-indigo-workspace\remotedeploy\target\nice-app.war]
[INFO] [mcat6xRemoteDeployer] Deploying [D:\dev\eclipse-jee-indigo-workspace\remotedeploy\target\nice-app.war]
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.098s
[INFO] Finished at: Sun Feb 05 01:06:52 CET 2012
[INFO] Final Memory: 6M/112M
[INFO] ------------------------------------------------------------------------

Next step: Point your browser (Firefox, Chrome, Opera, Lynx, whatever) to the corresponding adress ( and you should see the index.jsp you just created.

Now change the index.jsp in your maven-project and run the maven-command again. The result is a freshly deployed version of your war 🙂