Skip to content

Commit ad16426

Browse files
committed
Update classpath index to use jar name instead of full path
See gh-20564
1 parent 2ceec65 commit ad16426

File tree

7 files changed

+25
-57
lines changed

7 files changed

+25
-57
lines changed

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootJar.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ public void copy() {
125125
}
126126

127127
private File createClasspathIndex(List<String> dependencies) {
128-
String content = dependencies.stream().collect(Collectors.joining("\n", "", "\n"));
128+
String content = dependencies.stream().map((name) -> name.substring(name.lastIndexOf('/') + 1))
129+
.collect(Collectors.joining("\n", "", "\n"));
129130
File source = getProject().getResources().getText().fromString(content).asFile();
130131
File indexFile = new File(source.getParentFile(), "classpath.idx");
131132
source.renameTo(indexFile);

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,8 @@ void whenJarIsLayeredJarsInLibAreStored() throws IOException {
164164
@Test
165165
void whenJarIsLayeredClasspathIndexPointsToLayeredLibs() throws IOException {
166166
try (JarFile jarFile = new JarFile(createLayeredJar())) {
167-
assertThat(entryLines(jarFile, "BOOT-INF/classpath.idx")).containsExactly(
168-
"BOOT-INF/layers/dependencies/lib/first-library.jar",
169-
"BOOT-INF/layers/dependencies/lib/second-library.jar",
170-
"BOOT-INF/layers/snapshot-dependencies/lib/third-library-SNAPSHOT.jar");
167+
assertThat(entryLines(jarFile, "BOOT-INF/classpath.idx")).containsExactly("first-library.jar",
168+
"second-library.jar", "third-library-SNAPSHOT.jar");
171169
}
172170
}
173171

@@ -189,8 +187,8 @@ void classpathIndexPointsToBootInfLibs() throws IOException {
189187
try (JarFile jarFile = new JarFile(createPopulatedJar())) {
190188
assertThat(jarFile.getManifest().getMainAttributes().getValue("Spring-Boot-Classpath-Index"))
191189
.isEqualTo("BOOT-INF/classpath.idx");
192-
assertThat(entryLines(jarFile, "BOOT-INF/classpath.idx")).containsExactly("BOOT-INF/lib/first-library.jar",
193-
"BOOT-INF/lib/second-library.jar", "BOOT-INF/lib/third-library-SNAPSHOT.jar");
190+
assertThat(entryLines(jarFile, "BOOT-INF/classpath.idx")).containsExactly("first-library.jar",
191+
"second-library.jar", "third-library-SNAPSHOT.jar");
194192
}
195193
}
196194

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.jar.Attributes;
2929
import java.util.jar.JarFile;
3030
import java.util.jar.Manifest;
31+
import java.util.stream.Collectors;
3132

3233
import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
3334

@@ -492,7 +493,9 @@ private void write(AbstractJarWriter writer) throws IOException {
492493
}
493494
if (getLayout() instanceof RepackagingLayout) {
494495
String location = ((RepackagingLayout) getLayout()).getClasspathIndexFileLocation();
495-
writer.writeIndexFile(location, this.libraries.keySet());
496+
List<String> names = this.libraries.keySet().stream()
497+
.map((key) -> key.substring(key.lastIndexOf('/') + 1)).collect(Collectors.toList());
498+
writer.writeIndexFile(location, names);
496499
}
497500
}
498501

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ void index() throws Exception {
232232
assertThat(hasPackagedEntry("BOOT-INF/classpath.idx")).isTrue();
233233
String index = getPackagedEntryContent("BOOT-INF/classpath.idx");
234234
String[] libraries = index.split("\\r?\\n");
235-
assertThat(Arrays.asList(libraries)).contains("BOOT-INF/lib/" + libJarFile1.getName(),
236-
"BOOT-INF/lib/" + libJarFile2.getName(), "BOOT-INF/lib/" + libJarFile3.getName());
235+
assertThat(Arrays.asList(libraries)).contains(libJarFile1.getName(), libJarFile2.getName(),
236+
libJarFile3.getName());
237237
}
238238

239239
@Test
@@ -267,7 +267,8 @@ void layeredLayout() throws Exception {
267267
expectedJars.add("BOOT-INF/layers/0001/lib/" + libJarFile1.getName());
268268
expectedJars.add("BOOT-INF/layers/0002/lib/" + libJarFile2.getName());
269269
expectedJars.add("BOOT-INF/layers/0003/lib/" + libJarFile3.getName());
270-
assertThat(Arrays.asList(classpathIndex.split("\\n"))).containsExactly(expectedJars.toArray(new String[0]));
270+
assertThat(Arrays.asList(classpathIndex.split("\\n"))).containsExactly(libJarFile1.getName(),
271+
libJarFile2.getName(), libJarFile3.getName());
271272
assertThat(hasPackagedEntry("BOOT-INF/layers.idx")).isTrue();
272273
String layersIndex = getPackagedEntryContent("BOOT-INF/layers.idx");
273274
List<String> expectedLayers = new ArrayList<>();
@@ -288,8 +289,7 @@ void layeredLayoutAddJarModeJar() throws Exception {
288289
execute(packager, Libraries.NONE);
289290
assertThat(hasPackagedEntry("BOOT-INF/classpath.idx")).isTrue();
290291
String classpathIndex = getPackagedEntryContent("BOOT-INF/classpath.idx");
291-
assertThat(Arrays.asList(classpathIndex.split("\\n")))
292-
.containsExactly("BOOT-INF/layers/default/lib/spring-boot-jarmode-layertools.jar");
292+
assertThat(Arrays.asList(classpathIndex.split("\\n"))).containsExactly("spring-boot-jarmode-layertools.jar");
293293
assertThat(hasPackagedEntry("BOOT-INF/layers.idx")).isTrue();
294294
String layersIndex = getPackagedEntryContent("BOOT-INF/layers.idx");
295295
assertThat(Arrays.asList(layersIndex.split("\\n"))).containsExactly("default");

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import java.util.ArrayList;
3030
import java.util.Collections;
3131
import java.util.List;
32-
import java.util.Objects;
33-
import java.util.Set;
3432
import java.util.stream.Collectors;
3533

3634
/**
@@ -45,33 +43,15 @@ final class ClassPathIndexFile {
4543

4644
private final List<String> lines;
4745

48-
private final Set<String> folders;
49-
5046
private ClassPathIndexFile(File root, List<String> lines) {
5147
this.root = root;
5248
this.lines = lines;
53-
this.folders = this.lines.stream().map(this::getFolder).filter(Objects::nonNull).collect(Collectors.toSet());
54-
}
55-
56-
private String getFolder(String name) {
57-
int lastSlash = name.lastIndexOf('/');
58-
return (lastSlash != -1) ? name.substring(0, lastSlash) : null;
5949
}
6050

6151
int size() {
6252
return this.lines.size();
6353
}
6454

65-
boolean containsFolder(String name) {
66-
if (name == null || name.isEmpty()) {
67-
return false;
68-
}
69-
if (name.endsWith("/")) {
70-
return containsFolder(name.substring(0, name.length() - 1));
71-
}
72-
return this.folders.contains(name);
73-
}
74-
7555
boolean containsEntry(String name) {
7656
if (name == null || name.isEmpty()) {
7757
return false;

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

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,30 +73,16 @@ void sizeReturnsNumberOfLines() throws Exception {
7373
assertThat(indexFile.size()).isEqualTo(5);
7474
}
7575

76-
@Test
77-
void containsFolderWhenFolderIsPresentReturnsTrue() throws Exception {
78-
ClassPathIndexFile indexFile = copyAndLoadTestIndexFile();
79-
assertThat(indexFile.containsFolder("BOOT-INF/layers/one/lib")).isTrue();
80-
assertThat(indexFile.containsFolder("BOOT-INF/layers/one/lib/")).isTrue();
81-
assertThat(indexFile.containsFolder("BOOT-INF/layers/two/lib")).isTrue();
82-
}
83-
84-
@Test
85-
void containsFolderWhenFolderIsMissingReturnsFalse() throws Exception {
86-
ClassPathIndexFile indexFile = copyAndLoadTestIndexFile();
87-
assertThat(indexFile.containsFolder("BOOT-INF/layers/nope/lib/")).isFalse();
88-
}
89-
9076
@Test
9177
void getUrlsReturnsUrls() throws Exception {
9278
ClassPathIndexFile indexFile = copyAndLoadTestIndexFile();
9379
List<URL> urls = indexFile.getUrls();
9480
List<File> expected = new ArrayList<>();
95-
expected.add(new File(this.temp, "BOOT-INF/layers/one/lib/a.jar"));
96-
expected.add(new File(this.temp, "BOOT-INF/layers/one/lib/b.jar"));
97-
expected.add(new File(this.temp, "BOOT-INF/layers/one/lib/c.jar"));
98-
expected.add(new File(this.temp, "BOOT-INF/layers/two/lib/d.jar"));
99-
expected.add(new File(this.temp, "BOOT-INF/layers/two/lib/e.jar"));
81+
expected.add(new File(this.temp, "a.jar"));
82+
expected.add(new File(this.temp, "b.jar"));
83+
expected.add(new File(this.temp, "c.jar"));
84+
expected.add(new File(this.temp, "d.jar"));
85+
expected.add(new File(this.temp, "e.jar"));
10086
assertThat(urls).containsExactly(expected.stream().map(this::toUrl).toArray(URL[]::new));
10187
}
10288

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
BOOT-INF/layers/one/lib/a.jar
2-
BOOT-INF/layers/one/lib/b.jar
3-
BOOT-INF/layers/one/lib/c.jar
4-
BOOT-INF/layers/two/lib/d.jar
5-
BOOT-INF/layers/two/lib/e.jar
1+
a.jar
2+
b.jar
3+
c.jar
4+
d.jar
5+
e.jar

0 commit comments

Comments
 (0)