How do I configure an additional context path for tomcat-maven-plugin?

Dave picture Dave · Aug 8, 2011 · Viewed 16.5k times · Source

I'm using Maven 3.0.3 with the Tomcat plugin. Using Maven and Tomcat, I would like to deploy an embedded instance of the site. My question is how do I configure an additional context path in my embedded Tomcat server? Below is my Tomcat configuration, but either my <contextFile> specification is invalid or the contents of that file (below) are invalid:

<Context path="/all-new-jx-web" docBase="/Users/davea/Documents/workspace/NissanUSA2/Technology/nna/mycousa/jx/target/web">
</Context>

because when I invoke

mvn clean -Dmaven.test.skip=true verify -Ptomcat tomcat:run

none of the URLs mapped to /all-new-jx-web (my additional context path) are getting mapped (assets aren't being served by Tomcat). Any ideas why? Below is my tomcat profile from my pom.xml file:

<profile>
  <id>tomcat</id>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <version>1.1</version>
        <configuration>
          <contextFile>config/tomcat/context.xml</contextFile>
          <mode>context</mode>
          <addContextWarDependencies>true</addContextWarDependencies>
          <charset>UTF-8</charset>
          <path>/all-new-jx</path>
          <update>true</update>
          <warDirectory>target/${project.artifactId}-${project.version}.${project.packaging}</warDirectory>
          <systemProperties>
            <JAVA_OPTS>-Xms256m -Xmx512m -XX:MaxPermSize=256m -XX:NewRatio=6
                -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled
                -verbose:gc"
            </JAVA_OPTS>
          </systemProperties>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>

Answer

user897493 picture user897493 · Apr 8, 2012

I had the same issue.I tried giving location to external server xml but I could get my project to run. Ultimately I ended up modifying the tomcat plugin code to include the additional static context path.

// Snippet from AbstractRunMojo.java

            String appBase = new File(configurationDir, "webapps")
                    .getAbsolutePath();
            Host host = container.createHost("localHost", appBase);

            host.addChild(context);
            // Adding static context
            createStaticContext(container, context, host);//More code after this


    private void createStaticContext(final Embedded container, Context context,
        Host host) {
    if (null != staticContextDocbase) {
        Context ctx1 = container.createContext(staticContextPath,
                staticContextDocbase);
        ctx1.setPrivileged(true);
        Wrapper servlet = context.createWrapper();
        servlet.setServletClass(DefaultServlet.class.getName());
        servlet.setName("staticContent");
        ctx1.addChild(servlet);
        ctx1.addServletMapping("/", "staticContent");
        host.addChild(ctx1);
    }
}