我一直找不到任何简单的方法来弄清楚使用Tomcat7版本命名(即app##version.war)部署的WAR文件的版本字符串。您可以阅读有关它的here和它支持here的内容。
如果有一种更受支持的方法,而不是通常的瑞士军刀反射动力胸腔破解,那就更好了:
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));发布于 2012-11-06 19:24:59
我认为最简单的解决方案是在.war、web.xml和META-INF/MANIFEST.MF属性文件中使用相同的版本(SVN版本+填充作为示例),这样以后您就可以在应用程序或任何从JAR/WAR读取版本的标准工具中检索这些文件的版本
请参阅MANIFEST.MF
发布于 2013-11-21 00:50:00
here描述的另一个解决方案使用部署的WAR服务器上的路径名。您可以从"##“和"/”之间的字符串中提取版本号
runningVersion = StringUtils.substringBefore(
StringUtils.substringAfter(
servletConfig.getServletContext().getRealPath("/"),
"##"),
"/");发布于 2020-03-03 19:02:34
从Tomcat9.0.32、8.5.52和7.0.101版本开始,webapp版本被公开为名称为org.apache.catalina.webappVersion的ServletContext属性。
已关闭的增强请求链接:https://bz.apache.org/bugzilla/show_bug.cgi?id=64189
https://stackoverflow.com/questions/8230310
复制相似问题