Example usage for java.util.jar Manifest Manifest

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

Introduction

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

Prototype

public Manifest() 

Source Link

Document

Constructs a new, empty Manifest.

Usage

From source file:com.isomorphic.maven.util.ArchiveUtils.java

/**
 * Builds a JAR file from the contents of a directory on the filesystem (recursively).
 * Adapted from stackoverflow solution.  
 * //from w  w  w  . j a  va 2  s . c  o  m
 * @see http://stackoverflow.com/questions/1281229/how-to-use-jaroutputstream-to-create-a-jar-file
 * 
 * @param directory the directory containing the content to be xzipped up
 * @param output the zip file to be written to
 */
public static void jar(File directory, File output) throws IOException {
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    output.getParentFile().mkdirs();
    JarOutputStream target = new JarOutputStream(new FileOutputStream(output), manifest);
    zip(directory, directory, target);
    target.close();
}

From source file:org.apache.hadoop.hbase.util.ClassLoaderTestHelper.java

/**
 * Jar a list of files into a jar archive.
 *
 * @param archiveFile the target jar archive
 * @param tobejared a list of files to be jared
 *///from w  w w  .j  a  v a 2  s .  co  m
private static boolean createJarArchive(File archiveFile, File[] tobeJared) {
    try {
        byte buffer[] = new byte[BUFFER_SIZE];
        // Open archive file
        FileOutputStream stream = new FileOutputStream(archiveFile);
        JarOutputStream out = new JarOutputStream(stream, new Manifest());

        for (int i = 0; i < tobeJared.length; i++) {
            if (tobeJared[i] == null || !tobeJared[i].exists() || tobeJared[i].isDirectory()) {
                continue;
            }

            // Add archive entry
            JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
            jarAdd.setTime(tobeJared[i].lastModified());
            out.putNextEntry(jarAdd);

            // Write file to archive
            FileInputStream in = new FileInputStream(tobeJared[i]);
            while (true) {
                int nRead = in.read(buffer, 0, buffer.length);
                if (nRead <= 0)
                    break;
                out.write(buffer, 0, nRead);
            }
            in.close();
        }
        out.close();
        stream.close();
        LOG.info("Adding classes to jar file completed");
        return true;
    } catch (Exception ex) {
        LOG.error("Error: " + ex.getMessage());
        return false;
    }
}

From source file:io.zatarox.satellite.impl.BackgroundWrapperTest.java

@BeforeClass
public static void setBefore() throws Exception {
    file = File.createTempFile("archive", ".jar");
    file.deleteOnExit();/*from ww w.  ja  va  2 s. c  o m*/
    final FileOutputStream out = new FileOutputStream(file);
    ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP,
            out);
    ZipArchiveEntry entry = new ZipArchiveEntry("META-INF/MANIFEST.MF");
    archive.putArchiveEntry(entry);
    final Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    manifest.getMainAttributes().putValue("Background-Process-Class",
            FakeBackgroundProcessImpl.class.getName());
    manifest.write(archive);
    archive.closeArchiveEntry();
    archive.close();
}

From source file:de.u808.simpleinquest.web.tags.VersionInfoTag.java

@Override
public void doTag() throws JspException, IOException {
    PageContext pageContext = (PageContext) getJspContext();
    JspWriter out = pageContext.getOut();
    try {/*from   w w  w .j a v a 2s .c  o m*/
        String appServerHome = pageContext.getServletContext().getRealPath("/");

        File manifestFile = new File(appServerHome, "META-INF/MANIFEST.MF");

        Manifest mf = new Manifest();
        mf.read(new FileInputStream(manifestFile));

        Attributes atts = mf.getMainAttributes();

        out.println("<span id=\"version\"> (Revision " + atts.getValue("Implementation-Version") + " Build "
                + atts.getValue("Implementation-Build") + " Built-By " + atts.getValue("Built-By")
                + ")</span>");
    } catch (Exception e) {
        log.error("Tag error", e);
    }
}

From source file:org.sonar.api.utils.ManifestUtilsTest.java

