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'?>
<tomcat-users>
	<role rolename="manager"/>
	<user username="maven" password="secret" roles="manager"/>
</tomcat-users>

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.

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

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="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>eu.christophburmeister</groupId>
	<artifactId>nice-app</artifactId>

	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>nice-app Maven Webapp</name>

	<build>
		<finalName>nice-app</finalName>
		<plugins>
			<plugin>
				<groupId>org.codehaus.cargo</groupId>
				<artifactId>cargo-maven2-plugin</artifactId>
				<version>1.0.6</version>
				<configuration>
					<container>
						<containerId>tomcat6x</containerId>
						<type>remote</type>
					</container>
					<configuration>
						<type>runtime</type>
						<properties>
							<!-- keep in mind that for tomcat7 the url changed to /manager/html -->
							<cargo.tomcat.manager.url>http://127.0.0.1:8080/manager</cargo.tomcat.manager.url>
							<cargo.remote.username>maven</cargo.remote.username>
							<cargo.remote.password>secret</cargo.remote.password>
						</properties>
					</configuration>
					<deployer>
						<type>remote</type>
						<deployables>
							<deployable>
								<groupId>${project.groupId}</groupId>
								<artifactId>${project.artifactId}</artifactId>
								<type>war</type>
								<properties>
									<context>${project.artifactId}</context>
								</properties>
							</deployable>
						</deployables>
					</deployer>
				</configuration>
				<executions>
					<execution>
						<id>redeploy-war-file-to-tomcat</id>
						<phase>install</phase>
						<goals>
							<goal>redeploy</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

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]
[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]
[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] BUILD SUCCESS
[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 (http://127.0.0.1:8080/nice-app/) 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 :-)

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>