如何读取在apache tomcat中运行的webapp的清单文件? [英] How do I read the manifest file for a webapp running in apache tomcat?

查看:299
本文介绍了如何读取在apache tomcat中运行的webapp的清单文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含清单文件的webapp,我在其中编写了一个ant构建任务期间我的应用程序的当前版本。清单文件是正确创建的,但是当我尝试在运行时读取它时,我会得到一些奇怪的副作用。我在清单中读取的代码是这样的:

I have a webapp which contains a manifest file, in which I write the current version of my application during an ant build task. The manifest file is created correctly, but when I try to read it in during runtime, I get some strange side-effects. My code for reading in the manifest is something like this:

    InputStream manifestStream = Thread.currentThread()
                                 .getContextClassLoader()
                                 .getResourceAsStream("META-INFFFF/MANIFEST.MF");
    try {
        Manifest manifest = new Manifest(manifestStream);
        Attributes attributes = manifest.getMainAttributes();
        String impVersion = attributes.getValue("Implementation-Version");
        mVersionString = impVersion;
    }
    catch(IOException ex) {
        logger.warn("Error while reading version: " + ex.getMessage());
    }

当我将eclipse附加到tomcat时,我看到上面的代码有效,但是它似乎获得了一个不同于我预期的清单文件,我可以告诉它,因为ant版本和构建时间戳都是不同的。然后,我在那里放了META-INFFFF,上面的代码仍然有效!这意味着我正在阅读其他一些清单,而不是我的清单。我也试过

When I attach eclipse to tomcat, I see that the above code works, but it seems to get a different manifest file than the one I expected, which I can tell because the ant version and build timestamp are both different. Then, I put "META-INFFFF" in there, and the above code still works! This means that I'm reading some other manifest, not mine. I also tried

this.getClass().getClassLoader().getResourceAsStream(...)

但结果是一样的。从tomcat中运行的webapp内部读取清单文件的正确方法是什么?

But the result was the same. What's the proper way to read the manifest file from inside of a webapp running in tomcat?

编辑:感谢您的建议到目前为止。另外,我应该注意我运行tomcat独立;我从命令行启动它,然后附加到Eclipse调试器中正在运行的实例。这不应该有所不同,是吗?

Edit: Thanks for the suggestions so far. Also, I should note that I am running tomcat standalone; I launch it from the command line, and then attach to the running instance in Eclipse's debugger. That shouldn't make a difference, should it?

推荐答案

也许你的副作用来自几乎所有的罐子都包括的事实一个MANIFEST.MF,你没有得到正确的。要从webapp读取MANIFEST.MF,我会说:

Maybe your side-effects come from the fact that almost all jars include a MANIFEST.MF and you're not getting the right one. To read the MANIFEST.MF from the webapp, I would say:

ServletContext application = getServletConfig().getServletContext();
InputStream inputStream = application.getResourceAsStream("/META-INF/MANIFEST.MF");
Manifest manifest = new Manifest(inputStream);

请注意,从Eclipse运行Tomcat与单独运行Tomcat不同,因为Eclipse使用类加载器。

Please note that running Tomcat from Eclipse is not the same as running Tomcat alone as Eclipse plays with the classloader.

这篇关于如何读取在apache tomcat中运行的webapp的清单文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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