Browse Source

Make bootBuildImage produce native image when NBT plugin is applied

Closes gh-32768
pull/33505/head
Andy Wilkinson 3 years ago
parent
commit
2dacddb4d7
  1. 4
      spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/packaging-oci-image.adoc
  2. 1
      spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/reacting.adoc
  3. 10
      spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/NativeImagePluginAction.java
  4. 7
      spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/NativeImagePluginActionIntegrationTests.java
  5. 13
      spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/NativeImagePluginActionIntegrationTests-bootBuildImageIsConfiguredToBuildANativeImage.gradle

4
spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/packaging-oci-image.adoc

@ -105,7 +105,7 @@ The following table summarizes the available properties and their default values @@ -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`. @@ -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`
|

1
spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/reacting.adoc

@ -79,5 +79,6 @@ When the {nbt-gradle-plugin}[GraalVM Native Image plugin] is applied to a projec @@ -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.

10
spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/NativeImagePluginAction.java

@ -28,6 +28,7 @@ import org.gradle.api.plugins.JavaPluginExtension; @@ -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 { @@ -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 { @@ -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");
});
}
}

7
spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/NativeImagePluginActionIntegrationTests.java

@ -87,6 +87,13 @@ class NativeImagePluginActionIntegrationTests { @@ -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();

13
spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/NativeImagePluginActionIntegrationTests-bootBuildImageIsConfiguredToBuildANativeImage.gradle

@ -0,0 +1,13 @@ @@ -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']}"
}
}
Loading…
Cancel
Save