Skip to content

Commit 7d8f8d4

Browse files
committed
Fix packager code to generate layer index file
Update `Packager` to create the layer index file when repackaging. Closes gh-19767
1 parent bfd2ca7 commit 7d8f8d4

File tree

2 files changed

+27
-9
lines changed
  • spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src

2 files changed

+27
-9
lines changed

spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Packager.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ protected final void write(JarFile sourceJar, Libraries libraries, AbstractJarWr
159159
writeLoaderClasses(writer);
160160
writer.writeEntries(sourceJar, getEntityTransformer(), writeableLibraries);
161161
writeableLibraries.write(writer);
162+
writeLayerIndex(writer);
162163
}
163164

164165
private void writeLoaderClasses(AbstractJarWriter writer) throws IOException {
@@ -171,6 +172,17 @@ else if (layout.isExecutable()) {
171172
}
172173
}
173174

175+
private void writeLayerIndex(AbstractJarWriter writer) throws IOException {
176+
if (this.layers != null && getLayout() instanceof LayeredLayout) {
177+
String location = ((LayeredLayout) this.layout).getLayersIndexFileLocation();
178+
if (StringUtils.hasLength(location)) {
179+
List<String> layerNames = new ArrayList<>();
180+
this.layers.forEach((layer) -> layerNames.add(layer.toString()));
181+
writer.writeIndexFile(location, layerNames);
182+
}
183+
}
184+
}
185+
174186
private EntryTransformer getEntityTransformer() {
175187
if (getLayout() instanceof RepackagingLayout) {
176188
return new RepackagingEntryTransformer((RepackagingLayout) getLayout(), this.layers);
@@ -304,8 +316,7 @@ else if (layout instanceof RepackagingLayout) {
304316
}
305317

306318
private void addBootBootAttributesForLayeredLayout(Attributes attributes, LayeredLayout layout) {
307-
String layersIndexFileLocation = layout.getLayersIndexFileLocation();
308-
putIfHasLength(attributes, BOOT_LAYERS_INDEX_ATTRIBUTE, layersIndexFileLocation);
319+
putIfHasLength(attributes, BOOT_LAYERS_INDEX_ATTRIBUTE, layout.getLayersIndexFileLocation());
309320
putIfHasLength(attributes, BOOT_CLASSPATH_INDEX_ATTRIBUTE, layout.getClasspathIndexFileLocation());
310321
}
311322

spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/AbstractPackagerTests.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,20 @@ void layeredLayout() throws Exception {
261261
callback.library(new Library(libJarFile3, LibraryScope.COMPILE));
262262
});
263263
assertThat(hasPackagedEntry("BOOT-INF/classpath.idx")).isTrue();
264-
String index = getPackagedEntryContent("BOOT-INF/classpath.idx");
265-
String[] libraries = index.split("\\n");
266-
List<String> expected = new ArrayList<>();
267-
expected.add("BOOT-INF/layers/0001/lib/" + libJarFile1.getName());
268-
expected.add("BOOT-INF/layers/0002/lib/" + libJarFile2.getName());
269-
expected.add("BOOT-INF/layers/0003/lib/" + libJarFile3.getName());
270-
assertThat(Arrays.asList(libraries)).containsExactly(expected.toArray(new String[0]));
264+
String classpathIndex = getPackagedEntryContent("BOOT-INF/classpath.idx");
265+
List<String> expectedJars = new ArrayList<>();
266+
expectedJars.add("BOOT-INF/layers/0001/lib/" + libJarFile1.getName());
267+
expectedJars.add("BOOT-INF/layers/0002/lib/" + libJarFile2.getName());
268+
expectedJars.add("BOOT-INF/layers/0003/lib/" + libJarFile3.getName());
269+
assertThat(Arrays.asList(classpathIndex.split("\\n"))).containsExactly(expectedJars.toArray(new String[0]));
270+
assertThat(hasPackagedEntry("BOOT-INF/layers.idx")).isTrue();
271+
String layersIndex = getPackagedEntryContent("BOOT-INF/layers.idx");
272+
List<String> expectedLayers = new ArrayList<>();
273+
expectedLayers.add("default");
274+
expectedLayers.add("0001");
275+
expectedLayers.add("0002");
276+
expectedLayers.add("0003");
277+
assertThat(Arrays.asList(layersIndex.split("\\n"))).containsExactly(expectedLayers.toArray(new String[0]));
271278
}
272279

273280
@Test

0 commit comments

Comments
 (0)