Make bootBuildImage produce native image when NBT plugin is applied

Closes gh-32768
This commit is contained in:
Andy Wilkinson 2022-10-18 13:12:45 +01:00
parent 2c756ea874
commit 2dacddb4d7
5 changed files with 33 additions and 2 deletions

View File

@ -105,7 +105,7 @@ The following table summarizes the available properties and their default values
| `builder`
| `--builder`
| Name of the Builder image to use.
| `paketobuildpacks/builder:base`
| `paketobuildpacks/builder:base` or `paketobuildpacks/builder:tiny` when {nbt-gradle-plugin}[GraalVM Native Image plugin] is applied.
| `runImage`
| `--runImage`
@ -126,7 +126,7 @@ Acceptable values are `ALWAYS`, `NEVER`, and `IF_NOT_PRESENT`.
| `environment`
|
| Environment variables that should be passed to the builder.
|
| Empty or `['BP_NATIVE_IMAGE': 'true']` when {nbt-gradle-plugin}[GraalVM Native Image plugin] is applied.
| `buildpacks`
|

View File

@ -79,5 +79,6 @@ When the {nbt-gradle-plugin}[GraalVM Native Image plugin] is applied to a projec
. 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.
. Configures the `bootBuildImage` task to use `paketobuildpacks/builder:tiny` as its builder and to set `BP_NATIVE_IMAGE` to `true` in its environment.

View File

@ -28,6 +28,7 @@ import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.SourceSetOutput;
import org.springframework.boot.gradle.tasks.bundling.BootBuildImage;
import org.springframework.boot.gradle.tasks.bundling.BootJar;
/**
@ -58,6 +59,7 @@ class NativeImagePluginAction implements PluginApplicationAction {
SpringBootAotPlugin.AOT_TEST_SOURCE_SET_NAME);
configureGraalVmReachabilityExtension(graalVmExtension);
copyReachabilityMetadataToBootJar(project);
configureBootBuildImageToProduceANativeImage(project);
});
}
@ -84,4 +86,12 @@ class NativeImagePluginAction implements PluginApplicationAction {
.configure((bootJar) -> bootJar.from(project.getTasks().named("collectReachabilityMetadata")));
}
private void configureBootBuildImageToProduceANativeImage(Project project) {
project.getTasks().named(SpringBootPlugin.BOOT_BUILD_IMAGE_TASK_NAME, BootBuildImage.class)
.configure((bootBuildImage) -> {
bootBuildImage.setBuilder("paketobuildpacks/builder:tiny");
bootBuildImage.environment("BP_NATIVE_IMAGE", "true");
});
}
}

View File

@ -87,6 +87,13 @@ class NativeImagePluginActionIntegrationTests {
"META-INF/native-image/org.jline/jline/3.21.0/resource-config.json");
}
@TestTemplate
void bootBuildImageIsConfiguredToBuildANativeImage() {
writeDummySpringApplicationAotProcessorMainClass();
BuildResult result = this.gradleBuild.build("bootBuildImageConfiguration");
assertThat(result.getOutput()).contains("paketobuildpacks/builder:tiny").contains("BP_NATIVE_IMAGE = true");
}
private void writeDummySpringApplicationAotProcessorMainClass() {
File examplePackage = new File(this.gradleBuild.getProjectDir(), "src/main/java/org/springframework/boot");
examplePackage.mkdirs();

View File

@ -0,0 +1,13 @@
plugins {
id 'org.springframework.boot' version '{version}'
id 'java'
}
apply plugin: 'org.graalvm.buildtools.native'
task('bootBuildImageConfiguration') {
doFirst {
println "builder = ${tasks.getByName('bootBuildImage').builder}"
println "BP_NATIVE_IMAGE = ${tasks.getByName('bootBuildImage').environment['BP_NATIVE_IMAGE']}"
}
}