Thursday Jul 24

Key Concepts

Maven Tomcat Deployment using Cargo

PDFPrintE-mail
Friday, 29 April 2011 13:55
AddThis Social Bookmark Button


Maven Cargo tomcat deployment tutorial  

Maven Tomcat Deployment using Cargo plugin

There is a nice plugin for Maven called Cargo which simplifies deployment on different servlet container massively. With Cargo Maven Plugin you can manage different containers which are installed, start, stop fully automatic.

Cargo also performs the deployment of the webproject to the specific container(s). Thus we can really kickstart web development with a properly configured project. If this is not the case step into the tutorial configuring a webproject to simplify team development and testing first.

The project is configured properly for Cargo if the pom-file contains a plugin configuration for the cargo-maven2-plugin artifact which itself contains some container configuration as shown below:

Include the following snippet in the <build><plugins> section of your project's pom.xml-file:

Maven plugin configuration

<plugin>
    <groupId>org.codehaus.cargo</groupId>
    <artifactId>cargo-maven2-plugin</artifactId>
    <version>1.2.0</version>
    <configuration>
        <container>
            <containerId>tomcat7x</containerId>
            <!-- Type == Installed means that you want to deploy to a container that's installed on your computer -->
            <type>installed</type>
            <!-- The home folder for your local Tomcat -->
            <home>${catalina.home}</home>
        </container>
    </configuration>
    <!-- This is another one that confused me for long. Its not enough to specify 'installed' in the container tag. 
           You have to now specify another configuration with type == existing and re-issue the home path -->
        <type>existing</type>
        <home>${catalina.home}</home>
    <configuration>
        <deployer>
            <!-- You have to again specify that the type for the deployer -->
            <type>installed</type>
            <deployables>
                <!-- This deployable specifies the webapp you want to deploy -->
                <deployable>
                    <groupId>${project.groupId}</groupId>
                    <artifactId>${project.artifactId}</artifactId>
                    <type>war</type>
                </deployable>
            </deployables>
        </deployer>
    </configuration>
</plugin>       

Let's define why cargo should find the home folder for catalina (Tomcat). Assuming we have a Unix or Mac OSX machine like me, your Maven settings.xml should provide the following Maven profile. 

Include the following snippet in the <profiles> section of your project's settings.xml-file:

Maven path Tomcat activated using profile

<profile>
    <id>Unix</id>
    <activation>
        <os>
            <family>unix</family>
        </os>
    </activation>
    <properties>
        <catalina.home>/Applications/tomcat</catalina.home>
    </properties>
</profile>

This will activate based on OS settings. The Maven profile Unix with be activated because OSX is part of the family unix. catalina.home will be then available as a property.

Deploy a webapp on Tomcat7 with Maven

To try the Cargo deployment simply use:

Deploy webapp using Maven for Tomcat7

mvn cargo:deploy

Undeploy a Maven webapp on Tomcat7 

To try the Cargo undeploy simply use:

Undeploy webapp using Maven for Tomcat7

mvn cargo:undeploy

Tips and Tricks

Define the web context

The context root of a web application determines which URLs Tomcat will delegate to your web application. If your application's context root is webapp then any request for /webapp or /webapp/* will be handled by your application unless a more specific context root exists. If a second web application were assigned the context root myapp/help, a request for /webapp/help/help.jsp would be handled by the second web application, not the first.

Include or update the following snippet in the <deployer> section of your maven cargo plugin definition:

Maven plugin configuration

<deployable>
    <groupId>${project.groupId}</groupId>
    <artifactId>${project.artifactId}</artifactId>
    <type>war</type>
    <properties>
        <context>${project.artifactId}</context>
    </properties>
</deployable>

In this definition we are using the artifactId of the project to define the web context

If you have any remark or questions feel free to put a comment.If you enjoyed this tutorial and want to promote it don't hesitate to click on

Tags: maven, plugin, deployment, configuration, home, type, /type, cargo, context

Comments  

 
+1 #2 gabe 2013-03-10 06:02
Quoting Bjorn:
Please update the example of the pom.xml, as it's not correct.


org.codehaus.cargo
cargo-maven2-plugin
1.2.0


tomcat7x

installed
${catalina.home}



existing

${catalina.home}


installed



>${project.groupId}
${project.artifactId}
war

${project.artifactId}
Quote
 
 
+1 #1 Bjorn 2012-03-02 09:56
Please update the example of the pom.xml, as it's not correct.
Quote
 

Add comment


Security code
Refresh

Java Tutorial on Facebook