How do I configure an additional context path for Maven-Tomcat plugin?

痞子三分冷 提交于 2019-12-19 03:16:34

问题


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 "" 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">

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>

Thanks for any solutions and additional troubleshooting steps, - Dave


回答1:


Use the serverXml configuration for that.




回答2:


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);
    }
}



回答3:


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.




回答4:


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/


来源:https://stackoverflow.com/questions/6984242/how-do-i-configure-an-additional-context-path-for-maven-tomcat-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!