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();
}
}
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();
}
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();
}
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();
}
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);
}
}
}
Aggregations