Propagate manifest to exploded jars
Update `LaunchedURLClassLoader` so that packages defined from exploded archive folders have manifest attributes applied to them. Prior to this calling `package.getImplementationTitle()` would only return the a manifiest attribute when running non-exploded. The root cause of this issue is the way that `URLClassLoader` handles the different URL types. For URLs that reference a jar the manifest is available. For URLs that reference a folder it isn't. When running exploded we use a URL that references to the `BOOT-INF/classes` folder directly. To fix the issue we now attempt to detect when `definePackage` is being called directly, and replace `null` entries with actual manifest values. Fixes gh-21705
This commit is contained in:
@@ -33,6 +33,7 @@ import java.util.Set;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.jar.JarOutputStream;
|
||||
import java.util.jar.Manifest;
|
||||
import java.util.zip.CRC32;
|
||||
import java.util.zip.ZipEntry;
|
||||
|
||||
@@ -59,19 +60,31 @@ public abstract class AbstractExecutableArchiveLauncherTests {
|
||||
@SuppressWarnings("resource")
|
||||
protected File createJarArchive(String name, String entryPrefix, boolean indexed, List<String> extraLibs)
|
||||
throws IOException {
|
||||
return createJarArchive(name, null, entryPrefix, indexed, extraLibs);
|
||||
}
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
protected File createJarArchive(String name, Manifest manifest, String entryPrefix, boolean indexed,
|
||||
List<String> extraLibs) throws IOException {
|
||||
File archive = new File(this.tempDir, name);
|
||||
JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(archive));
|
||||
if (manifest != null) {
|
||||
jarOutputStream.putNextEntry(new JarEntry("META-INF/"));
|
||||
jarOutputStream.putNextEntry(new JarEntry("META-INF/MANIFEST.MF"));
|
||||
manifest.write(jarOutputStream);
|
||||
jarOutputStream.closeEntry();
|
||||
}
|
||||
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);
|
||||
jarOutputStream.putNextEntry(new JarEntry(entryPrefix + "/classpath.idx"));
|
||||
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();
|
||||
jarOutputStream.closeEntry();
|
||||
}
|
||||
addNestedJars(entryPrefix, "/lib/foo.jar", jarOutputStream);
|
||||
addNestedJars(entryPrefix, "/lib/bar.jar", jarOutputStream);
|
||||
|
||||
@@ -24,12 +24,17 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.jar.Attributes;
|
||||
import java.util.jar.Attributes.Name;
|
||||
import java.util.jar.Manifest;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.loader.archive.Archive;
|
||||
import org.springframework.boot.loader.archive.ExplodedArchive;
|
||||
import org.springframework.boot.loader.archive.JarFileArchive;
|
||||
import org.springframework.boot.testsupport.compiler.TestCompiler;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -95,6 +100,26 @@ class JarLauncherTests extends AbstractExecutableArchiveLauncherTests {
|
||||
assertThat(urls).containsExactly(expectedFileUrls);
|
||||
}
|
||||
|
||||
@Test
|
||||
void explodedJarDefinedPackagesIncludeManifestAttributes() throws Exception {
|
||||
Manifest manifest = new Manifest();
|
||||
Attributes attributes = manifest.getMainAttributes();
|
||||
attributes.put(Name.MANIFEST_VERSION, "1.0");
|
||||
attributes.put(Name.IMPLEMENTATION_TITLE, "test");
|
||||
File explodedRoot = explode(
|
||||
createJarArchive("archive.jar", manifest, "BOOT-INF", true, Collections.emptyList()));
|
||||
TestCompiler compiler = new TestCompiler(new File(explodedRoot, "BOOT-INF/classes"));
|
||||
File source = new File(this.tempDir, "explodedsample/ExampleClass.java");
|
||||
source.getParentFile().mkdirs();
|
||||
FileCopyUtils.copy(new File("src/test/resources/explodedsample/ExampleClass.txt"), source);
|
||||
compiler.getTask(Collections.singleton(source)).call();
|
||||
JarLauncher launcher = new JarLauncher(new ExplodedArchive(explodedRoot, true));
|
||||
Iterator<Archive> archives = launcher.getClassPathArchivesIterator();
|
||||
URLClassLoader classLoader = (URLClassLoader) launcher.createClassLoader(archives);
|
||||
Class<?> loaded = classLoader.loadClass("explodedsample.ExampleClass");
|
||||
assertThat(loaded.getPackage().getImplementationTitle()).isEqualTo("test");
|
||||
}
|
||||
|
||||
protected final URL[] getExpectedFileUrls(File explodedRoot) {
|
||||
return getExpectedFiles(explodedRoot).stream().map(this::toUrl).toArray(URL[]::new);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* 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 explodedsample;
|
||||
|
||||
/**
|
||||
* Example class used to test class loading.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class ExampleClass {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user