Tomcat 7 - Extract the webapp version (WAR version)

I was unable to find any easy way to figure out the version string for the WAR file deployed with the Tomcat 7 name version (e.g. app ## version.war). You can read about it here and what it allows here .

It would be nice if there was a slightly more supported approach, different from the usual Swiss army knife with reflex feed on the chest:

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)); 
+4
source share
3 answers

I think the easiest solution is to use the same version (SVN version + addition as an example) in the .war , web.xml and META-INF/MANIFEST.MF property files, so you can get the version of these files later in your APP or any standard tool that reads a version from a JAR / WAR

See version version MANIFEST.MF

+5
source

Another solution described here uses the path name on the deployed WAR server. You extract the version number from the line between "##" and "/"

 runningVersion = StringUtils.substringBefore( StringUtils.substringAfter( servletConfig.getServletContext().getRealPath("/"), "##"), "/"); 
+2
source

The easiest way for Tomcat is to make the version available through the ServletContext attribute (org.apache.catalina.core.StandardContext.webappVersion) or similar. The patch for this will be trivial. I suggest opening an extension request in Tomcat Bugzilla. If you enable the patch, then it should be applied pretty quickly.

0
source

Source: https://habr.com/ru/post/1382527/


All Articles