如何在运行Tomcat时获取软件包版本? [英] How to get package version at running Tomcat?

查看:198
本文介绍了如何在运行Tomcat时获取软件包版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在运行Tomcat时获取软件包版本?

How to get package version at running Tomcat?

我尝试getClass()。getPackage()。getImplementationVersion(),但总是返回
我猜这是因为我的战争还没有打包,Tomcat执行。类(又名爆炸式战争)。
META-INF \MANIFEST.MF 存在于最后的战争中,但不在 target\< project.name> \\ \\ META-INF 文件夹

I try getClass().getPackage().getImplementationVersion() but this always returns null. I guess it is because my war wasn't yet packaged and Tomcat executes .classes (aka exploded war). META-INF\MANIFEST.MF is present in final war, but not in target\<project.name>\META-INF folder


java.lang.Package

包对象包含关于实现
和Java包的规范的版本信息。该版本信息是
检索,并由ClassLoader实例提供加载
该类(es)。通常,它存储在与类分发的
的清单中。

Package objects contain version information about the implementation and specification of a Java package. This versioning information is retrieved and made available by the ClassLoader instance that loaded the class(es). Typically, it is stored in the manifest that is distributed with the classes.

相关是在运行时获取Maven工件版本

更新。之前我已经添加了战争构建的配置。但是当从Eclipse运行Tomcat时,我得到 null

UPDATE. Before I have already added configuration for war build. But when running Tomcat from Eclipse, I get null.

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1</version>
    <configuration>
        <archive>                   
            <manifest>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
            </manifest>
        </archive>
        <!-- this actually moves classes from \WEB-INF\classes to new jar 
        <archiveClasses>true</archiveClasses>
        -->
    </configuration>
</plugin>


推荐答案



解决1)在webapp文件夹中创建META-INF \MANIFEST.MF,使用 mvn war:manifest

2)编码

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

感谢@javadude在如何读取在apache tomcat中运行的webapp的清单文件?

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

这篇关于如何在运行Tomcat时获取软件包版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