Add classpath index support for exploded archives
Update the `Repackager` class so that an additional `classpath.idx` file is written into the jar that provides the original order of the classpath. The `JarLauncher` class now uses this file when running as an exploded archive to ensure that the classpath order is the same as when running from the far jar. Closes gh-9128 Co-authored-by: Phillip Webb <pwebb@pivotal.io>
This commit is contained in:
committed by
Phillip Webb
parent
ad72f86bdb
commit
45b1ab46c3
@@ -20,8 +20,11 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -41,6 +44,7 @@ import org.springframework.util.FileCopyUtils;
|
||||
* Base class for testing {@link ExecutableArchiveLauncher} implementations.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
public abstract class AbstractExecutableArchiveLauncherTests {
|
||||
|
||||
@@ -48,11 +52,25 @@ public abstract class AbstractExecutableArchiveLauncherTests {
|
||||
File tempDir;
|
||||
|
||||
protected File createJarArchive(String name, String entryPrefix) throws IOException {
|
||||
return createJarArchive(name, entryPrefix, false);
|
||||
}
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
protected File createJarArchive(String name, String entryPrefix, boolean indexed) throws IOException {
|
||||
File archive = new File(this.tempDir, name);
|
||||
JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(archive));
|
||||
jarOutputStream.putNextEntry(new JarEntry(entryPrefix + "/"));
|
||||
jarOutputStream.putNextEntry(new JarEntry(entryPrefix + "/classes/"));
|
||||
jarOutputStream.putNextEntry(new JarEntry(entryPrefix + "/lib/"));
|
||||
if (indexed) {
|
||||
JarEntry indexEntry = new JarEntry(entryPrefix + "/classpath.idx");
|
||||
jarOutputStream.putNextEntry(indexEntry);
|
||||
Writer writer = new OutputStreamWriter(jarOutputStream, StandardCharsets.UTF_8);
|
||||
writer.write("BOOT-INF/lib/foo.jar\n");
|
||||
writer.write("BOOT-INF/lib/bar.jar\n");
|
||||
writer.write("BOOT-INF/lib/baz.jar\n");
|
||||
writer.flush();
|
||||
}
|
||||
addNestedJars(entryPrefix, "/lib/foo.jar", jarOutputStream);
|
||||
addNestedJars(entryPrefix, "/lib/bar.jar", jarOutputStream);
|
||||
addNestedJars(entryPrefix, "/lib/baz.jar", jarOutputStream);
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.loader;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests for {@link ClassPathIndexFile}.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class ClassPathIndexFileTests {
|
||||
|
||||
@TempDir
|
||||
File temp;
|
||||
|
||||
@Test
|
||||
void loadIfPossibleWhenRootIsNotFileReturnsNull() throws IOException {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> ClassPathIndexFile.loadIfPossible(new URL("https://example.com/file"), "test.idx"))
|
||||
.withMessage("URL does not reference a file");
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadIfPossibleWhenRootDoesNotExistReturnsNull() throws Exception {
|
||||
File root = new File(this.temp, "missing");
|
||||
assertThat(ClassPathIndexFile.loadIfPossible(root.toURI().toURL(), "test.idx")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadIfPossibleWhenRootIsFolderThrowsException() throws Exception {
|
||||
File root = new File(this.temp, "folder");
|
||||
root.mkdirs();
|
||||
assertThat(ClassPathIndexFile.loadIfPossible(root.toURI().toURL(), "test.idx")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadIfPossibleReturnsInstance() throws Exception {
|
||||
ClassPathIndexFile indexFile = copyAndLoadTestIndexFile();
|
||||
assertThat(indexFile).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void sizeReturnsNumberOfLines() throws Exception {
|
||||
ClassPathIndexFile indexFile = copyAndLoadTestIndexFile();
|
||||
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"));
|
||||
assertThat(urls).containsExactly(expected.stream().map(this::toUrl).toArray(URL[]::new));
|
||||
}
|
||||
|
||||
private URL toUrl(File file) {
|
||||
try {
|
||||
return file.toURI().toURL();
|
||||
}
|
||||
catch (MalformedURLException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private ClassPathIndexFile copyAndLoadTestIndexFile() throws IOException, MalformedURLException {
|
||||
copyTestIndexFile();
|
||||
ClassPathIndexFile indexFile = ClassPathIndexFile.loadIfPossible(this.temp.toURI().toURL(), "test.idx");
|
||||
return indexFile;
|
||||
}
|
||||
|
||||
private void copyTestIndexFile() throws IOException {
|
||||
Files.copy(getClass().getResourceAsStream("classpath-index-file.idx"),
|
||||
new File(this.temp, "test.idx").toPath());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,7 +18,9 @@ package org.springframework.boot.loader;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -33,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* Tests for {@link JarLauncher}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
class JarLauncherTests extends AbstractExecutableArchiveLauncherTests {
|
||||
|
||||
@@ -67,6 +70,16 @@ class JarLauncherTests extends AbstractExecutableArchiveLauncherTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void explodedJarShouldPreserveClasspathOrderWhenIndexPresent() throws Exception {
|
||||
File explodedRoot = explode(createJarArchive("archive.jar", "BOOT-INF", true));
|
||||
JarLauncher launcher = new JarLauncher(new ExplodedArchive(explodedRoot, true));
|
||||
Iterator<Archive> archives = launcher.getClassPathArchivesIterator();
|
||||
URLClassLoader classLoader = (URLClassLoader) launcher.createClassLoader(archives);
|
||||
URL[] urls = classLoader.getURLs();
|
||||
assertThat(urls).containsExactly(getExpectedFileUrls(explodedRoot));
|
||||
}
|
||||
|
||||
protected final URL[] getExpectedFileUrls(File explodedRoot) {
|
||||
return getExpectedFiles(explodedRoot).stream().map(this::toUrl).toArray(URL[]::new);
|
||||
}
|
||||
|
||||
@@ -0,0 +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
|
||||
Reference in New Issue
Block a user