Code Snippet

Just another Code Snippet site

[Java] Read MANIFEST.MF information

ManifestHelper Class :

public final class ManifestHelper {

    private static final Logger LOGGER = Logger.getLogger(ManifestHelper.class);

    /** 'Implementation-Title' Field. */
    public static final String MF_IMPL_TITLE = "Implementation-Title";

    /** 'Implementation-Version' Field. */
    public static final String MF_IMPL_VERSION = "Implementation-Version";

    /** 'Implementation-Version-Revision' Field. */
    public static final String MF_IMPL_REVISION = "Implementation-Version-Revision";

    /**
     * Read manifest information from JAR file.
     * 
     * @param jarURL URL of JAR file to read
     * @param paramsToRead list of params to read from manifest file
     */
    public static void readLibInformation(final URL jarURL, final String... paramsToRead) {
        try {
            LOGGER.info("Lib : " + jarURL.toURI().toASCIIString());
            JarInputStream jarStream = new JarInputStream(jarURL.openStream());
            Manifest manifest = jarStream.getManifest();
            readManifestInformation(manifest, paramsToRead);
            jarStream.close();

        } catch (Exception e) {
            LOGGER.warn("readLibInformation", e);
        }
    }

    /**
     * Read Lib information from MANIFEST file.
     * 
     * @param manifestURL URL of JAR file to read
     * @param paramsToRead list of params to read from manifest file
     */
    public static void readManifestInformation(final URL manifestURL, final String... paramsToRead) {
        try {
            readManifestInformation(new Manifest(manifestURL.openStream()), paramsToRead);
        } catch (Exception e) {
            LOGGER.warn("readManifestInformation", e);
        }
    }

    /**
     * Read Lib information from MANIFEST file.
     * 
     * @param manifest  to read
     * @param paramsToRead list of params to read from manifest file
     */
    public static void readManifestInformation(final Manifest manifest, final String... paramsToRead) {
        try {
            Attributes attr = manifest.getMainAttributes();

            for (String param : paramsToRead) {
                String tmpValue = attr.getValue(param);
                if (tmpValue != null) {
                    LOGGER.info(param + " : " + tmpValue);
                }
            }

        } catch (Exception e) {
            LOGGER.warn("readManifestInformation", e);
        }
    }
}

Read Manifest Information (from WebApp) :

URL manifestUrl = this.getClass().getClassLoader().getResource("/META-INF/MANIFEST.MF");
ManifestHelper.readManifestInformation(manifestUrl, ManifestHelper.MF_IMPL_TITLE, ManifestHelper.MF_IMPL_VERSION, ManifestHelper.MF_IMPL_REVISION);

Read Manifest Information (from JAR file) :

URL jarURL = new URL("file:"+jarFile.getAbsoluteFile());
ManifestHelper.readLibInformation(jarURL, ManifestHelper.MF_IMPL_TITLE, ManifestHelper.MF_IMPL_VERSION, ManifestHelper.MF_IMPL_REVISION);

, ,


Comments are currently closed.