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

问题描述

我有一个包含清单文件的 web 应用程序,我在执行 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 中运行的 web 应用程序内部读取清单文件的正确方法是什么?

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?

推荐答案

也许你的副作用来自这样一个事实,即几乎所有的 jars 都包含一个 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 的清单文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 14:09