如何在 java war 文件中的 META-INF/context.xml 中的 Tomcat 8 上指定“上下文路径”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30223269/
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-11-02 16:42:19  来源:igfitidea点击:

How can I specify "Context path" on Tomcat 8 inside META-INF/context.xml in java war file?

javatomcatwartomcat8

提问by j2gl

How can I deploy mywebapp-1.0.0.warto $TOMCAT_HOME/webapps directory with context path /mywebappusing context.xml inside the war file on Tomcat 8?

如何使用 Tomcat 8 上的 war 文件中的 context.xml使用上下文路径/mywebappmywebapp-1.0.0.war部署到 $TOMCAT_HOME/webapps 目录?

I'm getting back to work with Tomcat after long time since version 5. I'm used to create META-INF/context.xml inside my war file:

自第 5 版以来很长一段时间后,我将重新使用 Tomcat。我习惯于在我的 war 文件中创建 META-INF/context.xml:

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

Maven creates a war file with this name: mywebapp-1.0.0.war

Maven 使用此名称创建一个 war 文件:mywebapp-1.0.0.war

But when I deploy the war file to $TOMCAT_HOME/webapps directory the context path will be http://localhost:8080/mywebapp-1.0.0instead of http://localhost:8080/mywebapp/.

但是当我将 war 文件部署到 $TOMCAT_HOME/webapps 目录时,上下文路径将是http://localhost:8080/mywebapp-1.0.0而不是http://localhost:8080/mywebapp/

Also I see that $TOMCAT_HOME/conf/Catalina/localhost is empty, instead of having the xml file copied from the war file deployed.

另外我看到 $TOMCAT_HOME/conf/Catalina/localhost 是空的,而不是从部署的war文件中复制xml文件。

I also added to $TOMCAT_HOME/conf/server.xml the deployXML="true"

我还在 $TOMCAT_HOME/conf/server.xml 中添加了 deployXML="true"

 <Host name="localhost"  appBase="webapps"
        unpackWARs="true" autoDeploy="true" 
        deployXML="true">

回答by j2gl

It is not possible to put a war inside the webappdirectory and set the pathattribute in META-INF/context.xml file at the same time. Tomcat 8 documentation, clearly says about this attribute:

不可能在webapp目录中放置一个 war并同时在 META-INF/context.xml 文件中设置path属性。Tomcat 8 documentation,清楚地说明了这个属性:

This attribute must only be used when statically defining a Context in server.xml. In all other circumstances, the path will be inferred from the filenames used for either the .xml context file or the docBase.

只有在 server.xml 中静态定义上下文时才必须使用此属性。在所有其他情况下,将从用于 .xml 上下文文件或 docBase 的文件名推断路径。

回答by Medhi Redjem

Can't you just rename your war file to mywebapp (via Maven or else) so that Tomcat will deploy it under /mywebapp ?

您不能将您的 war 文件重命名为 mywebapp(通过 Maven 或其他方式),以便 Tomcat 将其部署在 /mywebapp 下吗?

回答by Martin D

Thanks for your research j2gl! I've found out that good way how to achieve both .war file with full name with version and deployed short path is to use Tomcat manager API. For example through Tomcat7-maven-plugin.

感谢您对 j2gl 的研究!我发现如何使用 Tomcat 管理器 API 实现具有版本全名和部署短路径的 .war 文件的好方法。例如通过Tomcat7-maven-plugin。