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

@@ -16,14 +16,20 @@
package org.springframework.boot.loader.tools;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
@@ -31,6 +37,9 @@ import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
@@ -52,6 +61,8 @@ import org.springframework.util.StringUtils;
*/
public abstract class Packager {
private static final String REACHABILITY_METADATA_PROPERTIES_LOCATION = "META-INF/native-image/%s/%s/reachability-metadata.properties";
private static final String MAIN_CLASS_ATTRIBUTE = "Main-Class";
private static final String START_CLASS_ATTRIBUTE = "Start-Class";
@@ -196,7 +207,8 @@ public abstract class Packager {
writeLoaderClasses(writer);
writer.writeEntries(sourceJar, getEntityTransformer(), libraries.getUnpackHandler(),
libraries.getLibraryLookup());
libraries.write(writer);
Map<String, Library> writtenLibraries = libraries.write(writer);
writeNativeImageArgFile(writer, sourceJar, writtenLibraries);
if (isLayered()) {
writeLayerIndex(writer);
}
@@ -212,6 +224,39 @@ public abstract class Packager {
}
}
private void writeNativeImageArgFile(AbstractJarWriter writer, JarFile sourceJar,
Map<String, Library> writtenLibraries) throws IOException {
Set<String> excludes = new LinkedHashSet<>();
for (Map.Entry<String, Library> entry : writtenLibraries.entrySet()) {
LibraryCoordinates coordinates = entry.getValue().getCoordinates();
ZipEntry zipEntry = (coordinates != null) ? sourceJar.getEntry(REACHABILITY_METADATA_PROPERTIES_LOCATION
.formatted(coordinates.getGroupId(), coordinates.getArtifactId())) : null;
if (zipEntry != null) {
try (InputStream inputStream = sourceJar.getInputStream(zipEntry)) {
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.isEmpty()) {
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/.*\"");
}
String contents = args.stream().collect(Collectors.joining("\n")) + "\n";
writer.writeEntry("META-INF/native-image/argfile",
new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8)));
}
}
private void writeLayerIndex(AbstractJarWriter writer) throws IOException {
String name = this.layout.getLayersIndexFileLocation();
if (StringUtils.hasLength(name)) {
@@ -492,21 +537,22 @@ public abstract class Packager {
return this.libraryLookup;
}
void write(AbstractJarWriter writer) throws IOException {
List<String> writtenPaths = new ArrayList<>();
Map<String, Library> write(AbstractJarWriter writer) throws IOException {
Map<String, Library> writtenLibraries = new LinkedHashMap<>();
for (Entry<String, Library> entry : this.libraries.entrySet()) {
String path = entry.getKey();
Library library = entry.getValue();
if (library.isIncluded()) {
String location = path.substring(0, path.lastIndexOf('/') + 1);
writer.writeNestedLibrary(location, library);
writtenPaths.add(path);
writtenLibraries.put(path, library);
}
}
writeClasspathIndexIfNecessary(writtenPaths, getLayout(), writer);
writeClasspathIndexIfNecessary(writtenLibraries.keySet(), getLayout(), writer);
return writtenLibraries;
}
private void writeClasspathIndexIfNecessary(List<String> paths, Layout layout, AbstractJarWriter writer)
private void writeClasspathIndexIfNecessary(Collection<String> paths, Layout layout, AbstractJarWriter writer)
throws IOException {
if (layout.getClasspathIndexFileLocation() != null) {
List<String> names = paths.stream().map((path) -> "- \"" + path + "\"").toList();

View File

@@ -20,6 +20,7 @@ import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
@@ -614,6 +615,42 @@ abstract class AbstractPackagerTests<P extends Packager> {
assertThat(packagedEntryNames).containsExactly("WEB-INF/lib/" + libraryTwo.getName());
}
@Test
void nativeImageArgFileWithExcludesIsWritten() throws Exception {
this.testJarFile.addClass("com/example/Application.class", ClassWithMainMethod.class);
File libraryOne = createLibraryJar();
File libraryTwo = createLibraryJar();
File libraryThree = createLibraryJar();
File libraryFour = createLibraryJar();
this.testJarFile.addFile("META-INF/native-image/com.example.one/lib-one/reachability-metadata.properties",
new ByteArrayInputStream("override=true\n".getBytes(StandardCharsets.ISO_8859_1)));
this.testJarFile.addFile("META-INF/native-image/com.example.two/lib-two/reachability-metadata.properties",
new ByteArrayInputStream("override=true\n".getBytes(StandardCharsets.ISO_8859_1)));
this.testJarFile.addFile("META-INF/native-image/com.example.three/lib-three/reachability-metadata.properties",
new ByteArrayInputStream("other=test\n".getBytes(StandardCharsets.ISO_8859_1)));
P packager = createPackager(this.testJarFile.getFile());
execute(packager, (callback) -> {
callback.library(new Library(null, libraryOne, LibraryScope.COMPILE,
LibraryCoordinates.of("com.example.one", "lib-one", "123"), false, false, true));
callback.library(new Library(null, libraryTwo, LibraryScope.COMPILE,
LibraryCoordinates.of("com.example.two", "lib-two", "123"), false, false, true));
callback.library(new Library(null, libraryThree, LibraryScope.COMPILE,
LibraryCoordinates.of("com.example.three", "lib-three", "123"), false, false, true));
callback.library(new Library(null, libraryFour, LibraryScope.COMPILE,
LibraryCoordinates.of("com.example.four", "lib-four", "123"), false, false, true));
});
List<String> expected = new ArrayList<>();
expected.add("--exclude-config");
expected.add("\"\\\\Q" + libraryOne.getName() + "\\\\E\"");
expected.add("\"^/META-INF/native-image/.*\"");
expected.add("--exclude-config");
expected.add("\"\\\\Q" + libraryTwo.getName() + "\\\\E\"");
expected.add("\"^/META-INF/native-image/.*\"");
assertThat(getPackagedEntryContent("META-INF/native-image/argfile"))
.isEqualTo(expected.stream().collect(Collectors.joining("\n")) + "\n");
}
private File createLibraryJar() throws IOException {
TestJarFile library = new TestJarFile(this.tempDir);
library.addClass("com/example/library/Library.class", ClassWithoutMainMethod.class);

View File

@@ -68,11 +68,15 @@ public class TestJarFile {
}
public void addFile(String filename, File fileToCopy) throws IOException {
try (InputStream inputStream = new FileInputStream(fileToCopy)) {
addFile(filename, inputStream);
}
}
public void addFile(String filename, InputStream inputStream) throws IOException {
File file = getFilePath(filename);
file.getParentFile().mkdirs();
try (InputStream inputStream = new FileInputStream(fileToCopy)) {
copyToFile(inputStream, file);
}
copyToFile(inputStream, file);
this.entries.add(new FileSource(filename, file));
}