简体   繁体   English

在WAR-File中定义Servlet上下文

[英]Define Servlet Context in WAR-File

How can I tell eg Tomcat to use a specific context path when given my WAR-File? 在给出WAR-File时,如何告诉Tomcat使用特定的上下文路径?

Example: I have a war file created by maven build and the resulting name of the file is rather long. 示例:我有一个由maven build创建的war文件,结果文件的名称相当长。 So I do not want the tomcat manager application to use the filename of the war as the context. 所以我不希望tomcat管理器应用程序使用war的文件名作为上下文。

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 path attribute of Context : 我还在Contextpath属性的文档中找到了这个:

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中静态定义Context,否则不得设置此字段的值,因为它将从用于.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. 所以它似乎不是告诉应用程序服务器应该是什么路径的正确方法。

Any more hints? 还有什么提示吗?

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. $CATALINA_BASE/conf/[enginename]/[hostname]/目录中的单个文件(扩展名为“.xml”)中。 The name of the file (less the .xml extension) will be used as the context path . 文件名 (减去.xml扩展名) 将用作上下文路径 Multi-level context paths may be defined using #, eg foo#bar.xml for a context path of /foo/bar. 可以使用#来定义多级上下文路径,例如foo #bar.xml,用于/ foo / bar的上下文路径。 The default web application may be defined by using a file called ROOT.xml. 可以使用名为ROOT.xml的文件来定义默认Web应用程序。
  • 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. 仅当$CATALINA_BASE/conf/[enginename]/[hostname]/的应用程序不存在上下文文件时,才在应用程序文件内的/META-INF/context.xml中的单个文件中。 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 . 如果Web应用程序打包为WAR,则/META-INF/context.xml将复制到$CATALINA_BASE/conf/[enginename]/[hostname]/命名以匹配应用程序的上下文路径 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. 一旦此文件存在,如果在主机的appBase中放置了带有较新的/META-INF/context.xml的新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 path defined in the Context element. 因此,当您捆绑META-INF/context.xml ,文件将重命名为WAR的名称,并且此名称将成为上下文路径,而不管Context元素中定义的任何path

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): 将生成的war的名称设置为较短的名称(我建议使用<finalName> over <warName> ,不推荐使用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: 或者使用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> 

I found an easy solution to keep war file name and choose the context-path. 我找到了一个简单的解决方案来保存war文件名并选择context-path。

You just have to deploy your war outside of the Host's appBase and to create a link inside the appBase directory. 你只需要在Host的appBase之外部署你的战争,并在appBase目录中创建一个链接。

Ex. 防爆。 :

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

Ektor 埃克尔

You can set the path attribute of the <Context> element of your META-INF/context.xml . 您可以设置META-INF/context.xml<Context>元素的path属性。

Alternatively, you can configure maven to create the war artifact with a custom name: 或者,您可以配置maven以使用自定义名称创建war工件:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM