Run embedded Tomcat 8 in Maven

The most common way to run your Java web app using Maven in an embedded container is the Tomcat Maven Plugin. However, the latest version of this plugin is for Tomcat 7 (last update released back in 2013).

Luckily there is another plugin that you can use, the Maven Cargo Plugin.

Here is an example working configuration that you can use with the plugin.

<plugin>
    <groupId>org.codehaus.cargo</groupId>
    <artifactId>cargo-maven2-plugin</artifactId>
    <configuration>
        <container>
            <containerId>tomcat8x</containerId>
            <artifactInstaller>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat</artifactId>
                <version>${tomcat.version}</version>
            </artifactInstaller>
        </container>
        <configuration>
            <type>standalone</type>
            <home>
                ${project.build.directory}/apache-tomcat-${tomcat.version}
            </home>
            <properties>
                <cargo.servlet.port>8080</cargo.servlet.port>
                <cargo.logging>high</cargo.logging>
            </properties>
        </configuration>
        <deployables>
            <deployable>
                <groupId>${project.groupId}</groupId>
                <artifactId>${project.artifactId}</artifactId>
                <type>war</type>
                <properties>
                    <context>/myapp</context>
                </properties>
            </deployable>
        </deployables>
    </configuration>
</plugin>

You can run your project using Cargo by using the cargo:run goal of the plugin. This will download the specified tomcat version and deploy your app into it.

Please note the ${tomcat.version} property, which you need to define in your pom.xml.