diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java index dd4487a30e..651be8294a 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java @@ -423,13 +423,14 @@ public class Repackager { libraries.doWithLibraries((library) -> { if (isZip(library.getFile())) { String libraryDestination = Repackager.this.layout - .getLibraryDestination(library.getName(), library.getScope()) - + library.getName(); - Library existing = this.libraryEntryNames - .putIfAbsent(libraryDestination, library); - if (existing != null) { - throw new IllegalStateException( - "Duplicate library " + library.getName()); + .getLibraryDestination(library.getName(), library.getScope()); + if (libraryDestination != null) { + Library existing = this.libraryEntryNames.putIfAbsent( + libraryDestination + library.getName(), library); + if (existing != null) { + throw new IllegalStateException( + "Duplicate library " + library.getName()); + } } } }); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java index e157f00d3d..52ad367edd 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java @@ -621,6 +621,23 @@ public class RepackagerTests { assertThat(unpackLibrary.getComment()).startsWith("UNPACK:"); } + @Test + public void layoutCanOmitLibraries() throws IOException { + TestJarFile libJar = new TestJarFile(this.temporaryFolder); + libJar.addClass("a/b/C.class", ClassWithoutMainMethod.class); + final File libJarFile = libJar.getFile(); + this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class); + File file = this.testJarFile.getFile(); + Repackager repackager = new Repackager(file); + Layout layout = mock(Layout.class); + final LibraryScope scope = mock(LibraryScope.class); + repackager.setLayout(layout); + repackager.repackage( + (callback) -> callback.library(new Library(libJarFile, scope))); + assertThat(getEntryNames(file)).containsExactly("META-INF/", + "META-INF/MANIFEST.MF", "a/", "a/b/", "a/b/C.class"); + } + private File createLibrary() throws IOException { TestJarFile library = new TestJarFile(this.temporaryFolder); library.addClass("com/example/library/Library.class",