Add native image manifest entry

A manifest entry `Spring-Boot-Native-Processed: true` is added to the
jar manifest by the Maven or Gradle plugin when the jar has been built
for use in a native image. With the Gradle plugin, this is done in
reaction to the GraalVM Native Image Plugin being applied to the
project. With the Maven plugin, this is done when the `native` profile
is applied to the build.
This commit is contained in:
Scott Frederick
2023-05-02 15:11:49 -05:00
parent 5ac6a3d90b
commit 23ae91b008
4 changed files with 41 additions and 3 deletions

View File

@@ -25,6 +25,7 @@ import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.TaskOutcome;
@@ -115,6 +116,17 @@ class NativeImagePluginActionIntegrationTests {
projectPath("build/resources/aotTest"), projectPath("build/generated/aotTestClasses"));
}
@TestTemplate
void nativeEntryIsAddedToManifest() throws IOException {
writeDummySpringApplicationAotProcessorMainClass();
BuildResult result = this.gradleBuild.build("bootJar");
assertThat(result.task(":bootJar").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
File buildLibs = new File(this.gradleBuild.getProjectDir(), "build/libs");
JarFile jarFile = new JarFile(new File(buildLibs, this.gradleBuild.getProjectDir().getName() + ".jar"));
Manifest manifest = jarFile.getManifest();
assertThat(manifest.getMainAttributes().getValue("Spring-Boot-Native-Processed")).isEqualTo("true");
}
private String projectPath(String path) {
try {
return new File(this.gradleBuild.getProjectDir(), path).getCanonicalPath();