Example usage for java.util.jar Manifest getMainAttributes

List of usage examples for java.util.jar Manifest getMainAttributes

Introduction

In this page you can find the example usage for java.util.jar Manifest getMainAttributes.

Prototype

public Attributes getMainAttributes() 

Source Link

Document

Returns the main Attributes for the Manifest.

Usage

From source file:com.ikon.util.WarUtils.java

/**
 * //from   ww w.j  a va2  s .  c o  m
 */
public static synchronized void readAppVersion(ServletContext sc) {
    String appServerHome = sc.getRealPath("/");
    File manifestFile = new File(appServerHome, "META-INF/MANIFEST.MF");
    FileInputStream fis = null;

    try {
        fis = new FileInputStream(manifestFile);
        Manifest mf = new Manifest();
        mf.read(fis);
        Attributes atts = mf.getMainAttributes();
        String impVersion = atts.getValue("Implementation-Version");
        String impBuild = atts.getValue("Implementation-Build");
        log.info("Implementation-Version: " + impVersion);
        log.info("Implementation-Build: " + impBuild);

        if (impVersion != null) {
            String[] version = impVersion.split("\\.");

            if (version.length > 0 && version[0] != null) {
                appVersion.setMajor(version[0]);
            }

            if (version.length > 1 && version[1] != null) {
                appVersion.setMinor(version[1]);
            }

            if (version.length > 2 && version[2] != null && !version[2].equals("")) {
                appVersion.setMaintenance(version[2]);
            }
        }

        if (impBuild != null) {
            appVersion.setBuild(impBuild);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fis);
    }
}

From source file:com.xebialabs.deployit.cli.ext.mustachify.dar.DarManifestParser.java

private static DarManifest parse(Manifest manifest) {
    Attributes mainAttributes = manifest.getMainAttributes();
    validate(mainAttributes);//from   w  ww  .  j  av  a2s.com

    Iterable<DarManifestEntry> darEntries = filter(transform(manifest.getEntries().entrySet(),
            new Function<Entry<String, Attributes>, DarManifestEntry>() {
                @Override
                public DarManifestEntry apply(Entry<String, Attributes> input) {
                    String entryName = input.getKey();
                    Attributes entryAttributes = input.getValue();
                    return (DarManifestEntry.isDarEntry(entryName, entryAttributes)
                            ? DarManifestEntry.fromEntryAttributes(entryName, entryAttributes)
                            : DarManifestEntry.NULL);
                }
            }), not(new Predicate<Object>() {
                @Override
                public boolean apply(Object input) {
                    return (input == DarManifestEntry.NULL);
                }
            }));
    return new DarManifest(mainAttributes.getValue(APPLICATION_ATTRIBUTE_NAME),
            mainAttributes.getValue(VERSION_ATTRIBUTE_NAME), copyOf(darEntries));
}

From source file:ubicrypt.UbiCrypt.java

public static String getVersion() {
    String version = null;//from  www.ja va  2s.c o  m
    // try to load from maven properties first
    try {
        try (final InputStream is = UbiCrypt.class.getResourceAsStream("META-INF/MANIFEST.MF")) {
            if (is != null) {
                Manifest manifest = new Manifest(is);
                version = manifest.getMainAttributes().getValue("Implementation-Version");
            }
        }
    } catch (final Exception e) {
        // ignore
    }
    // fallback to using Java API
    if (version == null) {
        final Package aPackage = UbiCrypt.class.getPackage();
        if (aPackage != null) {
            version = aPackage.getImplementationVersion();
            if (version == null) {
                version = aPackage.getSpecificationVersion();
            }
        }
    }
    if (version == null) {
        // we could not compute the version so use a blank
        version = "";
    }
    return version;
}

From source file:com.tesora.dve.common.PELogUtils.java

private static Attributes readManifestFile() throws PEException {
    try {/* w ww .j  a v a 2 s. c  o m*/
        Enumeration<URL> resources = PELogUtils.class.getClassLoader().getResources(MANIFEST_FILE_NAME);

        Attributes attrs = new Attributes();
        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            if (url.toString().contains(CORE_PROJECT_NAME)) {
                Manifest manifest = new Manifest(url.openStream());
                attrs = manifest.getMainAttributes();
                break;
            }
        }
        return attrs;
    } catch (Exception e) {
        throw new PEException("Error retrieving build manifest", e);
    }
}

