简体   繁体   English

如何在 WAR 应用程序中读取 MANIFEST.MF?

[英]How to read MANIFEST.MF inside WAR application?

I would like to read MANIFEST.MF of my WAR application.我想阅读我的WAR应用程序的MANIFEST.MF How can I find its file name?我怎样才能找到它的文件名?

How can I find its file name?我怎样才能找到它的文件名?

You already have it.你已经拥有了。 Maybe you meant to find the absolute file location?也许你想找到绝对文件位置? You can use ServletContext#getRealPath() for this.您可以ServletContext#getRealPath()使用ServletContext#getRealPath()

String relativeWARPath = "/META-INF/MANIFEST.MF";
String absoluteDiskPath = getServletContext().getRealPath(relativeWARPath);
File file = new File(absoluteDiskPath);
// ...

Or if you want to get it as InputStream directly, use ServletContext#getResourceAsStream() .或者,如果您想直接将其作为InputStream获取,请使用ServletContext#getResourceAsStream()

InputStream input = getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF");
// ...

I had the same problem in every application and decided to create a component with a utility class in it: jcabi-manifests .我在每个应用程序中都遇到了同样的问题,并决定创建一个包含实用程序类的组件: jcabi-manifests Now it's easy to load any attribute from one of available MANIFEST.MF in classpath:现在很容易从类路径中的可用MANIFEST.MF之一加载任何属性:

import com.jcabi.manifests.Manifests;
String value = Manifests.read("My-Version");

Also, check this out:http://www.yegor256.com/2014/07/03/how-to-read-manifest-mf.html另外,看看这个:http ://www.yegor256.com/2014/07/03/how-to-read-manifest-mf.html

There are a few ways to do it, and the chosen answer is only works when you want to read Manifiest files on Servlet/SpringMVC layer or whatever layer you can access to ServletContext.有几种方法可以做到这一点,所选择的答案仅在您想要读取 Servlet/SpringMVC 层或您可以访问 ServletContext 的任何层上的 Manifying 文件时才有效。

However, if you want to read a value like "version" even before Servlet starts up, like during logback configuration or something else, you might need to do some old-way classloading or Manifest file manipulation.然而,如果你想在 Servlet 启动之前读取一个像“version”这样的值,比如在 logback 配置或其他事情期间,你可能需要做一些旧的类加载或 Manifest 文件操作。

I found this github repository(not mine) and it includes 4 different way to read information from Manifest file.我找到了这个 github 存储库(不是我的),它包含 4 种从清单文件中读取信息的不同方式。 If your situation is not accessible to ServletContext, check these out.如果 ServletContext 无法访问您的情况,请查看这些。

https://github.com/khmarbaise/version-examples https://github.com/khmarbaise/version-examples

I did some researches to find the right solution for me.我做了一些研究以找到适合我的解决方案。 Mainly thanks to BalusC but also others I cannot remember now because I went deeply in many sources.主要感谢BalusC ,还有其他一些我现在记不清了,因为我深入研究了许多来源。 I have a web application running on Weblogic 12c (12.2.1.4) version.我有一个在 Weblogic 12c (12.2.1.4) 版本上运行的 Web 应用程序。 First I had to say maven to add information in MANIFEST.MF, adding the following in plugins section of POM file首先我不得不说maven在MANIFEST.MF中添加信息,在POM文件的plugins部分添加以下内容

 <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.2</version>
    <configuration>
      <archive>
        <manifest> 
         <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
          <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
        </manifest>
      </archive>
    </configuration>
  </plugin>

You can find the version of maven plugin looking at the console in the package goal.您可以在包目标中的控制台中找到 maven 插件的版本。

Then I've mainly followed the BalusC direction in the post Using special auto start servlet to initialize on startup and share application data然后我主要遵循了使用特殊的自动启动 servlet 在启动时初始化并共享应用程序数据一文中的 BalusC 方向

Before registering the class in the servlet context in the contextInitialized methodcontextInitialized方法中在servlet上下文中注册类之前

 @Override public void contextInitialized(ServletContextEvent servletContextEvent)

I've put my business logic to retrieve information from the manifest, like the following我已经把我的业务逻辑从清单中检索信息,如下所示

    @Override public void contextInitialized(ServletContextEvent servletContextEvent) {
        logger.info("context initialized - reading MANIFEST.MF infos");
        StringBuilder welcomeStrBuild;
        welcomeStrBuild = new StringBuilder("version");
        try (InputStream inputStream = servletContextEvent.getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF")) {
            Properties prop = new Properties();
            prop.load(inputStream);
            welcomeStrBuild.append(prop.getProperty("Implementation-Version", "n/a"));
        } catch (Exception e) {
            logger.info("Unable to extract info from MANIFEST.MF");
            welcomeStrBuild.append("Unable to extract info from MANIFEST.MF");
        }
        infoProject = welcomeStrBuild.toString();
        servletContextEvent.getServletContext().setAttribute("config", this);
}

You can of course read the content of the manifest using the Java Manifest class, instead of using Properties class.您当然可以使用 Java Manifest 类读取清单的内容,而不是使用 Properties 类。

The main points of the solution are the two:解决方案的要点有两个:

  • add the information to the manifest using the maven war goal使用 Maven 战争目标将信息添加到清单
  • read the information at right moment and from the right WAR, because if you are not using the correct approach you end in reading the manifest from the classloader or other jars在正确的时间从正确的 WAR 中读取信息,因为如果您没有使用正确的方法,您最终会从类加载器或其他 jar 中读取清单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM