Define Servlet Context in WAR-File

前端 未结 4 516
执笔经年
执笔经年 2020-12-05 03:15

How can I tell e.g. Tomcat to use a specific context path when given my WAR-File?

Example: I have a war file created by maven build and the resulting name of the fil

相关标签:
4条回答
  • 2020-12-05 03:18

    You can set the path attribute of the <Context> element of your META-INF/context.xml.

    Alternatively, you can configure maven to create the war artifact with a custom name:

    <build>
        <plugins>
             <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.0</version>
                <configuration>
                    <warName>yourCustomWarName</warName>
                </configuration>
            </plugin>
            ........
        </plugins>
    </build>
    
    0 讨论(0)
  • 2020-12-05 03:19

    I found an easy solution to keep war file name and choose the context-path.

    You just have to deploy your war outside of the Host's appBase and to create a link inside the appBase directory.

    Ex. :

    ln -sf ${CATALINA_HOME}/wars/myapp-0.0.8-SNAPSHOT.war ${CATALINA_HOME}/webapps/myapp.war
    

    Ektor

    0 讨论(0)
  • 2020-12-05 03:25

    In your project there is a folder META-INF, in that folder there is a context.xml file.

    <?xml version="1.0" encoding="UTF-8"?>
    <Context  path="/myproject" />
    
    0 讨论(0)
  • 2020-12-05 03:27

    There are two important points in the the documentation of the Context Container:

    • In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The name of the file (less the .xml extension) will be used as the context path. Multi-level context paths may be defined using #, e.g. foo#bar.xml for a context path of /foo/bar. The default web application may be defined by using a file called ROOT.xml.
    • Only if a context file does not exist for the application in the $CATALINA_BASE/conf/[enginename]/[hostname]/, in an individual file at /META-INF/context.xml inside the application files. If the web application is packaged as a WAR then /META-INF/context.xml will be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to match the application's context path. Once this file exists, it will not be replaced if a new WAR with a newer /META-INF/context.xml is placed in the host's appBase.

    So, when you bundle a META-INF/context.xml, the file gets renamed to the name of the WAR and this name becomes the context path, regardless of any path defined in the Context element.

    I see thus two options here:

    1. Either set the name of the generated war to a shorter name (I suggest using <finalName> over <warName> which is deprecated AFAIK):

      <project>
        ...
        <build>
          <finalName>mycontext</finalName>
          ...
        </build>
        ...
      </project>
      
    2. Or use the maven-tomcat-plugin for the deployment and set the context path in the plugin configuration:

      <project>
        ...
        <build>
          ...
          <plugins>
            ...
            <plugin>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>tomcat-maven-plugin</artifactId>
              <version>1.0-SNAPSHOT</version>
              <configuration>
                <path>/mycontext</path>
              </configuration>
            </plugin>
            ...
          </plugins>
          ...
        </build>
        ...
      </project>
      
    0 讨论(0)
提交回复
热议问题