@Test
public void emptyManifest() throws Exception {
    Manifest mf = new Manifest();
    File jar = createJar(mf, "emptyManifest.jar");

    URLClassLoader classloader = new URLClassLoader(FileUtils.toURLs(new File[] { jar }));
    assertThat(ManifestUtils.getPropertyValues(classloader, "foo").size(), is(0));
}

From source file:org.wso2.carbon.server.extensions.FragmentBundleCreator.java

public void perform() {
    File[] files = getBundleConfigs();
    if (files.length > 0) {
        for (File file : files) {
            String fragmentHostBundleName = getFragmentHostBundleName(file);
            String fragmentBundleName = getFragmentBundleName(file);

            try {
                Manifest mf = new Manifest();
                Attributes attribs = mf.getMainAttributes();
                attribs.putValue(LauncherConstants.MANIFEST_VERSION, "1.0");
                attribs.putValue(LauncherConstants.BUNDLE_MANIFEST_VERSION, "2");
                attribs.putValue(LauncherConstants.BUNDLE_NAME, fragmentBundleName);
                attribs.putValue(LauncherConstants.BUNDLE_SYMBOLIC_NAME, fragmentBundleName);
                attribs.putValue(LauncherConstants.BUNDLE_VERSION, FRAGMENT_BUNDLE_VERSION);
                attribs.putValue(LauncherConstants.FRAGMENT_HOST, fragmentHostBundleName);
                attribs.putValue(LauncherConstants.BUNDLE_CLASSPATH, ".");

                File dropinsFolder = new File(Utils.getCarbonComponentRepo(), "dropins");
                String targetFilePath = dropinsFolder.getAbsolutePath() + File.separator + fragmentBundleName
                        + "_" + FRAGMENT_BUNDLE_VERSION + ".jar";

                String tempDirPath = Utils.JAR_TO_BUNDLE_DIR + File.separator + System.currentTimeMillis()
                        + Math.random();

                FileOutputStream mfos = null;
                try {
                    if (file.isDirectory()) {
                        FileUtils.copyDirectory(file, new File(tempDirPath));
                    } else { // is a single file..
                        Utils.copyFileToDir(file, new File(tempDirPath));
                    }//from  w ww  .  ja va 2  s. c o m
                    String metaInfPath = tempDirPath + File.separator + "META-INF";
                    if (!new File(metaInfPath).mkdirs()) {
                        throw new IOException("Failed to create the directory: " + metaInfPath);
                    }
                    mfos = new FileOutputStream(metaInfPath + File.separator + "MANIFEST.MF");
                    mf.write(mfos);

                    Utils.archiveDir(targetFilePath, tempDirPath);
                    Utils.deleteDir(new File(tempDirPath));
                } finally {
                    try {
                        if (mfos != null) {
                            mfos.close();
                        }
                    } catch (IOException e) {
                        log.error("Unable to close the OutputStream " + e.getMessage(), e);
                    }
                }
            } catch (IOException e) {
                log.error("Error occured while creating the log4j prop fragment bundle.", e);
            }
        }
    }
}

From source file:org.eclipse.gemini.blueprint.test.internal.util.ManifestUtilsTest.java

public void testEmptyManifest() throws Exception {
    Manifest mf = new Manifest();
    mf.getMainAttributes().putValue("foo", "bar");
    createJar(mf);// www .java2  s . co  m
    in = new JarInputStream(storage.getInputStream());
    assertEquals(mf, in.getManifest());
}

From source file:org.mule.config.MuleManifest.java

public static Manifest getManifest() {
    if (manifest == null) {
        manifest = new Manifest();

        InputStream is = null;//w w  w  .j ava2s  .c  o  m
        try {
            // We want to load the MANIFEST.MF from the mule-core jar. Sine we
            // don't know the version we're using we have to search for the jar on the classpath
            URL url = AccessController.doPrivileged(new UrlPrivilegedAction());

            if (url != null) {
                is = url.openStream();
            }

            if (is != null) {
                manifest.read(is);
            }
        } catch (IOException e) {
            logger.warn("Failed to read manifest Info, Manifest information will not display correctly: "
                    + e.getMessage());
        }
    }
    return manifest;
}

