From 467c092fdc83b16e27cefc9b9378e32ab4b58cb5 Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Fri, 3 Dec 2021 11:41:28 -0600 Subject: [PATCH] Remove classpath index manifest attribute from repackaged war files Fixes gh-28895 --- .../springframework/boot/loader/tools/Layouts.java | 6 +----- .../springframework/boot/loader/tools/Packager.java | 12 ++++++------ .../boot/maven/AbstractArchiveIntegrationTests.java | 6 ++++++ .../boot/maven/JarIntegrationTests.java | 11 +++++++++++ .../boot/maven/WarIntegrationTests.java | 11 +++++++++++ 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Layouts.java b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Layouts.java index 11d4332b1927..87e61c60a979 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Layouts.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Layouts.java @@ -29,6 +29,7 @@ * @author Dave Syer * @author Andy Wilkinson * @author Madhura Bhave + * @author Scott Frederick * @since 1.0.0 */ public final class Layouts { @@ -160,11 +161,6 @@ public String getClassesLocation() { return "WEB-INF/classes/"; } - @Override - public String getClasspathIndexFileLocation() { - return "WEB-INF/classpath.idx"; - } - @Override public String getLayersIndexFileLocation() { return "WEB-INF/layers.idx"; diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Packager.java b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Packager.java index 7f5edd7476fc..5a1f5b68aece 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Packager.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Packager.java @@ -510,15 +510,15 @@ void write(AbstractJarWriter writer) throws IOException { writtenPaths.add(path); } } - if (getLayout() instanceof RepackagingLayout) { - writeClasspathIndex(writtenPaths, (RepackagingLayout) getLayout(), writer); - } + writeClasspathIndexIfNecessary(writtenPaths, getLayout(), writer); } - private void writeClasspathIndex(List paths, RepackagingLayout layout, AbstractJarWriter writer) + private void writeClasspathIndexIfNecessary(List paths, Layout layout, AbstractJarWriter writer) throws IOException { - List names = paths.stream().map((path) -> "- \"" + path + "\"").collect(Collectors.toList()); - writer.writeIndexFile(layout.getClasspathIndexFileLocation(), names); + if (layout.getClasspathIndexFileLocation() != null) { + List names = paths.stream().map((path) -> "- \"" + path + "\"").collect(Collectors.toList()); + writer.writeIndexFile(layout.getClasspathIndexFileLocation(), names); + } } private class PackagedLibrariesUnpackHandler implements UnpackHandler { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/AbstractArchiveIntegrationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/AbstractArchiveIntegrationTests.java index 05cc9a7764d1..9ece3542565e 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/AbstractArchiveIntegrationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/AbstractArchiveIntegrationTests.java @@ -43,6 +43,7 @@ * Base class for archive (jar or war) related Maven plugin integration tests. * * @author Andy Wilkinson + * @author Scott Frederick */ abstract class AbstractArchiveIntegrationTests { @@ -201,6 +202,11 @@ ManifestAssert hasAttribute(String name, String value) { return this; } + ManifestAssert doesNotHaveAttribute(String name) { + assertThat(this.actual.getMainAttributes().getValue(name)).isNull(); + return this; + } + } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/JarIntegrationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/JarIntegrationTests.java index c85ecc9c8880..e08548d388da 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/JarIntegrationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/JarIntegrationTests.java @@ -38,6 +38,7 @@ * * @author Andy Wilkinson * @author Madhura Bhave + * @author Scott Frederick */ @ExtendWith(MavenBuildExtension.class) class JarIntegrationTests extends AbstractArchiveIntegrationTests { @@ -363,6 +364,16 @@ void whenJarIsRepackagedWithTheCustomLayers(MavenBuild mavenBuild) { }); } + @TestTemplate + void repackagedJarContainsClasspathIndex(MavenBuild mavenBuild) { + mavenBuild.project("jar").execute((project) -> { + File repackaged = new File(project, "target/jar-0.0.1.BUILD-SNAPSHOT.jar"); + assertThat(jar(repackaged)).manifest( + (manifest) -> manifest.hasAttribute("Spring-Boot-Classpath-Index", "BOOT-INF/classpath.idx")); + assertThat(jar(repackaged)).hasEntryWithName("BOOT-INF/classpath.idx"); + }); + } + @TestTemplate void whenJarIsRepackagedWithOutputTimestampConfiguredThenJarIsReproducible(MavenBuild mavenBuild) throws InterruptedException { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/WarIntegrationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/WarIntegrationTests.java index 6615cd1567c8..653fb321bbba 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/WarIntegrationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/WarIntegrationTests.java @@ -38,6 +38,7 @@ * Integration tests for the Maven plugin's war support. * * @author Andy Wilkinson + * @author Scott Frederick */ @ExtendWith(MavenBuildExtension.class) class WarIntegrationTests extends AbstractArchiveIntegrationTests { @@ -190,6 +191,16 @@ void whenWarIsRepackagedWithTheCustomLayers(MavenBuild mavenBuild) { }); } + @TestTemplate + void repackagedWarDoesNotContainClasspathIndex(MavenBuild mavenBuild) { + mavenBuild.project("war").execute((project) -> { + File repackaged = new File(project, "target/war-0.0.1.BUILD-SNAPSHOT.war"); + assertThat(jar(repackaged)) + .manifest((manifest) -> manifest.doesNotHaveAttribute("Spring-Boot-Classpath-Index")); + assertThat(jar(repackaged)).doesNotHaveEntryWithName("BOOT-INF/classpath.idx"); + }); + } + @TestTemplate void whenEntryIsExcludedItShouldNotBePresentInTheRepackagedWar(MavenBuild mavenBuild) { mavenBuild.project("war-exclude-entry").execute((project) -> {