将战争应用程序名称与战争文件名分开 [英] Separating war application name from war file name
问题描述
目前,如果我在 tomcat 上部署一个名为 say myapp.war
的战争文件,我可以通过 http://localhost/myapp/MyServlet.
Currently if i deploy a war file on tomcat named say myapp.war
, I can access its url by http://localhost/myapp/MyServlet.
然而,我想要的是部署一个战争文件名中带有版本号的战争,并且仍然具有相同的网址.例如,我想部署 myapp-1.1.0.war
并且 URL 仍然是 http://localhost/myapp/MyServlet
However what I want is to deploy a war with a version number in the war file name and still have the same url.
For eg, I want to deploy myapp-1.1.0.war
and still have the url be http://localhost/myapp/MyServlet
当然我需要不断更新war,版本号也会不断变化,所以我不能在任何地方硬编码war文件名.web.xml
中是否有任何设置可以用来为应用程序保留相同的 url,而不管 war 文件名如何?
Of course I need to keep updating the war and the version number will keep changing, so I cant hardcode the war filename anywhere.
Is there any setting in web.xml
I can use to keep the same url for the app regardless of the war filename?
推荐答案
解决办法是停止使用Tomcat的自动部署功能,它采用设置上下文名称"的快捷方式(/myapp
URL 的一部分)到.war"之前的 WAR 文件名部分.
The solution is to stop using the automatic deployment feature of Tomcat, which takes the shortcut of setting the "context name" (the /myapp
part of the URL) to the portion of the WAR filename before ".war".
相反,自己将 WAR 内容提取到文件系统,并在 TOMCAT_HOME/conf/[enginename]/[hostname]/[contextname].xml
中设置一个 XML 文件,该文件指向所需的上下文路径(例如 /myapp
) 到应用程序在磁盘上的位置(例如 /opt/webapps/myapp-1.1.0/
).
Instead, extract the WAR contents to the filesystem yourself and setup an XML file at TOMCAT_HOME/conf/[enginename]/[hostname]/[contextname].xml
which points the desired context path (such as /myapp
) to the location of the application on disk (such as /opt/webapps/myapp-1.1.0/
).
Tomcat 参考文档 提供了很好的解释Tomcat 如何自动部署应用程序,以及如何配置自定义逻辑以将上下文路径映射到应用程序文件位置(除了我上面建议的方法之外,还有一些其他的设置方法).
The Tomcat reference docs provide a good explanation of how Tomcat deploys applications automatically, and how you can configure customized logic for the mapping of context path to application file location (there are a handful of alternate ways to set this up other than the one I suggest above).
这篇关于将战争应用程序名称与战争文件名分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!