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\"");
}
}

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));
}