From 2c756ea8743ff1137a101642428686846f351fb4 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 18 Oct 2022 11:24:44 +0100 Subject: [PATCH] Use collectReachabilityMetadata task to add metadata to bootJar Closes gh-32737 --- .../src/docs/asciidoc/reacting.adoc | 1 + .../plugin/NativeImagePluginAction.java | 39 ++----------------- 2 files changed, 5 insertions(+), 35 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/reacting.adoc b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/reacting.adoc index 49b124971a..04dcff5db6 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/reacting.adoc +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/reacting.adoc @@ -78,5 +78,6 @@ When the {nbt-gradle-plugin}[GraalVM Native Image plugin] is applied to a projec . Adds the output of the `aot` source set to the classpath of the `main` GraalVM native binary. . Adds the output of the `aotTest` source set to the classpath of the `test` GraalVM native binary. . Configures the GraalVM extension to disable Toolchain detection. +. Configures the `bootJar` task to include the reachability metadata produced by the `collectReachabilityMetadata` task in its jar. diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/NativeImagePluginAction.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/NativeImagePluginAction.java index 8834294be8..aacae7f6b4 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/NativeImagePluginAction.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/NativeImagePluginAction.java @@ -16,22 +16,15 @@ package org.springframework.boot.gradle.plugin; -import java.io.File; -import java.net.URI; -import java.nio.file.Path; - import org.graalvm.buildtools.gradle.NativeImagePlugin; import org.graalvm.buildtools.gradle.dsl.GraalVMExtension; import org.graalvm.buildtools.gradle.dsl.GraalVMReachabilityMetadataRepositoryExtension; -import org.graalvm.buildtools.gradle.dsl.NativeImageOptions; import org.gradle.api.Action; import org.gradle.api.Plugin; import org.gradle.api.Project; -import org.gradle.api.file.FileCopyDetails; import org.gradle.api.plugins.ExtensionAware; import org.gradle.api.plugins.JavaPlugin; import org.gradle.api.plugins.JavaPluginExtension; -import org.gradle.api.provider.Property; import org.gradle.api.tasks.SourceSetContainer; import org.gradle.api.tasks.SourceSetOutput; @@ -64,7 +57,7 @@ class NativeImagePluginAction implements PluginApplicationAction { configureNativeBinaryClasspath(sourceSets, graalVmExtension, NativeImagePlugin.NATIVE_TEST_EXTENSION, SpringBootAotPlugin.AOT_TEST_SOURCE_SET_NAME); configureGraalVmReachabilityExtension(graalVmExtension); - copyReachabilityMetadataToBootJar(project, graalVmExtension); + copyReachabilityMetadataToBootJar(project); }); } @@ -86,33 +79,9 @@ class NativeImagePluginAction implements PluginApplicationAction { extension.getEnabled().set(true); } - private void copyReachabilityMetadataToBootJar(Project project, GraalVMExtension graalVmExtension) { - Path repositoryCacheDir = new File(project.getGradle().getGradleUserHomeDir(), - "native-build-tools/repositories").toPath(); - - project.getTasks().named(SpringBootPlugin.BOOT_JAR_TASK_NAME, BootJar.class).configure((bootJar) -> { - NativeImageOptions options = graalVmExtension.getBinaries().named(NativeImagePlugin.NATIVE_MAIN_EXTENSION) - .get(); - GraalVMReachabilityMetadataRepositoryExtension metadataRepositoryExtension = ((ExtensionAware) graalVmExtension) - .getExtensions().getByType(GraalVMReachabilityMetadataRepositoryExtension.class); - Property metadataRepositoryUri = metadataRepositoryExtension.getUri(); - bootJar.from(options.getConfigurationFileDirectories()) - .eachFile((file) -> normalizePathIfNecessary(repositoryCacheDir, metadataRepositoryUri, file)); - }); - } - - private void normalizePathIfNecessary(Path repositoryCacheDir, Property metadataRepositoryUri, - FileCopyDetails configurationFile) { - Path configurationFilePath = configurationFile.getFile().toPath(); - Path repositoryMetadataRoot = ("file".equals(metadataRepositoryUri.get().getScheme())) - ? Path.of(metadataRepositoryUri.get()) : repositoryCacheDir; - if (configurationFilePath.startsWith(repositoryMetadataRoot)) { - Path versionDir = configurationFilePath.getParent(); - Path artifactDir = versionDir.getParent(); - Path groupDir = artifactDir.getParent(); - Path gavParentDir = groupDir.getParent(); - configurationFile.setPath("/META-INF/native-image/" + gavParentDir.relativize(configurationFilePath)); - } + private void copyReachabilityMetadataToBootJar(Project project) { + project.getTasks().named(SpringBootPlugin.BOOT_JAR_TASK_NAME, BootJar.class) + .configure((bootJar) -> bootJar.from(project.getTasks().named("collectReachabilityMetadata"))); } }