560 words — 3 minutes read

Integrate Tomcat 8x with Maven and Cargo in your Java web application

Cargo “is a thin wrapper that allows you to manipulate various types of application containers - Java EE and others - in a standard way.”

You can also integrate Cargo with Maven using the Maven 23 plugin, which can be handy for Magnolia projects which are usually based on a Maven structure.

Within Magnolia projects there is often an existing integration of Tomcat 6 or 7 in the Maven webapp configuration using the Tomcat Maven Plugin so that Magnolia can be started on the command line.

Using a more recent Tomcat version

Because Tomcat 7 is already quite dated now and often newer Tomcat versions are required, this article quickly shows you how to add the needed configuration using Cargo which is a replacement for the Tomcat Maven Plugin.

Maven plugin configuration

See the the cargo configuration for Tomcat 8.5.4 added to a very basic Magnolia webapp configuration file / pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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">
<name>magnolia-cargo-demo: webapp</name>
        <!-- exclude jars copied "physically" from the webapp overlay - so we only get those resolved by Maven's dependency management -->

    <!-- Cargo with Tomcat 8 starts here -->

          <!-- https://codehaus-cargo.github.io/cargo/Maven2+Plugin+Reference+Guide.html#Maven2PluginReferenceGuide-container -->
          <!-- Instead of downloading the container, you can also reuse an existing installation by settings its directory:
          <home>/Users/horsti/files/tomcats/apache-tomcat-8.5.4</home> -->

          <!-- If true, then the file specified by <output> will not be erased across different runs -->
          <!-- set this value if your web app is taking some time to start -->
            <cargo.jvmargs>-XX:MaxPermSize=512m -Xms256m -Xmx3072m -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -XX:+DisableExplicitGC -Xverify:none -Djava.awt.headless=true</cargo.jvmargs>
              <!-- set the context for your web app so the correct configuration is applied -->
    <!-- end of Cargo plugin configuration -->

You can start the server from within your webapp directory using the Maven command

  mvn cargo:run


  • In a “real” project you would replace concrete versions with variable names and wrap maven settings and execution in one ore more shell scripts.
  • I already added JVM arguments to the cargo.jvmargs parameter suitable for Magnolia projects: adjust them to your needs, especially the memory settings.
  • The JVM arguments already contain parameters for remote debugging on port 5005.
  • The context your web application is running under was already set, in this example to /dev. This is needed by some applications so they know which configuration to load.
  • The commented out timeout parameter is very important if your application needs some time for initial startup. If starting can take quite some time for your application then the default timeout setting for Cargo might prevent a successful deployment.
  • Consider setting magnolia.update.auto=false in your magnolia.properties file so that the initial deployment with Cargo doesn’t take too long.

Convenient SSL configuration for development

In the next post I will show you how you can extend the Cargo configuration to add Java Keystore / certificate configuration on the fly for deployments needing the HTTPS protocol for development and testing.


Lars Fischer

IT. Aerophonist. Autor ohne Werk.