How to get package version at running Tomcat?

混江龙づ霸主 提交于 2019-11-30 20:30:20
Paul Verest

Solved by
1) Creating META-INF\MANIFEST.MF in webapp folder with mvn war:manifest
2) Coding

version  = getClass().getPackage().getImplementationVersion();  
if (version==null) {
    Properties prop = new Properties();
    try {
        prop.load(getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF"));
        version = prop.getProperty("Implementation-Version");
    } catch (IOException e) {
        logger.error(e.toString());
    }
}
logger.info("Starting App version "+version);

Thanks to @javadude answer at How do I read the manifest file for a webapp running in apache tomcat?

The normal source of a package's version / title / vendor information is attributes in the JAR file manifest. See the JAR File Specification.

If the getters return null that means that the corresponding attribute has not been specified. (And that is normal for regular JAR files too.)

Yes, it is related to the linked question. The accepted answer describes what to do to tell Maven to add some Maven version details to a JAR manifest so that you can access them at runtime using the Package API.


How to get package version at running Tomcat?

You need to create JAR files, and put the version info into the respective JAR manifests. Apparently you can also do this at the WAR file level.

Reference: "Deploy: War File Versioning and Manifest Reader" by Fred Puls.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!