From 899b851c6f249f8b5b02bc0f8fc1632e23233df0 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 10 Aug 2016 13:08:23 +0100 Subject: [PATCH] Remove META-INF/INDEX.LIST when repackaging a jar file META-INF/INDEX.LIST files are pointless in an executable jar and moving application classes from the root of the jar to BOOT-INF/classes breaks the index, resulting in an InvalidJarIndexException being thrown. This commit updates the Repackager to automatically remove a META-INF/INDEX.LIST file from a jar file that is being repackaged. Closes gh-6601 --- .../boot/loader/tools/JarWriter.java | 5 ++++- .../boot/loader/tools/Repackager.java | 3 +++ .../boot/loader/tools/RepackagerTests.java | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java index a0eb52b018..d5a05dd094 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java @@ -141,7 +141,10 @@ public class JarWriter { jarFile.getInputStream(entry)); } EntryWriter entryWriter = new InputStreamEntryWriter(inputStream, true); - writeEntry(entryTransformer.transform(entry), entryWriter); + JarEntry transformedEntry = entryTransformer.transform(entry); + if (transformedEntry != null) { + writeEntry(transformedEntry, entryWriter); + } } finally { inputStream.close(); diff --git a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java index 35a373221d..c25129844b 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java @@ -337,6 +337,9 @@ public class Repackager { @Override public JarEntry transform(JarEntry entry) { + if (entry.getName().equals("META-INF/INDEX.LIST")) { + return null; + } if (entry.getName().startsWith("META-INF/") || entry.getName().startsWith("BOOT-INF/")) { return entry; diff --git a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java index edc4656fbf..4c3ee88ea6 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java @@ -518,6 +518,24 @@ public class RepackagerTests { } } + @Test + public void metaInfIndexListIsRemovedFromRepackagedJar() throws Exception { + this.testJarFile.addClass("A.class", ClassWithMainMethod.class); + this.testJarFile.addFile("META-INF/INDEX.LIST", + this.temporaryFolder.newFile("INDEX.LIST")); + File source = this.testJarFile.getFile(); + File dest = this.temporaryFolder.newFile("dest.jar"); + Repackager repackager = new Repackager(source); + repackager.repackage(dest, NO_LIBRARIES); + JarFile jarFile = new JarFile(dest); + try { + assertThat(jarFile.getEntry("META-INF/INDEX.LIST")).isNull(); + } + finally { + jarFile.close(); + } + } + private boolean hasLauncherClasses(File file) throws IOException { return hasEntry(file, "org/springframework/boot/") && hasEntry(file, "org/springframework/boot/loader/JarLauncher.class");