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.
pull/35256/head
Scott Frederick 2 years ago
parent 5ac6a3d90b
commit 23ae91b008

@ -233,6 +233,17 @@ publishing.publications.withType(MavenPublication) {
build {
pluginManagement {
plugins {
plugin {
delegate.groupId('org.apache.maven.plugins')
delegate.artifactId('maven-jar-plugin')
configuration {
archive {
manifestEntries {
delegate.'Spring-Boot-Native-Processed'("true")
}
}
}
}
plugin {
delegate.groupId('org.springframework.boot')
delegate.artifactId('spring-boot-maven-plugin')

@ -59,10 +59,11 @@ class NativeImagePluginAction implements PluginApplicationAction {
SourceSetContainer sourceSets = javaPluginExtension.getSourceSets();
GraalVMExtension graalVmExtension = configureGraalVmExtension(project);
configureMainNativeBinaryClasspath(project, sourceSets, graalVmExtension);
configureTestNativeBinaryClasspath(project, sourceSets, graalVmExtension);
configureTestNativeBinaryClasspath(sourceSets, graalVmExtension);
configureGraalVmReachabilityExtension(graalVmExtension);
copyReachabilityMetadataToBootJar(project);
configureBootBuildImageToProduceANativeImage(project);
configureJarManifestNativeAttribute(project);
});
}
@ -85,8 +86,7 @@ class NativeImagePluginAction implements PluginApplicationAction {
return !SpringBootPlugin.DEVELOPMENT_ONLY_CONFIGURATION_NAME.equals(configuration.getName());
}
private void configureTestNativeBinaryClasspath(Project project, SourceSetContainer sourceSets,
GraalVMExtension graalVmExtension) {
private void configureTestNativeBinaryClasspath(SourceSetContainer sourceSets, GraalVMExtension graalVmExtension) {
FileCollection runtimeClasspath = sourceSets.getByName(SpringBootAotPlugin.AOT_TEST_SOURCE_SET_NAME)
.getRuntimeClasspath();
graalVmExtension.getBinaries().getByName(NativeImagePlugin.NATIVE_TEST_EXTENSION).classpath(runtimeClasspath);
@ -119,4 +119,11 @@ class NativeImagePluginAction implements PluginApplicationAction {
});
}
private void configureJarManifestNativeAttribute(Project project) {
project.getTasks()
.named(SpringBootPlugin.BOOT_JAR_TASK_NAME, BootJar.class)
.configure((bootJar) -> bootJar
.manifest(((manifest) -> manifest.getAttributes().put("Spring-Boot-Native-Processed", true))));
}
}

@ -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();

@ -0,0 +1,8 @@
plugins {
id 'java'
id 'org.springframework.boot'
id 'org.springframework.boot.aot'
}
apply plugin: 'org.graalvm.buildtools.native'
Loading…
Cancel
Save