Search in sources :

Example 81 with JarFile

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

the class RepackagerTests method unpackLibrariesTakePrecedenceOverExistingSourceEntries.

@Test
public void unpackLibrariesTakePrecedenceOverExistingSourceEntries() throws Exception {
    TestJarFile nested = new TestJarFile(this.temporaryFolder);
    nested.addClass("a/b/C.class", ClassWithoutMainMethod.class);
    final File nestedFile = nested.getFile();
    this.testJarFile.addFile("BOOT-INF/lib/" + nestedFile.getName(), nested.getFile());
    this.testJarFile.addClass("A.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    repackager.repackage(new Libraries() {

        @Override
        public void doWithLibraries(LibraryCallback callback) throws IOException {
            callback.library(new Library(nestedFile, LibraryScope.COMPILE, true));
        }
    });
    JarFile jarFile = new JarFile(file);
    try {
        assertThat(jarFile.getEntry("BOOT-INF/lib/" + nestedFile.getName()).getComment()).startsWith("UNPACK:");
    } finally {
        jarFile.close();
    }
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.Test)

Example 82 with JarFile

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

the class BootRunApplicationLauncher method explodeArchive.

private void explodeArchive(File archive) throws IOException {
    FileSystemUtils.deleteRecursively(this.exploded);
    JarFile jarFile = new JarFile(archive);
    Enumeration<JarEntry> entries = jarFile.entries();
    while (entries.hasMoreElements()) {
        JarEntry jarEntry = entries.nextElement();
        File extracted = new File(this.exploded, jarEntry.getName());
        if (jarEntry.isDirectory()) {
            extracted.mkdirs();
        } else {
            FileOutputStream extractedOutputStream = new FileOutputStream(extracted);
            StreamUtils.copy(jarFile.getInputStream(jarEntry), extractedOutputStream);
            extractedOutputStream.close();
        }
    }
    jarFile.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 83 with JarFile

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

the class ClassifierTests method checkFilesExist.

private void checkFilesExist(String name) throws Exception {
    JarFile jar = new JarFile("target/" + name + "/build/libs/" + name + ".jar");
    assertThat(jar.getManifest()).isNotNull();
    jar.close();
    jar = new JarFile("target/" + name + "/build/libs/" + name + "-exec.jar");
    assertThat(jar.getManifest()).isNotNull();
    jar.close();
}
Also used : JarFile(java.util.jar.JarFile)

Example 84 with JarFile

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

the class MultiProjectRepackagingTests method repackageWithRuntimeProjectDependency.

@Test
public void repackageWithRuntimeProjectDependency() throws Exception {
    ProjectConnection project = new ProjectCreator().createProject("multi-project-runtime-project-dependency");
    project.newBuild().forTasks("clean", "build").withArguments("-PbootVersion=" + BOOT_VERSION).run();
    File buildLibs = new File("target/multi-project-runtime-project-dependency/projectA/build/libs");
    JarFile jarFile = new JarFile(new File(buildLibs, "projectA.jar"));
    assertThat(jarFile.getEntry("BOOT-INF/lib/projectB.jar")).isNotNull();
    jarFile.close();
}
Also used : ProjectConnection(org.gradle.tooling.ProjectConnection) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.Test)

Example 85 with JarFile

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

the class Repackager method repackage.

/**
	 * Repackage to the given destination so that it can be launched using '
	 * {@literal java -jar}'.
	 * @param destination the destination file (may be the same as the source)
	 * @param libraries the libraries required to run the archive
	 * @param launchScript an optional launch script prepended to the front of the jar
	 * @throws IOException if the file cannot be repackaged
	 * @since 1.3.0
	 */
public void repackage(File destination, Libraries libraries, LaunchScript launchScript) throws IOException {
    if (destination == null || destination.isDirectory()) {
        throw new IllegalArgumentException("Invalid destination");
    }
    if (libraries == null) {
        throw new IllegalArgumentException("Libraries must not be null");
    }
    if (this.layout == null) {
        this.layout = getLayoutFactory().getLayout(this.source);
    }
    if (alreadyRepackaged()) {
        return;
    }
    destination = destination.getAbsoluteFile();
    File workingSource = this.source;
    if (this.source.equals(destination)) {
        workingSource = getBackupFile();
        workingSource.delete();
        renameFile(this.source, workingSource);
    }
    destination.delete();
    try {
        JarFile jarFileSource = new JarFile(workingSource);
        try {
            repackage(jarFileSource, destination, libraries, launchScript);
        } finally {
            jarFileSource.close();
        }
    } finally {
        if (!this.backupSource && !this.source.equals(workingSource)) {
            deleteFile(workingSource);
        }
    }
}
Also used : JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

JarFile (java.util.jar.JarFile)509 File (java.io.File)289 JarEntry (java.util.jar.JarEntry)230 IOException (java.io.IOException)212 URL (java.net.URL)92 ZipEntry (java.util.zip.ZipEntry)92 InputStream (java.io.InputStream)90 Manifest (java.util.jar.Manifest)81 ZipFile (java.util.zip.ZipFile)74 FileOutputStream (java.io.FileOutputStream)71 Test (org.junit.Test)66 ArrayList (java.util.ArrayList)54 JarURLConnection (java.net.JarURLConnection)48 Attributes (java.util.jar.Attributes)43 JarOutputStream (java.util.jar.JarOutputStream)41 MalformedURLException (java.net.MalformedURLException)31 FileInputStream (java.io.FileInputStream)30 Support_PlatformFile (tests.support.Support_PlatformFile)26 Enumeration (java.util.Enumeration)24 HashSet (java.util.HashSet)20