From source file:org.wso2.carbon.server.extensions.Log4jPropFileFragmentBundleCreator.java

public void perform() {

    //Get the log4j.properties file path.
    //Calculate the target fragment bundle file name with required manifest headers.
    //org.wso2.carbon.logging.propfile_1.0.0.jar

    try {//from  w  w  w.  ja  va 2 s  .c  o  m

        Manifest mf = new Manifest();
        Attributes attribs = mf.getMainAttributes();
        attribs.putValue(LauncherConstants.MANIFEST_VERSION, "1.0");
        attribs.putValue(LauncherConstants.BUNDLE_MANIFEST_VERSION, "2");
        attribs.putValue(LauncherConstants.BUNDLE_NAME, FRAGMENT_BUNDLE_NAME);
        attribs.putValue(LauncherConstants.BUNDLE_SYMBOLIC_NAME, FRAGMENT_BUNDLE_NAME);
        attribs.putValue(LauncherConstants.BUNDLE_VERSION, FRAGMENT_BUNDLE_VERSION);
        attribs.putValue(LauncherConstants.FRAGMENT_HOST, FRAGMENT_HOST_BUNDLE_NAME);
        attribs.putValue(LauncherConstants.BUNDLE_CLASSPATH, ".");

        File confFolder = new File(Utils.getCarbonComponentRepo(), "../conf");
        String loggingPropFilePath = confFolder.getAbsolutePath() + File.separator + LOG4J_PROP_FILE_NAME;

        File dropinsFolder = new File(Utils.getCarbonComponentRepo(), "dropins");
        String targetFilePath = dropinsFolder.getAbsolutePath() + File.separator + FRAGMENT_BUNDLE_NAME + "_"
                + FRAGMENT_BUNDLE_VERSION + ".jar";

        String tempDirPath = Utils.JAR_TO_BUNDLE_DIR + File.separator + System.currentTimeMillis()
                + Math.random();

        FileOutputStream mfos = null;
        try {

            Utils.copyFileToDir(new File(loggingPropFilePath), new File(tempDirPath));
            String metaInfPath = tempDirPath + File.separator + "META-INF";
            if (!new File(metaInfPath).mkdirs()) {
                throw new IOException("Failed to create the directory: " + metaInfPath);
            }
            mfos = new FileOutputStream(metaInfPath + File.separator + "MANIFEST.MF");
            mf.write(mfos);

            Utils.archiveDir(targetFilePath, tempDirPath);
            Utils.deleteDir(new File(tempDirPath));
        } finally {
            try {
                if (mfos != null) {
                    mfos.close();
                }
            } catch (IOException e) {
                log.error("Unable to close the OutputStream " + e.getMessage(), e);
            }
        }

    } catch (IOException e) {
        log.error("Error occured while creating the log4j prop fragment bundle.", e);
    }

    //Utils.createBundle(log4jPropFile, targetFragmentBundle, mf);
    //To change body of implemented methods use File | Settings | File Templates.
}

From source file:oz.tez.deployment.utils.ClassPathUtils.java

/**
 * Will create a JAR file frombase dir/*from  w  w  w. j  ava  2  s .c om*/
 *
 * @param source
 * @param jarName
 * @return
 */
public static File toJar(File source, String jarName) {
    if (!source.isAbsolute()) {
        throw new IllegalArgumentException("Source must be expressed through absolute path");
    }
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    File jarFile = new File(jarName);
    try {
        JarOutputStream target = new JarOutputStream(new FileOutputStream(jarFile), manifest);
        add(source, source.getAbsolutePath().length(), target);
        target.close();
    } catch (Exception e) {
        throw new IllegalStateException(
                "Failed to create JAR file '" + jarName + "' from " + source.getAbsolutePath(), e);
    }
    return jarFile;
}