首页 > 解决方案 > 如何从战争应用程序中获取 manifest.mf 文件

问题描述

我想显示所有工件名称及其部署在 jboss eap 服务器中部署在同一服务器中的 web 应用程序中的实现版本详细信息。如何从另一个 web 应用程序中的 war 文件中读取清单文件。

sce.getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF"); 仅用于从 war servlet 上下文中获取清单文件。

public class Version implements ServletContextListener {
    private static final Logger LOG = LoggerFactory.getLogger(Version.class);

    private static Attributes sMainManifestAttributes;
    public static final String ARTIFACT_ID = "Implementation-Title";
    public static final String ARTIFACT_VERSION = "Implementation-Version";

    /**
     * Read the manifest from /META-INF/MANIFEST.MF
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try {
            ServletContext application = sce.getServletContext();
            InputStream inputStream = application.getResourceAsStream("/META-INF/MANIFEST.MF");
            Manifest manifest = new Manifest(inputStream);
            sMainManifestAttributes = manifest.getMainAttributes();
            LOG.info("BIH Artifact Name:" + sMainManifestAttributes.getValue(ARTIFACT_ID) + " Artifact Version :"
                    + sMainManifestAttributes.getValue(ARTIFACT_VERSION));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        sMainManifestAttributes = null;
    }

    /**
     * Generic querying of the manifest.
     * 
     * @return The result, as run through String.trim()
     */
    public static String getValue(String name) {
        return sMainManifestAttributes.getValue(name).trim();
    }


}

标签: javajspservletsjboss-eap-6

解决方案


推荐阅读