Saturday, January 27, 2007

Using Maven to deploy application to Apache Tomcat Server

For those who are not familiar with Maven, it is (as stated in the project site) a "software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information."

Based on the concept of "convention over configuration" developers may use Maven to compile, build, test, deploy, generate reports and use a whole bunch of other features to manage their software development project. In another oppportunity I will post an article about Maven, for now I will write about my most immediate need: deploy a war file to a tomcat server.

As I am using maven to manage my project from test to build, all I need is to configure the Cargo plugin. Cargo is able to download, install and start Tomcat, but this is not our focus right now, so I will assume you have Tomcat installed and running with no problems.

Using Maven´s Cargo plugin it is possible to install an application into Tomcat in two ways:

  1. Use the Tomcat Manager application, which is deployed by default with Tomcat 5.x and is nothing more than a web interface to manage your server. It has the ability to install a WAR into the server from a remote location.
  2. Copy the WAR File of your application directly into the File System where Tomcat is expecting applications to be, normally %CATALINE_HOME%/webapps.
As one can find strong and weak points in each of these options, I will tell a little bit about the first one.

Using the Tomcat Manager

Now we will deploy an application to Tomcat while it is still running. This is called Hot Deploying.

You should add some comands to your pom file, and they should look like the excerpt of code below. Actually it should be positioned between the tags:
<build>
<plugins>
...
</plugins>
</build>

<plugin>
<groupid>org.codehaus.cargo</groupid>
<artifactid>cargo-maven2-plugin</artifactid>

<configuration>

<!-- Container configuration -->
<container>
<containerid>tomcat5x</containerid>
<type>remote</type>
</container>

<!-- Configuration to use with the Container -->
<configuration>
<type>runtime</type>
<properties>
<cargo.tomcat.manager.url>http://localhost:8080/manager</cargo.tomcat.manager.url>
<cargo.remote.username>admin</cargo.remote.username>
<cargo.remote.password>admin</cargo.remote.password>
</properties>
</configuration>

<!-- Deployer configuration -->
<deployer>
<type>remote</type>
<deployables>
<deployable>
<groupid>com.company</groupid>
<artifactid>web-component</artifactid>
<type>war</type>
</deployable>
</deployables>
</deployer>

</configuration>

</plugin>
You should need to change only the values in the and tags with the values that describe your component WAR file you wanto to install to Tomcat.

Having this in your pom file, you need to issue the following command:

mvn cargo:deploy
If you already have the application running in tomcat it is necessary that you undeploy it before deploying. So, issue the command below and you should be ok:

mvn cargo:undeploy

You also have the option to execute a command that will do an undeploy before attempting to deploy:

mvn cargo:deployer-redeploy
That should be enough to get the application running in Tomcat, deploying from a remote server.

I would just like to add that the configuration for the tags
and are values that must match the ones in the tomcat-users.xml file with the role "manager". Check this before trying to do the deploy.

1 comment:

Björn Schütte said...

The example pom.xml above doesn't work as it is case sensitive (e.g. groupId instead of groupid).

But then it works!