Fix packager code to generate layer index file
Update `Packager` to create the layer index file when repackaging. Closes gh-19767
This commit is contained in:
@@ -159,6 +159,7 @@ public abstract class Packager {
|
||||
writeLoaderClasses(writer);
|
||||
writer.writeEntries(sourceJar, getEntityTransformer(), writeableLibraries);
|
||||
writeableLibraries.write(writer);
|
||||
writeLayerIndex(writer);
|
||||
}
|
||||
|
||||
private void writeLoaderClasses(AbstractJarWriter writer) throws IOException {
|
||||
@@ -171,6 +172,17 @@ public abstract class Packager {
|
||||
}
|
||||
}
|
||||
|
||||
private void writeLayerIndex(AbstractJarWriter writer) throws IOException {
|
||||
if (this.layers != null && getLayout() instanceof LayeredLayout) {
|
||||
String location = ((LayeredLayout) this.layout).getLayersIndexFileLocation();
|
||||
if (StringUtils.hasLength(location)) {
|
||||
List<String> layerNames = new ArrayList<>();
|
||||
this.layers.forEach((layer) -> layerNames.add(layer.toString()));
|
||||
writer.writeIndexFile(location, layerNames);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private EntryTransformer getEntityTransformer() {
|
||||
if (getLayout() instanceof RepackagingLayout) {
|
||||
return new RepackagingEntryTransformer((RepackagingLayout) getLayout(), this.layers);
|
||||
@@ -304,8 +316,7 @@ public abstract class Packager {
|
||||
}
|
||||
|
||||
private void addBootBootAttributesForLayeredLayout(Attributes attributes, LayeredLayout layout) {
|
||||
String layersIndexFileLocation = layout.getLayersIndexFileLocation();
|
||||
putIfHasLength(attributes, BOOT_LAYERS_INDEX_ATTRIBUTE, layersIndexFileLocation);
|
||||
putIfHasLength(attributes, BOOT_LAYERS_INDEX_ATTRIBUTE, layout.getLayersIndexFileLocation());
|
||||
putIfHasLength(attributes, BOOT_CLASSPATH_INDEX_ATTRIBUTE, layout.getClasspathIndexFileLocation());
|
||||
}
|
||||
|
||||
|
||||
@@ -261,13 +261,20 @@ abstract class AbstractPackagerTests<P extends Packager> {
|
||||
callback.library(new Library(libJarFile3, LibraryScope.COMPILE));
|
||||
});
|
||||
assertThat(hasPackagedEntry("BOOT-INF/classpath.idx")).isTrue();
|
||||
String index = getPackagedEntryContent("BOOT-INF/classpath.idx");
|
||||
String[] libraries = index.split("\\n");
|
||||
List<String> expected = new ArrayList<>();
|
||||
expected.add("BOOT-INF/layers/0001/lib/" + libJarFile1.getName());
|
||||
expected.add("BOOT-INF/layers/0002/lib/" + libJarFile2.getName());
|
||||
expected.add("BOOT-INF/layers/0003/lib/" + libJarFile3.getName());
|
||||
assertThat(Arrays.asList(libraries)).containsExactly(expected.toArray(new String[0]));
|
||||
String classpathIndex = getPackagedEntryContent("BOOT-INF/classpath.idx");
|
||||
List<String> expectedJars = new ArrayList<>();
|
||||
expectedJars.add("BOOT-INF/layers/0001/lib/" + libJarFile1.getName());
|
||||
expectedJars.add("BOOT-INF/layers/0002/lib/" + libJarFile2.getName());
|
||||
expectedJars.add("BOOT-INF/layers/0003/lib/" + libJarFile3.getName());
|
||||
assertThat(Arrays.asList(classpathIndex.split("\\n"))).containsExactly(expectedJars.toArray(new String[0]));
|
||||
assertThat(hasPackagedEntry("BOOT-INF/layers.idx")).isTrue();
|
||||
String layersIndex = getPackagedEntryContent("BOOT-INF/layers.idx");
|
||||
List<String> expectedLayers = new ArrayList<>();
|
||||
expectedLayers.add("default");
|
||||
expectedLayers.add("0001");
|
||||
expectedLayers.add("0002");
|
||||
expectedLayers.add("0003");
|
||||
assertThat(Arrays.asList(layersIndex.split("\\n"))).containsExactly(expectedLayers.toArray(new String[0]));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user