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

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