Update classpath index to use jar name instead of full path
See gh-20564
This commit is contained in:
parent
2ceec65b5d
commit
ad164269e9
|
|
@ -125,7 +125,8 @@ public class BootJar extends Jar implements BootArchive {
|
|||
}
|
||||
|
||||
private File createClasspathIndex(List<String> dependencies) {
|
||||
String content = dependencies.stream().collect(Collectors.joining("\n", "", "\n"));
|
||||
String content = dependencies.stream().map((name) -> name.substring(name.lastIndexOf('/') + 1))
|
||||
.collect(Collectors.joining("\n", "", "\n"));
|
||||
File source = getProject().getResources().getText().fromString(content).asFile();
|
||||
File indexFile = new File(source.getParentFile(), "classpath.idx");
|
||||
source.renameTo(indexFile);
|
||||
|
|
|
|||
|
|
@ -164,10 +164,8 @@ class BootJarTests extends AbstractBootArchiveTests<BootJar> {
|
|||
@Test
|
||||
void whenJarIsLayeredClasspathIndexPointsToLayeredLibs() throws IOException {
|
||||
try (JarFile jarFile = new JarFile(createLayeredJar())) {
|
||||
assertThat(entryLines(jarFile, "BOOT-INF/classpath.idx")).containsExactly(
|
||||
"BOOT-INF/layers/dependencies/lib/first-library.jar",
|
||||
"BOOT-INF/layers/dependencies/lib/second-library.jar",
|
||||
"BOOT-INF/layers/snapshot-dependencies/lib/third-library-SNAPSHOT.jar");
|
||||
assertThat(entryLines(jarFile, "BOOT-INF/classpath.idx")).containsExactly("first-library.jar",
|
||||
"second-library.jar", "third-library-SNAPSHOT.jar");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -189,8 +187,8 @@ class BootJarTests extends AbstractBootArchiveTests<BootJar> {
|
|||
try (JarFile jarFile = new JarFile(createPopulatedJar())) {
|
||||
assertThat(jarFile.getManifest().getMainAttributes().getValue("Spring-Boot-Classpath-Index"))
|
||||
.isEqualTo("BOOT-INF/classpath.idx");
|
||||
assertThat(entryLines(jarFile, "BOOT-INF/classpath.idx")).containsExactly("BOOT-INF/lib/first-library.jar",
|
||||
"BOOT-INF/lib/second-library.jar", "BOOT-INF/lib/third-library-SNAPSHOT.jar");
|
||||
assertThat(entryLines(jarFile, "BOOT-INF/classpath.idx")).containsExactly("first-library.jar",
|
||||
"second-library.jar", "third-library-SNAPSHOT.jar");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ import java.util.concurrent.TimeUnit;
|
|||
import java.util.jar.Attributes;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.jar.Manifest;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
|
||||
|
||||
|
|
@ -492,7 +493,9 @@ public abstract class Packager {
|
|||
}
|
||||
if (getLayout() instanceof RepackagingLayout) {
|
||||
String location = ((RepackagingLayout) getLayout()).getClasspathIndexFileLocation();
|
||||
writer.writeIndexFile(location, this.libraries.keySet());
|
||||
List<String> names = this.libraries.keySet().stream()
|
||||
.map((key) -> key.substring(key.lastIndexOf('/') + 1)).collect(Collectors.toList());
|
||||
writer.writeIndexFile(location, names);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -232,8 +232,8 @@ abstract class AbstractPackagerTests<P extends Packager> {
|
|||
assertThat(hasPackagedEntry("BOOT-INF/classpath.idx")).isTrue();
|
||||
String index = getPackagedEntryContent("BOOT-INF/classpath.idx");
|
||||
String[] libraries = index.split("\\r?\\n");
|
||||
assertThat(Arrays.asList(libraries)).contains("BOOT-INF/lib/" + libJarFile1.getName(),
|
||||
"BOOT-INF/lib/" + libJarFile2.getName(), "BOOT-INF/lib/" + libJarFile3.getName());
|
||||
assertThat(Arrays.asList(libraries)).contains(libJarFile1.getName(), libJarFile2.getName(),
|
||||
libJarFile3.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -267,7 +267,8 @@ abstract class AbstractPackagerTests<P extends Packager> {
|
|||
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(Arrays.asList(classpathIndex.split("\\n"))).containsExactly(libJarFile1.getName(),
|
||||
libJarFile2.getName(), libJarFile3.getName());
|
||||
assertThat(hasPackagedEntry("BOOT-INF/layers.idx")).isTrue();
|
||||
String layersIndex = getPackagedEntryContent("BOOT-INF/layers.idx");
|
||||
List<String> expectedLayers = new ArrayList<>();
|
||||
|
|
@ -288,8 +289,7 @@ abstract class AbstractPackagerTests<P extends Packager> {
|
|||
execute(packager, Libraries.NONE);
|
||||
assertThat(hasPackagedEntry("BOOT-INF/classpath.idx")).isTrue();
|
||||
String classpathIndex = getPackagedEntryContent("BOOT-INF/classpath.idx");
|
||||
assertThat(Arrays.asList(classpathIndex.split("\\n")))
|
||||
.containsExactly("BOOT-INF/layers/default/lib/spring-boot-jarmode-layertools.jar");
|
||||
assertThat(Arrays.asList(classpathIndex.split("\\n"))).containsExactly("spring-boot-jarmode-layertools.jar");
|
||||
assertThat(hasPackagedEntry("BOOT-INF/layers.idx")).isTrue();
|
||||
String layersIndex = getPackagedEntryContent("BOOT-INF/layers.idx");
|
||||
assertThat(Arrays.asList(layersIndex.split("\\n"))).containsExactly("default");
|
||||
|
|
|
|||
|
|
@ -29,8 +29,6 @@ import java.nio.charset.StandardCharsets;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
|
|
@ -45,33 +43,15 @@ final class ClassPathIndexFile {
|
|||
|
||||
private final List<String> lines;
|
||||
|
||||
private final Set<String> folders;
|
||||
|
||||
private ClassPathIndexFile(File root, List<String> lines) {
|
||||
this.root = root;
|
||||
this.lines = lines;
|
||||
this.folders = this.lines.stream().map(this::getFolder).filter(Objects::nonNull).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private String getFolder(String name) {
|
||||
int lastSlash = name.lastIndexOf('/');
|
||||
return (lastSlash != -1) ? name.substring(0, lastSlash) : null;
|
||||
}
|
||||
|
||||
int size() {
|
||||
return this.lines.size();
|
||||
}
|
||||
|
||||
boolean containsFolder(String name) {
|
||||
if (name == null || name.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (name.endsWith("/")) {
|
||||
return containsFolder(name.substring(0, name.length() - 1));
|
||||
}
|
||||
return this.folders.contains(name);
|
||||
}
|
||||
|
||||
boolean containsEntry(String name) {
|
||||
if (name == null || name.isEmpty()) {
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -73,30 +73,16 @@ class ClassPathIndexFileTests {
|
|||
assertThat(indexFile.size()).isEqualTo(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
void containsFolderWhenFolderIsPresentReturnsTrue() throws Exception {
|
||||
ClassPathIndexFile indexFile = copyAndLoadTestIndexFile();
|
||||
assertThat(indexFile.containsFolder("BOOT-INF/layers/one/lib")).isTrue();
|
||||
assertThat(indexFile.containsFolder("BOOT-INF/layers/one/lib/")).isTrue();
|
||||
assertThat(indexFile.containsFolder("BOOT-INF/layers/two/lib")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void containsFolderWhenFolderIsMissingReturnsFalse() throws Exception {
|
||||
ClassPathIndexFile indexFile = copyAndLoadTestIndexFile();
|
||||
assertThat(indexFile.containsFolder("BOOT-INF/layers/nope/lib/")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getUrlsReturnsUrls() throws Exception {
|
||||
ClassPathIndexFile indexFile = copyAndLoadTestIndexFile();
|
||||
List<URL> urls = indexFile.getUrls();
|
||||
List<File> expected = new ArrayList<>();
|
||||
expected.add(new File(this.temp, "BOOT-INF/layers/one/lib/a.jar"));
|
||||
expected.add(new File(this.temp, "BOOT-INF/layers/one/lib/b.jar"));
|
||||
expected.add(new File(this.temp, "BOOT-INF/layers/one/lib/c.jar"));
|
||||
expected.add(new File(this.temp, "BOOT-INF/layers/two/lib/d.jar"));
|
||||
expected.add(new File(this.temp, "BOOT-INF/layers/two/lib/e.jar"));
|
||||
expected.add(new File(this.temp, "a.jar"));
|
||||
expected.add(new File(this.temp, "b.jar"));
|
||||
expected.add(new File(this.temp, "c.jar"));
|
||||
expected.add(new File(this.temp, "d.jar"));
|
||||
expected.add(new File(this.temp, "e.jar"));
|
||||
assertThat(urls).containsExactly(expected.stream().map(this::toUrl).toArray(URL[]::new));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
BOOT-INF/layers/one/lib/a.jar
|
||||
BOOT-INF/layers/one/lib/b.jar
|
||||
BOOT-INF/layers/one/lib/c.jar
|
||||
BOOT-INF/layers/two/lib/d.jar
|
||||
BOOT-INF/layers/two/lib/e.jar
|
||||
a.jar
|
||||
b.jar
|
||||
c.jar
|
||||
d.jar
|
||||
e.jar
|
||||
|
|
|
|||
Loading…
Reference in New Issue