From d0cdae23699a0424a733e0364d09fab481b55750 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Mon, 17 Mar 2025 11:10:43 +0100 Subject: [PATCH] Align nativeTest to native to make it multi-modules friendly This commit harmonizes the behavior of the native and nativeTest Maven profiles. Previously, enabling the nativeTest profile would automatically add an execution of the Spring Boot and Native Build Tools plugins, even in a module that doesn't represent an application or a Spring Boot-related module. With this commit, the native testing feature is only enabled if the plugins are defined in the project, either directly, or in a parent. The documentation has been updated as the behavior of both profiles is now much more consistent. Closes gh-44696 --- .../testing-native-applications.adoc | 4 +- .../spring-boot-starter-parent/build.gradle | 46 ++++++++++--------- .../maven-plugin/examples/aot-test/pom.xml | 25 ---------- .../modules/maven-plugin/pages/aot.adoc | 26 +++++++++-- 4 files changed, 48 insertions(+), 53 deletions(-) delete mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/docs/antora/modules/maven-plugin/examples/aot-test/pom.xml diff --git a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/native-image/testing-native-applications.adoc b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/native-image/testing-native-applications.adoc index 1ccefe199d..aafe2c7c38 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/native-image/testing-native-applications.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/native-image/testing-native-applications.adoc @@ -85,7 +85,9 @@ You should have a `` section that looks like this: ---- -The `spring-boot-starter-parent` declares a `nativeTest` profile that configures the executions that are needed to run the native tests. +The `spring-boot-starter-parent` defines a `nativeTest` profile that provides the necessary configuration for the Spring Boot and Native Build Tools plugins. +First you need to add those two plugin in the module to opt-in for the feature. +Your tests are executed in native mode only when the `nativeTest` is enabled. You can activate profiles using the `-P` flag on the command line. TIP: If you don't want to use `spring-boot-starter-parent` you'll need to configure executions for the `process-test-aot` goal from the Spring Boot plugin and the `test` goal from the Native Build Tools plugin. diff --git a/spring-boot-project/spring-boot-starters/spring-boot-starter-parent/build.gradle b/spring-boot-project/spring-boot-starters/spring-boot-starter-parent/build.gradle index f695fbfd61..1accf86dcf 100644 --- a/spring-boot-project/spring-boot-starters/spring-boot-starter-parent/build.gradle +++ b/spring-boot-project/spring-boot-starters/spring-boot-starter-parent/build.gradle @@ -306,31 +306,33 @@ publishing.publications.withType(MavenPublication) { } } build { - plugins { - plugin { - delegate.groupId('org.springframework.boot') - delegate.artifactId('spring-boot-maven-plugin') - executions { - execution { - delegate.id('process-test-aot') - goals { - delegate.goal('process-test-aot') + pluginManagement { + plugins { + plugin { + delegate.groupId('org.springframework.boot') + delegate.artifactId('spring-boot-maven-plugin') + executions { + execution { + delegate.id('process-test-aot') + goals { + delegate.goal('process-test-aot') + } } } } - } - plugin { - delegate.groupId('org.graalvm.buildtools') - delegate.artifactId('native-maven-plugin') - configuration { - delegate.classesDirectory('${project.build.outputDirectory}') - delegate.requiredVersion('22.3') - } - executions { - execution { - delegate.id('native-test') - goals { - delegate.goal('test') + plugin { + delegate.groupId('org.graalvm.buildtools') + delegate.artifactId('native-maven-plugin') + configuration { + delegate.classesDirectory('${project.build.outputDirectory}') + delegate.requiredVersion('22.3') + } + executions { + execution { + delegate.id('native-test') + goals { + delegate.goal('test') + } } } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/docs/antora/modules/maven-plugin/examples/aot-test/pom.xml b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/docs/antora/modules/maven-plugin/examples/aot-test/pom.xml deleted file mode 100644 index b20f0d0dcc..0000000000 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/docs/antora/modules/maven-plugin/examples/aot-test/pom.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - 4.0.0 - aot - - - - - org.springframework.boot - spring-boot-maven-plugin - - - process-test-aot - - process-test-aot - - - - - - - - - - diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/docs/antora/modules/maven-plugin/pages/aot.adoc b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/docs/antora/modules/maven-plugin/pages/aot.adoc index 442f2877bf..fcbec79dee 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/docs/antora/modules/maven-plugin/pages/aot.adoc +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/docs/antora/modules/maven-plugin/pages/aot.adoc @@ -90,17 +90,33 @@ include::partial$goals/process-aot.adoc[leveloffset=+1] == Processing Tests The AOT engine can be applied to JUnit 5 tests that use Spring's Test Context Framework. -Suitable tests are processed by the AOT engine in order to generate `ApplicationContextInitializer` code. +Those tests are processed by the AOT engine and are then executed in a native image. -To configure your application to use this feature, add an execution for the `process-test-aot` goal, as shown in the following example: +Just like <>, the `spring-boot-starter-parent` defines a `nativeTest` profile that can be used to streamline the steps required to execute your tests in a native image. + +The `nativeTest` profile configures the following: + +* Execution of `process-test-aot` when the Spring Boot Maven Plugin is applied on a project. +* Execution of `test` when the {url-native-build-tools-docs-maven-plugin}[Native Build Tools Maven Plugin] is applied on a project. +The execution defines sensible defaults, in particular: +** Making sure the plugin uses the raw classpath, and not the main jar file as it does not understand our repackaged jar format. +** Validate that a suitable GraalVM version is available. +** Download third-party reachability metadata. + +To benefit from the `nativeTest` profile, a module that represents an application should define two plugins, as shown in the following example: [source,xml,indent=0,subs="verbatim,attributes"] ---- -include::example$aot-test/pom.xml[tags=aot] +include::example$aot-native/pom.xml[tags=aot-native] ---- -TIP: If you are using `spring-boot-starter-parent`, this execution is automatically configured if you enable the `nativeTest` profile. +Once the above is in place for each module that needs this feature, you can build your multi-modules project and execute your tests in a native image in the relevant sub-modules, as shown in the following example: -As with application AOT processing, the `BeanFactory` is fully prepared at build-time. +[source,shell] +---- +$ mvn test -PnativeTest +---- + +NOTE: As with application AOT processing, the `BeanFactory` is fully prepared at build-time. include::partial$goals/process-test-aot.adoc[leveloffset=+1]