Omit any file that is not a zip when repackaging
When repackaging an archive, the files in the resulting lib directory must be zip files. If they're not zip files, the resulting archive may fail to run (#324). The previous approach was to consider an artifact's type when deciding whether or not it should be packaged. The type is a string and, while there are a number of well-known values, it can essentially be anything. This caused a problem with an artifact incorrectly being identified as being unsuitable for inclusion (#489). This commit changes the approach. Rather than looking at an artifact's type, it looks at the first four bytes of the archive's file. Only if these header bytes matche that of a zip file is the artifact included. This is a better match for the requirement that all files in lib be zip files. Fixes #489
This commit is contained in:
@@ -17,8 +17,10 @@
|
||||
package org.springframework.boot.loader.tools;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.jar.Manifest;
|
||||
@@ -33,6 +35,8 @@ import org.springframework.boot.loader.tools.MainClassFinder.ClassNameCallback;
|
||||
*/
|
||||
public class Repackager {
|
||||
|
||||
private static final byte[] ZIP_FILE_HEADER = new byte[] { 'P', 'K', 3, 4 };
|
||||
|
||||
private static final String MAIN_CLASS_ATTRIBUTE = "Main-Class";
|
||||
|
||||
private static final String START_CLASS_ATTRIBUTE = "Start-Class";
|
||||
@@ -142,10 +146,37 @@ public class Repackager {
|
||||
|
||||
@Override
|
||||
public void library(File file, LibraryScope scope) throws IOException {
|
||||
String destination = Repackager.this.layout.getLibraryDestination(
|
||||
file.getName(), scope);
|
||||
if (destination != null) {
|
||||
writer.writeNestedLibrary(destination, file);
|
||||
|
||||
if (isZipFile(file)) {
|
||||
String destination = Repackager.this.layout
|
||||
.getLibraryDestination(file.getName(), scope);
|
||||
if (destination != null) {
|
||||
writer.writeNestedLibrary(destination, file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isZipFile(File file) {
|
||||
byte[] buffer = new byte[4];
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
fis = new FileInputStream(file);
|
||||
int read = fis.read(buffer);
|
||||
|
||||
return (read == 4 && Arrays.equals(buffer, ZIP_FILE_HEADER));
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
return false;
|
||||
}
|
||||
finally {
|
||||
if (fis != null) {
|
||||
try {
|
||||
fis.close();
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
// Close quietly
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.junit.rules.ExpectedException;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.springframework.boot.loader.tools.sample.ClassWithMainMethod;
|
||||
import org.springframework.boot.loader.tools.sample.ClassWithoutMainMethod;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
@@ -256,6 +257,8 @@ public class RepackagerTests {
|
||||
TestJarFile libJar = new TestJarFile(this.temporaryFolder);
|
||||
libJar.addClass("a/b/C.class", ClassWithoutMainMethod.class);
|
||||
final File libJarFile = libJar.getFile();
|
||||
final File libNonJarFile = this.temporaryFolder.newFile();
|
||||
FileCopyUtils.copy(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, libNonJarFile);
|
||||
this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
|
||||
File file = this.testJarFile.getFile();
|
||||
Repackager repackager = new Repackager(file);
|
||||
@@ -263,9 +266,11 @@ public class RepackagerTests {
|
||||
@Override
|
||||
public void doWithLibraries(LibraryCallback callback) throws IOException {
|
||||
callback.library(libJarFile, LibraryScope.COMPILE);
|
||||
callback.library(libNonJarFile, LibraryScope.COMPILE);
|
||||
}
|
||||
});
|
||||
assertThat(hasEntry(file, "lib/" + libJarFile.getName()), equalTo(true));
|
||||
assertThat(hasEntry(file, "lib/" + libNonJarFile.getName()), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user