Tomcat Maven plugin does not use the configured context path

When you use the Tomcat Maven Plugin to deploy your application, you can specify the context path under which you want your application to be accessible. An example of the configuration of this plugin looks like this:

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <path>/jtuts</path>
    </configuration>
</plugin>

This way you application will be accessible under the /jtuts path. Depending on you port setting it can be for example localhost:8080/jtuts.

Sometimes you can run into an issue where no matter what you configure in the <path> element, you always end up with a context path that is equal to your artifactId. For example:

http://localhost:8080/using-spring-type-conversion-20140208

In my case the problem was the way I referenced the tomcat plugin in my mvn command. To use the Tomcat 7 plugin that you configured in your pom.xml (like I showed you above) you have to use the plugin name “tomcat7“. So you would build and deploy you app for example with the following Maven command:

mvn clean install tomcat7:run

This starts an embedded Tomcat using the plugin you have configured and uses the context path you have set.

The problem comes when you accidentally leave out the “7” at the end of the plugin name. If you deploy you app using:

mvn clean install tomcat:run

Maven will see that there are no plugins configured that has the name “tomcat“, but the build won’t fail because Maven has some conventions for finding plugins that you have not declared in your POM. In this case maven will find the org.codehaus.mojo:tomcat-maven-plugin and use it with default configuration. The default configuration means that the context path will be equal to the artifactId.

If you would like to read more about how Maven finds plugins that you have not configured, read the accepted answer in this Stackoverflow question.

So to sum it up, if you misspell the plugin name and use “tomcat” instead of “tomcat7“, Maven will not use the plugin that you configured, but another one with a default configuration. This could be the reason why your context path setting is not taken into account.

Of course, there can be other reasons for this kind of issue, but I have ran into this a couple of times and this was the solution for me.