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

4

5

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>
Virg answered 8/8, 2011 at 15:9 Comment(0)
D
1

Use the serverXml configuration for that.

Donata answered 8/8, 2011 at 19:14 Comment(2)
Could you provide an example? Everything I'm reading about server.xml indicates it is NOT the place where you would add additional context paths for your resources.Virg
Yes, it is, you are reading the wrong sources. The doc says "server.xml to use Note if you use this you must configure in this file your webapp paths." This is the default server.xml. You put your contexts into the Host element.Donata
H
4

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);
    }
}
Houk answered 8/4, 2012 at 1:49 Comment(0)
A
2

You've got the right idea with your contextFile xml element, but try being explicit instead of using a relative path.

<contextFile>${basedir}/config/tomcat/context.xml</contextFile>

${basedir} is the folder your pom.xml lives in. You might need to change this part if that's not correct for your project.

Apostil answered 26/11, 2012 at 22:51 Comment(0)
D
1

Use the serverXml configuration for that.

Donata answered 8/8, 2011 at 19:14 Comment(2)
Could you provide an example? Everything I'm reading about server.xml indicates it is NOT the place where you would add additional context paths for your resources.Virg
Yes, it is, you are reading the wrong sources. The doc says "server.xml to use Note if you use this you must configure in this file your webapp paths." This is the default server.xml. You put your contexts into the Host element.Donata
C
1

We needed the same in order to run in dev mode our app, which uses Apache solr. The following seemed to work:

  1. declare solr as type=war, scope=tomcat dependency
  2. use the tomcat maven plugin (version 1.1) with addContextWarDependencies
  3. add system property for solr home as example below

then one can run maven like: mvn tomcat:run. Keep in mind that java will require lots of memory in this scenario, so before running maven do export MAVEN_OPTS="-Xmx1536m -XX:MaxPermSize=512m" for example.

The pom.xml is like, where we assume there are some properties -including one called solr.home- read from a config file:

<project>
  ...
  <dependencies>
  ...
    <dependency>
      <groupId>org.apache.solr</groupId>
      <artifactId>solr</artifactId>
      <version>3.3.0</version>
      <type>war</type>
      <scope>tomcat</scope>
    </dependency>
  </dependencies>
  <build>
  ...
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>tomcat-maven-plugin</artifactId>
      <version>1.1</version>
      <configuration>
        <addContextWarDependencies>true</addContextWarDependencies>
        <systemProperties>
          <property.you.need>${example.property.yours}</property.you.need>
          <solr.solr.home>${solr.home}</solr.solr.home>
        </systemProperties>
      </configuration>
    </plugin>
    ...
  </build>
</project>

Solr can then be reached in:

http://localhost:8080/solr/admin

and your application in

http://localhost:8080/your-app-id/
Cocoon answered 3/7, 2012 at 7:44 Comment(1)
tomcat is NOT a possible value as scope. Possible values are only provided, compile, runtime, test, systemCauliflower

© 2022 - 2024 — McMap. All rights reserved.