Search in sources :

Example 56 with Manifest

use of java.util.jar.Manifest in project beam by apache.

the class ApexYarnLauncher method createJar.

/**
   * Create a jar file from the given directory.
   * @param dir source directory
   * @param jarFile jar file name
   * @throws IOException when file cannot be created
   */
public static void createJar(File dir, File jarFile) throws IOException {
    final Map<String, ?> env = Collections.singletonMap("create", "true");
    if (jarFile.exists() && !jarFile.delete()) {
        throw new RuntimeException("Failed to remove " + jarFile);
    }
    URI uri = URI.create("jar:" + jarFile.toURI());
    try (final FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
        File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
        Files.createDirectory(zipfs.getPath("META-INF"));
        try (final OutputStream out = Files.newOutputStream(zipfs.getPath(JarFile.MANIFEST_NAME))) {
            if (!manifestFile.exists()) {
                new Manifest().write(out);
            } else {
                FileUtils.copyFile(manifestFile, out);
            }
        }
        final java.nio.file.Path root = dir.toPath();
        Files.walkFileTree(root, new java.nio.file.SimpleFileVisitor<Path>() {

            String relativePath;

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                relativePath = root.relativize(dir).toString();
                if (!relativePath.isEmpty()) {
                    if (!relativePath.endsWith("/")) {
                        relativePath += "/";
                    }
                    if (!relativePath.equals("META-INF/")) {
                        final Path dstDir = zipfs.getPath(relativePath);
                        Files.createDirectory(dstDir);
                    }
                }
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String name = relativePath + file.getFileName();
                if (!JarFile.MANIFEST_NAME.equals(name)) {
                    try (final OutputStream out = Files.newOutputStream(zipfs.getPath(name))) {
                        FileUtils.copyFile(file.toFile(), out);
                    }
                }
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                relativePath = root.relativize(dir.getParent()).toString();
                if (!relativePath.isEmpty() && !relativePath.endsWith("/")) {
                    relativePath += "/";
                }
                return super.postVisitDirectory(dir, exc);
            }
        });
    }
}
Also used : Path(java.nio.file.Path) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) URI(java.net.URI) FileSystem(java.nio.file.FileSystem) JarFile(java.util.jar.JarFile) File(java.io.File) Path(java.nio.file.Path) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 57 with Manifest

use of java.util.jar.Manifest in project spring-boot by spring-projects.

the class TestJarCreator method writeManifest.

private static void writeManifest(JarOutputStream jarOutputStream, String name) throws Exception {
    writeDirEntry(jarOutputStream, "META-INF/");
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().putValue("Built-By", name);
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    jarOutputStream.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
    manifest.write(jarOutputStream);
    jarOutputStream.closeEntry();
}
Also used : ZipEntry(java.util.zip.ZipEntry) Manifest(java.util.jar.Manifest)

Example 58 with Manifest

use of java.util.jar.Manifest in project spring-boot by spring-projects.

the class RepackagerTests method mainClassFromManifest.

@Test
public void mainClassFromManifest() throws Exception {
    this.testJarFile.addClass("a/b/C.class", ClassWithoutMainMethod.class);
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
    manifest.getMainAttributes().putValue("Main-Class", "a.b.C");
    this.testJarFile.addManifest(manifest);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    repackager.repackage(NO_LIBRARIES);
    Manifest actualManifest = getManifest(file);
    assertThat(actualManifest.getMainAttributes().getValue("Main-Class")).isEqualTo("org.springframework.boot.loader.JarLauncher");
    assertThat(actualManifest.getMainAttributes().getValue("Start-Class")).isEqualTo("a.b.C");
    assertThat(hasLauncherClasses(file)).isTrue();
}
Also used : Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.Test)

Example 59 with Manifest

use of java.util.jar.Manifest in project robovm by robovm.

the class URLClassLoader method createURLJarHandler.

private URLHandler createURLJarHandler(URL url) {
    String prefixName;
    String file = url.getFile();
    if (url.getFile().endsWith("!/")) {
        prefixName = "";
    } else {
        int sepIdx = file.lastIndexOf("!/");
        if (sepIdx == -1) {
            // Invalid URL, don't look here again
            return null;
        }
        sepIdx += 2;
        prefixName = file.substring(sepIdx);
    }
    try {
        URL jarURL = ((JarURLConnection) url.openConnection()).getJarFileURL();
        JarURLConnection juc = (JarURLConnection) new URL("jar", "", jarURL.toExternalForm() + "!/").openConnection();
        JarFile jf = juc.getJarFile();
        URLJarHandler jarH = new URLJarHandler(url, jarURL, jf, prefixName);
        if (jarH.getIndex() == null) {
            try {
                Manifest manifest = jf.getManifest();
                if (manifest != null) {
                    String classpath = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
                    if (classpath != null) {
                        searchList.addAll(0, getInternalURLs(url, classpath));
                    }
                }
            } catch (IOException e) {
            }
        }
        return jarH;
    } catch (IOException e) {
    }
    return null;
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest)

Example 60 with Manifest

use of java.util.jar.Manifest in project spring-boot by spring-projects.

the class RepackagerTests method springBootVersion.

@Test
public void springBootVersion() throws Exception {
    this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    repackager.repackage(NO_LIBRARIES);
    Manifest actualManifest = getManifest(file);
    assertThat(actualManifest.getMainAttributes()).containsKey(new Attributes.Name("Spring-Boot-Version"));
}
Also used : Attributes(java.util.jar.Attributes) Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.Test)

Aggregations

Manifest (java.util.jar.Manifest)1226 Attributes (java.util.jar.Attributes)392 File (java.io.File)391 IOException (java.io.IOException)336 JarFile (java.util.jar.JarFile)231 InputStream (java.io.InputStream)184 URL (java.net.URL)177 JarOutputStream (java.util.jar.JarOutputStream)145 FileOutputStream (java.io.FileOutputStream)131 Test (org.junit.Test)129 FileInputStream (java.io.FileInputStream)119 Jar (aQute.bnd.osgi.Jar)105 JarInputStream (java.util.jar.JarInputStream)104 Builder (aQute.bnd.osgi.Builder)99 ZipEntry (java.util.zip.ZipEntry)99 ByteArrayOutputStream (java.io.ByteArrayOutputStream)96 JarEntry (java.util.jar.JarEntry)96 ByteArrayInputStream (java.io.ByteArrayInputStream)93 ArrayList (java.util.ArrayList)83 Map (java.util.Map)83