Tomcat 7 - 检索 web 应用程序的版本(版本化 WAR)
我一直无法找到任何简单的方法来确定使用 Tomcat 7 版本命名部署的 WAR 文件的版本字符串(即 app##version.war)。您可以在此处了解它以及它的功能< a href="http://www.javacodegeeks.com/2011/06/zero-downtime-deployment-and-rollback.html" rel="nofollow">此处。
除了通常的瑞士军刀反射驱动的胸腔破裂之外,如果有一种更受支持的方法那就太好了:
final ServletContextEvent event ...
final ServletContext applicationContextFacade = event.getServletContext();
final Field applicationContextField = applicationContextFacade.getClass().getDeclaredField("context");
applicationContextField.setAccessible(true);
final Object applicationContext = applicationContextField.get(applicationContextFacade);
final Field standardContextField = applicationContext.getClass().getDeclaredField("context");
standardContextField.setAccessible(true);
final Object standardContext = standardContextField.get(applicationContext);
final Method webappVersion = standardContext.getClass().getMethod("getWebappVersion");
System.err.println("WAR version: " + webappVersion.invoke(standardContext));
I've been unable to find any easy way of figuring out the version string for a WAR file deployed with Tomcat 7 versioned naming (ie app##version.war). You can read about it here and what it enables here.
It'd be nice if there was a somewhat more supported approach other than the usual swiss army knife of reflection powered ribcage cracking:
final ServletContextEvent event ...
final ServletContext applicationContextFacade = event.getServletContext();
final Field applicationContextField = applicationContextFacade.getClass().getDeclaredField("context");
applicationContextField.setAccessible(true);
final Object applicationContext = applicationContextField.get(applicationContextFacade);
final Field standardContextField = applicationContext.getClass().getDeclaredField("context");
standardContextField.setAccessible(true);
final Object standardContext = standardContextField.get(applicationContext);
final Method webappVersion = standardContext.getClass().getMethod("getWebappVersion");
System.err.println("WAR version: " + webappVersion.invoke(standardContext));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为最简单的解决方案是在
.war
、web.xml
和META-INF/MANIFEST 中使用相同的版本(例如 SVN 修订版 + 填充)。 MF
属性文件,因此您可以稍后在您的应用程序或任何从 JAR/WAR 读取版本的标准工具中检索这些文件的版本,请参阅MANIFEST.MF 版本号
I think the simplest solution is using the same version (SVN revision + padding as an example) in
.war
,web.xml
andMETA-INF/MANIFEST.MF
properties files, so you could retrieve the version of these files later in your APP or any standard tool that read version from a JAR/WARSee MANIFEST.MF version-number
此处描述的另一个解决方案使用部署的 WAR 的服务器。您可以从“##”和“/”之间的字符串中提取版本号
Another solution described here uses the path name on the server of the deployed WAR. You'd extract the version number from the string between the "##" and the "/"
从 Tomcat 版本 9.0.32、8.5.52 和 7.0.101 开始,webapp 版本作为名称为 org.apache.catalina.webappVersion 的 ServletContext 属性公开。
已关闭增强请求的链接:https://bz.apache.org/bugzilla /show_bug.cgi?id=64189
Starting from Tomcat versions 9.0.32, 8.5.52 and 7.0.101, the webapp version is exposed as a ServletContext attribute with the name
org.apache.catalina.webappVersion
.Link to the closed enhancement request: https://bz.apache.org/bugzilla/show_bug.cgi?id=64189
最简单的方法是 Tomcat 通过 ServletContext 属性 (org.apache.catalina.core.StandardContext.webappVersion) 或类似属性提供版本。做到这一点的补丁是微不足道的。我建议在 Tomcat 的 Bugzilla 中打开增强请求。如果您包含补丁,那么它应该会很快得到应用。
The easiest way would be for Tomcat to make the version available via a ServletContext attribute (org.apache.catalina.core.StandardContext.webappVersion) or similar. The patch to do that would be trivial. I'd suggest opening an enhancement request in Tomcat's Bugzilla. If you include a patch then it should get applied fairly quickly.