Generate 'META-INF/native-image/argfile' file for buildpack use

Update the Maven and Gradle plugin to generate an `argfile` file
file under `META-INF/native-image` that contains `--exclude-config`
arguments that should be passed when generating a native image.

The contents of the file is generated for each nested jar that has a
`reachability-metadata.properties` file containing 'override=true'.

The `reachability-metadata.properties` file is expected to be generated
by the Graal native build tools plugin.

Closes gh-32738
This commit is contained in:
Phillip Webb
2022-10-06 13:57:29 -07:00
parent 430c6b7e9f
commit 071649360b
10 changed files with 227 additions and 34 deletions

View File

@@ -105,11 +105,12 @@ class BootArchiveSupport {
return (version != null) ? version : "unknown";
}
CopyAction createCopyAction(Jar jar) {
return createCopyAction(jar, null, null);
CopyAction createCopyAction(Jar jar, ResolvedDependencies resolvedDependencies) {
return createCopyAction(jar, resolvedDependencies, null, null);
}
CopyAction createCopyAction(Jar jar, LayerResolver layerResolver, String layerToolsLocation) {
CopyAction createCopyAction(Jar jar, ResolvedDependencies resolvedDependencies, LayerResolver layerResolver,
String layerToolsLocation) {
File output = jar.getArchiveFile().get().getAsFile();
Manifest manifest = jar.getManifest();
boolean preserveFileTimestamps = jar.isPreserveFileTimestamps();
@@ -122,7 +123,7 @@ class BootArchiveSupport {
String encoding = jar.getMetadataCharset();
CopyAction action = new BootZipCopyAction(output, manifest, preserveFileTimestamps, includeDefaultLoader,
layerToolsLocation, requiresUnpack, exclusions, launchScript, librarySpec, compressionResolver,
encoding, layerResolver);
encoding, resolvedDependencies, layerResolver);
return jar.isReproducibleFileOrder() ? new ReproducibleOrderingCopyAction(action) : action;
}

View File

@@ -136,9 +136,9 @@ public class BootJar extends Jar implements BootArchive {
if (!isLayeredDisabled()) {
LayerResolver layerResolver = new LayerResolver(this.resolvedDependencies, this.layered, this::isLibrary);
String layerToolsLocation = this.layered.isIncludeLayerTools() ? LIB_DIRECTORY : null;
return this.support.createCopyAction(this, layerResolver, layerToolsLocation);
return this.support.createCopyAction(this, this.resolvedDependencies, layerResolver, layerToolsLocation);
}
return this.support.createCopyAction(this);
return this.support.createCopyAction(this, this.resolvedDependencies);
}
@Override

View File

@@ -111,9 +111,9 @@ public class BootWar extends War implements BootArchive {
if (!isLayeredDisabled()) {
LayerResolver layerResolver = new LayerResolver(this.resolvedDependencies, this.layered, this::isLibrary);
String layerToolsLocation = this.layered.isIncludeLayerTools() ? LIB_DIRECTORY : null;
return this.support.createCopyAction(this, layerResolver, layerToolsLocation);
return this.support.createCopyAction(this, this.resolvedDependencies, layerResolver, layerToolsLocation);
}
return this.support.createCopyAction(this);
return this.support.createCopyAction(this, this.resolvedDependencies);
}
@Override

View File

@@ -22,14 +22,19 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
@@ -47,11 +52,13 @@ import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.WorkResult;
import org.gradle.api.tasks.WorkResults;
import org.springframework.boot.gradle.tasks.bundling.ResolvedDependencies.DependencyDescriptor;
import org.springframework.boot.loader.tools.DefaultLaunchScript;
import org.springframework.boot.loader.tools.FileUtils;
import org.springframework.boot.loader.tools.JarModeLibrary;
import org.springframework.boot.loader.tools.Layer;
import org.springframework.boot.loader.tools.LayersIndex;
import org.springframework.boot.loader.tools.LibraryCoordinates;
import org.springframework.util.Assert;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
@@ -69,6 +76,11 @@ class BootZipCopyAction implements CopyAction {
static final long CONSTANT_TIME_FOR_ZIP_ENTRIES = new GregorianCalendar(1980, Calendar.FEBRUARY, 1, 0, 0, 0)
.getTimeInMillis();
private static final String REACHABILITY_METADATA_PROPERTIES_LOCATION = "META-INF/native-image/%s/%s/reachability-metadata.properties";
private static final Pattern REACHABILITY_METADATA_PROPERTIES_LOCATION_PATTERN = Pattern
.compile(REACHABILITY_METADATA_PROPERTIES_LOCATION.formatted(".*", ".*"));
private final File output;
private final Manifest manifest;
@@ -91,13 +103,15 @@ class BootZipCopyAction implements CopyAction {
private final String encoding;
private final ResolvedDependencies resolvedDependencies;
private final LayerResolver layerResolver;
BootZipCopyAction(File output, Manifest manifest, boolean preserveFileTimestamps, boolean includeDefaultLoader,
String layerToolsLocation, Spec<FileTreeElement> requiresUnpack, Spec<FileTreeElement> exclusions,
LaunchScriptConfiguration launchScript, Spec<FileCopyDetails> librarySpec,
Function<FileCopyDetails, ZipCompression> compressionResolver, String encoding,
LayerResolver layerResolver) {
ResolvedDependencies resolvedDependencies, LayerResolver layerResolver) {
this.output = output;
this.manifest = manifest;
this.preserveFileTimestamps = preserveFileTimestamps;
@@ -109,6 +123,7 @@ class BootZipCopyAction implements CopyAction {
this.librarySpec = librarySpec;
this.compressionResolver = compressionResolver;
this.encoding = encoding;
this.resolvedDependencies = resolvedDependencies;
this.layerResolver = layerResolver;
}
@@ -189,7 +204,9 @@ class BootZipCopyAction implements CopyAction {
private final Set<String> writtenDirectories = new LinkedHashSet<>();
private final Set<String> writtenLibraries = new LinkedHashSet<>();
private final Map<String, FileCopyDetails> writtenLibraries = new LinkedHashMap<>();
private final Map<String, FileCopyDetails> reachabilityMetadataProperties = new HashMap<>();
Processor(ZipArchiveOutputStream out) {
this.out = out;
@@ -241,7 +258,10 @@ class BootZipCopyAction implements CopyAction {
details.copyTo(this.out);
this.out.closeArchiveEntry();
if (BootZipCopyAction.this.librarySpec.isSatisfiedBy(details)) {
this.writtenLibraries.add(name);
this.writtenLibraries.put(name, details);
}
if (REACHABILITY_METADATA_PROPERTIES_LOCATION_PATTERN.matcher(name).matches()) {
this.reachabilityMetadataProperties.put(name, details);
}
if (BootZipCopyAction.this.layerResolver != null) {
Layer layer = BootZipCopyAction.this.layerResolver.getLayer(details);
@@ -271,6 +291,7 @@ class BootZipCopyAction implements CopyAction {
writeLoaderEntriesIfNecessary(null);
writeJarToolsIfNecessary();
writeClassPathIndexIfNecessary();
writeNativeImageArgFileIfNecessary();
// We must write the layer index last
writeLayersIndexIfNecessary();
}
@@ -321,9 +342,45 @@ class BootZipCopyAction implements CopyAction {
Attributes manifestAttributes = BootZipCopyAction.this.manifest.getAttributes();
String classPathIndex = (String) manifestAttributes.get("Spring-Boot-Classpath-Index");
if (classPathIndex != null) {
List<String> lines = this.writtenLibraries.stream().map((line) -> "- \"" + line + "\"").toList();
writeEntry(classPathIndex, ZipEntryContentWriter.fromLines(BootZipCopyAction.this.encoding, lines),
true);
Set<String> libraryNames = this.writtenLibraries.keySet();
List<String> lines = libraryNames.stream().map((line) -> "- \"" + line + "\"").toList();
ZipEntryContentWriter writer = ZipEntryContentWriter.fromLines(BootZipCopyAction.this.encoding, lines);
writeEntry(classPathIndex, writer, true);
}
}
private void writeNativeImageArgFileIfNecessary() throws IOException {
Set<String> excludes = new LinkedHashSet<>();
for (Map.Entry<String, FileCopyDetails> entry : this.writtenLibraries.entrySet()) {
DependencyDescriptor descriptor = BootZipCopyAction.this.resolvedDependencies
.find(entry.getValue().getFile());
LibraryCoordinates coordinates = (descriptor != null) ? descriptor.getCoordinates() : null;
FileCopyDetails propertiesFile = (coordinates != null)
? this.reachabilityMetadataProperties.get(REACHABILITY_METADATA_PROPERTIES_LOCATION
.formatted(coordinates.getGroupId(), coordinates.getArtifactId()))
: null;
if (propertiesFile != null) {
try (InputStream inputStream = propertiesFile.open()) {
Properties properties = new Properties();
properties.load(inputStream);
if (Boolean.parseBoolean(properties.getProperty("override"))) {
excludes.add(entry.getKey());
}
}
}
}
// https://docs.oracle.com/en/java/javase/18/docs/specs/man/java.html#java-command-line-argument-files
if (excludes != null) {
List<String> args = new ArrayList<>();
for (String exclude : excludes) {
int lastSlash = exclude.lastIndexOf('/');
String jar = (lastSlash != -1) ? exclude.substring(lastSlash + 1) : exclude;
args.add("--exclude-config");
args.add("\"" + Pattern.quote(jar).replace("\\", "\\\\") + "\"");
args.add("\"^/META-INF/native-image/.*\"");
}
ZipEntryContentWriter writer = ZipEntryContentWriter.fromLines(BootZipCopyAction.this.encoding, args);
writeEntry("META-INF/native-image/argfile", writer, true);
}
}

View File

@@ -69,6 +69,7 @@ import org.junit.jupiter.api.io.TempDir;
import org.springframework.boot.gradle.junit.GradleProjectBuilder;
import org.springframework.boot.loader.tools.DefaultLaunchScript;
import org.springframework.boot.loader.tools.JarModeLibrary;
import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
@@ -482,6 +483,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
expected.add("- \"dependencies\":");
expected.add(" - \"" + this.libPath + "first-library.jar\"");
expected.add(" - \"" + this.libPath + "first-project-library.jar\"");
expected.add(" - \"" + this.libPath + "fourth-library.jar\"");
expected.add(" - \"" + this.libPath + "second-library.jar\"");
if (!layerToolsJar.contains("SNAPSHOT")) {
expected.add(" - \"" + layerToolsJar + "\"");
@@ -536,6 +538,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
expected.add("- \"my-internal-deps\":");
expected.add(" - \"" + this.libPath + "first-library.jar\"");
expected.add(" - \"" + this.libPath + "first-project-library.jar\"");
expected.add(" - \"" + this.libPath + "fourth-library.jar\"");
expected.add(" - \"" + this.libPath + "second-library.jar\"");
expected.add("- \"my-snapshot-deps\":");
expected.add(" - \"" + this.libPath + "second-project-library-SNAPSHOT.jar\"");
@@ -616,27 +619,43 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
}
File createLayeredJar() throws IOException {
return createLayeredJar((spec) -> {
return createLayeredJar(false);
}
File createLayeredJar(boolean addReachabilityProperties) throws IOException {
return createLayeredJar(addReachabilityProperties, (spec) -> {
});
}
File createLayeredJar(Action<LayeredSpec> action) throws IOException {
return createLayeredJar(false, action);
}
File createLayeredJar(boolean addReachabilityProperties, Action<LayeredSpec> action) throws IOException {
applyLayered(action);
addContent();
addContent(addReachabilityProperties);
executeTask();
return getTask().getArchiveFile().get().getAsFile();
}
File createPopulatedJar() throws IOException {
addContent();
return createPopulatedJar(false);
}
File createPopulatedJar(boolean addReachabilityProperties) throws IOException {
addContent(addReachabilityProperties);
executeTask();
return getTask().getArchiveFile().get().getAsFile();
}
abstract void applyLayered(Action<LayeredSpec> action);
@SuppressWarnings("unchecked")
void addContent() throws IOException {
addContent(false);
}
@SuppressWarnings("unchecked")
void addContent(boolean addReachabilityProperties) throws IOException {
this.task.getMainClass().set("com.example.Main");
File classesJavaMain = new File(this.temp, "classes/java/main");
File applicationClass = new File(classesJavaMain, "com/example/Application.class");
@@ -650,14 +669,20 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
staticResources.mkdir();
File css = new File(staticResources, "test.css");
css.createNewFile();
if (addReachabilityProperties) {
createReachabilityProperties(resourcesMain, "com.example", "first-library", "true");
createReachabilityProperties(resourcesMain, "com.example", "second-library", "true");
createReachabilityProperties(resourcesMain, "com.example", "fourth-library", "false");
}
this.task.classpath(classesJavaMain, resourcesMain, jarFile("first-library.jar"), jarFile("second-library.jar"),
jarFile("third-library-SNAPSHOT.jar"), jarFile("first-project-library.jar"),
jarFile("second-project-library-SNAPSHOT.jar"));
jarFile("third-library-SNAPSHOT.jar"), jarFile("fourth-library.jar"),
jarFile("first-project-library.jar"), jarFile("second-project-library-SNAPSHOT.jar"));
Set<ResolvedArtifact> artifacts = new LinkedHashSet<>();
artifacts.add(mockLibraryArtifact("first-library.jar", "com.example", "first-library", "1.0.0"));
artifacts.add(mockLibraryArtifact("second-library.jar", "com.example", "second-library", "1.0.0"));
artifacts.add(
mockLibraryArtifact("third-library-SNAPSHOT.jar", "com.example", "third-library", "1.0.0.SNAPSHOT"));
artifacts.add(mockLibraryArtifact("fourth-library.jar", "com.example", "fourth-library", "1.0.0"));
artifacts
.add(mockProjectArtifact("first-project-library.jar", "com.example", "first-project-library", "1.0.0"));
artifacts.add(mockProjectArtifact("second-project-library-SNAPSHOT.jar", "com.example",
@@ -682,6 +707,14 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
populateResolvedDependencies(configuration);
}
protected void createReachabilityProperties(File directory, String groupId, String artifactId, String override)
throws IOException {
File targetDirectory = new File(directory, "META-INF/native-image/%s/%s".formatted(groupId, artifactId));
File target = new File(targetDirectory, "reachability-metadata.properties");
targetDirectory.mkdirs();
FileCopyUtils.copy("override=%s\n".formatted(override).getBytes(StandardCharsets.ISO_8859_1), target);
}
abstract void populateResolvedDependencies(Configuration configuration);
private ResolvedArtifact mockLibraryArtifact(String fileName, String group, String module, String version) {

View File

@@ -86,7 +86,8 @@ class BootJarTests extends AbstractBootArchiveTests<BootJar> {
try (JarFile jarFile = new JarFile(createLayeredJar())) {
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\"", "- \"BOOT-INF/lib/first-project-library.jar\"",
"- \"BOOT-INF/lib/third-library-SNAPSHOT.jar\"", "- \"BOOT-INF/lib/fourth-library.jar\"",
"- \"BOOT-INF/lib/first-project-library.jar\"",
"- \"BOOT-INF/lib/second-project-library-SNAPSHOT.jar\"");
}
}
@@ -98,7 +99,8 @@ class BootJarTests extends AbstractBootArchiveTests<BootJar> {
.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\"", "- \"BOOT-INF/lib/first-project-library.jar\"",
"- \"BOOT-INF/lib/third-library-SNAPSHOT.jar\"", "- \"BOOT-INF/lib/fourth-library.jar\"",
"- \"BOOT-INF/lib/first-project-library.jar\"",
"- \"BOOT-INF/lib/second-project-library-SNAPSHOT.jar\"");
}
}
@@ -181,7 +183,15 @@ class BootJarTests extends AbstractBootArchiveTests<BootJar> {
assertThat(jarFile.getEntry("BOOT-INF/classes/META-INF/services/com.example.Service")).isNotNull();
assertThat(jarFile.getEntry("META-INF/services/com.example.Service")).isNull();
}
}
@Test
void nativeImageArgFileWithExcludesIsWritten() throws IOException {
try (JarFile jarFile = new JarFile(createLayeredJar(true))) {
assertThat(entryLines(jarFile, "META-INF/native-image/argfile")).containsExactly("--exclude-config",
"\"\\\\Qfirst-library.jar\\\\E\"", "\"^/META-INF/native-image/.*\"", "--exclude-config",
"\"\\\\Qsecond-library.jar\\\\E\"", "\"^/META-INF/native-image/.*\"");
}
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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.
@@ -24,6 +24,8 @@ import org.gradle.api.Action;
import org.gradle.api.artifacts.Configuration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
import static org.assertj.core.api.Assertions.assertThat;
/**
@@ -32,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Andy Wilkinson
* @author Scott Frederick
*/
@ClassPathExclusions("kotlin-daemon-client-*")
class BootWarTests extends AbstractBootArchiveTests<BootWar> {
BootWarTests() {
@@ -115,7 +118,8 @@ class BootWarTests extends AbstractBootArchiveTests<BootWar> {
try (JarFile jarFile = new JarFile(createLayeredJar())) {
assertThat(entryLines(jarFile, "WEB-INF/classpath.idx")).containsExactly(
"- \"WEB-INF/lib/first-library.jar\"", "- \"WEB-INF/lib/second-library.jar\"",
"- \"WEB-INF/lib/third-library-SNAPSHOT.jar\"", "- \"WEB-INF/lib/first-project-library.jar\"",
"- \"WEB-INF/lib/third-library-SNAPSHOT.jar\"", "- \"WEB-INF/lib/fourth-library.jar\"",
"- \"WEB-INF/lib/first-project-library.jar\"",
"- \"WEB-INF/lib/second-project-library-SNAPSHOT.jar\"");
}
}
@@ -127,7 +131,8 @@ class BootWarTests extends AbstractBootArchiveTests<BootWar> {
.isEqualTo("WEB-INF/classpath.idx");
assertThat(entryLines(jarFile, "WEB-INF/classpath.idx")).containsExactly(
"- \"WEB-INF/lib/first-library.jar\"", "- \"WEB-INF/lib/second-library.jar\"",
"- \"WEB-INF/lib/third-library-SNAPSHOT.jar\"", "- \"WEB-INF/lib/first-project-library.jar\"",
"- \"WEB-INF/lib/third-library-SNAPSHOT.jar\"", "- \"WEB-INF/lib/fourth-library.jar\"",
"- \"WEB-INF/lib/first-project-library.jar\"",
"- \"WEB-INF/lib/second-project-library-SNAPSHOT.jar\"");
}
}