Java 在 WAR 文件中定义 Servlet 上下文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2593472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:38:32  来源:igfitidea点击:

Define Servlet Context in WAR-File

javatomcatmaven-2tomcat6

提问by er4z0r

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

当给定我的 WAR 文件时,如何告诉例如 Tomcat 使用特定的上下文路径?

Example: I have a war file created by maven build and the resulting name of the file is rather long. So I do not want the tomcat manager application to use the filename of the war as the context.

示例:我有一个由 maven build 创建的 war 文件,生成的文件名很长。所以我不希望 tomcat 管理器应用程序使用战争的文件名作为上下文。

Supplying a context.xml in META-INF did not produce the desired results

在 META-INF 中提供 context.xml 没有产生预期的结果

I also found this in the documentation for the pathattribute of Context:

我还在以下path属性的文档中找到了这一点Context

The value of this field must not be set except when statically defining a Context in server.xml, as it will be inferred from the filenames used for either the .xml context file or the docBase.

除非在 server.xml 中静态定义上下文,否则不得设置此字段的值,因为它将从用于 .xml 上下文文件或 docBase 的文件名中推断出来。

So it does not seem to be the right way to tell the application-server what the path for my WAR should be.

因此,告诉应用程序服务器我的 WAR 的路径应该是什么似乎不是正确的方法。

Any more hints?

还有什么提示吗?

采纳答案by Pascal Thivent

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

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.xmlinside the application files. If the web application is packaged as a WAR then /META-INF/context.xmlwill 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.xmlis placed in the host's appBase.
  • 在目录中的单个文件(带有“.xml”扩展名)中$CATALINA_BASE/conf/[enginename]/[hostname]/文件的名称(减去 .xml 扩展名)将用作上下文路径。可以使用# 定义多级上下文路径,例如foo#bar.xml 用于/foo/bar 的上下文路径。可以使用名为 ROOT.xml 的文件来定义默认 Web 应用程序。
  • 仅当应用程序文件$CATALINA_BASE/conf/[enginename]/[hostname]/中的单个文件中不存在应用程序的上下文/META-INF/context.xml文件时。如果 Web 应用程序打包为 WAR,/META-INF/context.xml则将被复制到$CATALINA_BASE/conf/[enginename]/[hostname]/并重命名以匹配应用程序的上下文路径。一旦此文件存在,如果/META-INF/context.xml在主机的 appBase 中放置了具有较新版本的新 WAR,它将不会被替换。

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 pathdefined in the Contextelement.

因此,当您捆绑 a 时META-INF/context.xml,该文件将被重命名为 WAR 的名称,并且该名称成为上下文路径,而不管元素中是否path定义了任何Context内容。

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-pluginfor 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>
    
  1. 无论是在生成的战争较短的名称的名称(我建议使用<finalName><warName>它被废弃据我所知):

    <project>
      ...
      <build>
        <finalName>mycontext</finalName>
        ...
      </build>
      ...
    </project>
    
  2. 或者使用maven-tomcat-plugin进行部署并在插件配置中设置上下文路径:

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

回答by Bozho

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

您可以设置path该属性<Context>的元素META-INF/context.xml

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

或者,您可以配置 maven 以使用自定义名称创建战争工件:

<build>
    <plugins>
         <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.0</version>
            <configuration>
                <warName>yourCustomWarName</warName>
            </configuration>
        </plugin>
        ........
    </plugins>
</build>

回答by Mark

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

在您的项目中有一个文件夹 META-INF,在该文件夹中有一个 context.xml 文件。

<?xml version="1.0" encoding="UTF-8"?>
<Context  path="/myproject" />

回答by Ektor

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 appBaseand to create a link inside the appBasedirectory.

您只需将您的战争部署在主机之外appBase并在appBase目录内创建一个链接。

Ex. :

前任。:

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

Ektor

埃克托