4

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));

4 Answers 4

5

I think the simplest solution is using the same version (SVN revision + padding as an example) in .war, web.xml and META-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/WAR

See MANIFEST.MF version-number

2

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 "/"

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

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

1
  • 1
    Im getting this variable empty. Is it necessary to to do some configuration or should it work out of the box?
    – dvsaraiva
    Commented Apr 28, 2020 at 14:31
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.