Use collectReachabilityMetadata task to add metadata to bootJar

Closes gh-32737
pull/33505/head
Andy Wilkinson 2 years ago
parent b986a9b12e
commit 2c756ea874

@ -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.

@ -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<URI> metadataRepositoryUri = metadataRepositoryExtension.getUri();
bootJar.from(options.getConfigurationFileDirectories())
.eachFile((file) -> normalizePathIfNecessary(repositoryCacheDir, metadataRepositoryUri, file));
});
}
private void normalizePathIfNecessary(Path repositoryCacheDir, Property<URI> 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")));
}
}

Loading…
Cancel
Save