From source file:org.talend.designer.maven.utils.ClasspathsJarGenerator.java

public static String createJar(Property property, String classpath, String separator) throws Exception {
    String newClasspath = generateClasspathForManifest(classpath, separator);

    Manifest manifest = new Manifest();
    Attributes a = manifest.getMainAttributes();
    a.put(Attributes.Name.MANIFEST_VERSION, "1.0"); //$NON-NLS-1$
    a.put(Attributes.Name.IMPLEMENTATION_VENDOR, "Talend Open Studio"); //$NON-NLS-1$
    a.put(Attributes.Name.CLASS_PATH, newClasspath);

    String jarLocation = getJarLocation(property);
    File jarFile = new File(jarLocation);
    if (!jarFile.exists()) {
        jarFile.createNewFile();/*w  ww  . j  ava  2 s  . c o m*/
    }
    JarOutputStream stream = null;
    try {
        stream = new JarOutputStream(new FileOutputStream(jarLocation), manifest);
        stream.flush();
    } finally {
        stream.close();
    }

    return getFinalClasspath(classpath, separator, jarLocation);
}

From source file:com.thoughtworks.go.agent.common.util.JarUtil.java

private static String getManifestKey(JarFile jarFile, String key) {
    try {/*from ww  w  .ja v a  2 s  .c  o  m*/
        Manifest manifest = jarFile.getManifest();
        if (manifest != null) {
            Attributes attributes = manifest.getMainAttributes();
            return attributes.getValue(key);
        }
    } catch (IOException e) {
        LOG.error("Exception while trying to read key {} from manifest of {}", key, jarFile.getName(), e);
    }
    return null;
}

From source file:ezbake.deployer.utilities.VersionHelper.java

private static String getVersionFromManifest(File artifact) throws IOException {
    String versionNumber = null;// www  . j a v  a  2 s. c  om
    try (JarFile jar = new JarFile(artifact)) {
        Manifest manifest = jar.getManifest();
        Attributes attributes = manifest.getMainAttributes();
        if (attributes != null) {
            for (Object o : attributes.keySet()) {
                Attributes.Name key = (Attributes.Name) o;
                String keyword = key.toString();
                if (keyword.equals("Implementation-Version") || keyword.equals("Bundle-Version")) {
                    versionNumber = (String) attributes.get(key);
                    break;
                }
            }
        }
    }
    return versionNumber;
}

From source file:org.sourcepit.common.maven.testing.ArtifactRepositoryFacade.java

private static File createStubJar(File dir) throws IOException {
    final File jarFile = File.createTempFile("stub", ".jar", dir);

    JarOutputStream jarOut = null;
    try {// www.  j  a  va  2s  .  c  om
        jarOut = new JarOutputStream(new FileOutputStream(jarFile));

        final JarEntry mfEntry = new JarEntry(JarFile.MANIFEST_NAME);
        jarOut.putNextEntry(mfEntry);

        final Manifest mf = new Manifest();
        mf.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1");
        mf.write(jarOut);

        jarOut.closeEntry();
    } finally {
        IOUtils.closeQuietly(jarOut);
    }

    return jarFile;
}

From source file:org.talend.designer.maven.utils.ClasspathsJarGenerator.java

public static String getClasspathFromManifest(Property property) throws Exception {
    String jarLocation = getJarLocation(property);
    JarInputStream stream = null;
    try {/*from w  w  w  .  j  a v  a 2 s  .co  m*/
        stream = new JarInputStream(new FileInputStream(jarLocation));
        Manifest manifest = stream.getManifest();
        String classpath = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
        return classpath;
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}

From source file:com.igormaznitsa.jcp.it.maven.ITPreprocessorMojo.java

private static void assertMainClass(final String jarFile, final String mainClass) throws Exception {
    JarInputStream jarStream = null;
    try {/* w w  w  . j a v  a 2s  . c  o m*/
        jarStream = new JarInputStream(new FileInputStream(jarFile));
        final Manifest manifest = jarStream.getManifest();
        final Attributes attrs = manifest.getMainAttributes();
        assertEquals("Maven plugin must also provide and main class in manifest", mainClass,
                attrs.getValue("Main-Class"));
    } finally {
        IOUtils.closeQuietly(jarStream);
    }
}