From 53a87b8bf047e8efee609ab0dd0cddf3277be739 Mon Sep 17 00:00:00 2001 From: Cedric Champeau Date: Wed, 17 May 2023 16:39:12 +0200 Subject: [PATCH] Use an automatic dependency for graal This commit changes how the `micronaut-graal` dependency is added in order to use the `AutomaticDependency` mechanism. This makes it possible to override the version of the Micronaut Graal dependency by changing the `coreVersion` in the DSL. --- .../graalvm/MicronautGraalPluginSpec.groovy | 2 ++ .../gradle/graalvm/MicronautGraalPlugin.java | 11 +++++---- .../gradle/NativeImageTaskSpec.groovy | 4 +++- .../gradle/MicronautKotlinSupport.java | 23 ++++++++++--------- .../gradle/AbstractGradleBuildSpec.groovy | 8 +++++++ 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/functional-tests/src/test/groovy/io/micronaut/gradle/graalvm/MicronautGraalPluginSpec.groovy b/functional-tests/src/test/groovy/io/micronaut/gradle/graalvm/MicronautGraalPluginSpec.groovy index 89e8ddbb..82de289c 100644 --- a/functional-tests/src/test/groovy/io/micronaut/gradle/graalvm/MicronautGraalPluginSpec.groovy +++ b/functional-tests/src/test/groovy/io/micronaut/gradle/graalvm/MicronautGraalPluginSpec.groovy @@ -54,6 +54,7 @@ class MicronautGraalPluginSpec extends AbstractEagerConfiguringFunctionalTest { void 'native-image is called with the generated JSON file directory (Micronaut Application)'() { given: withSwaggerMicronautApplication() + withNativeImageDryRun() when: def result = build('nativeCompile', '-i', '--stacktrace') @@ -76,6 +77,7 @@ class MicronautGraalPluginSpec extends AbstractEagerConfiguringFunctionalTest { void 'native-image is called with the generated JSON file directory (regular Application)'() { given: withSwaggerApplication() + withNativeImageDryRun() when: def result = build('nativeCompile', '-i', '--stacktrace') diff --git a/graalvm-plugin/src/main/java/io/micronaut/gradle/graalvm/MicronautGraalPlugin.java b/graalvm-plugin/src/main/java/io/micronaut/gradle/graalvm/MicronautGraalPlugin.java index 2d3598e7..1ec5918e 100644 --- a/graalvm-plugin/src/main/java/io/micronaut/gradle/graalvm/MicronautGraalPlugin.java +++ b/graalvm-plugin/src/main/java/io/micronaut/gradle/graalvm/MicronautGraalPlugin.java @@ -3,6 +3,7 @@ import io.micronaut.gradle.MicronautExtension; import io.micronaut.gradle.MicronautRuntime; import io.micronaut.gradle.PluginsHelper; +import io.micronaut.gradle.internal.AutomaticDependency; import org.graalvm.buildtools.gradle.NativeImagePlugin; import org.graalvm.buildtools.gradle.dsl.GraalVMExtension; import org.graalvm.buildtools.gradle.dsl.GraalVMReachabilityMetadataRepositoryExtension; @@ -24,8 +25,11 @@ import java.util.HashSet; import java.util.List; import java.util.Objects; +import java.util.Optional; import java.util.Set; +import static io.micronaut.gradle.PluginsHelper.CORE_VERSION_PROPERTY; + /** * Support for building GraalVM native images. * @@ -127,10 +131,9 @@ private static void configureAnnotationProcessing(Project project, MicronautExte private static void addGraalVMAnnotationProcessorDependency(Project project, Iterable sourceSets) { for (SourceSet sourceSet : sourceSets) { - project.getDependencies().add( - sourceSet.getAnnotationProcessorConfigurationName(), - "io.micronaut:micronaut-graal" - ); + new AutomaticDependency(sourceSet.getAnnotationProcessorConfigurationName(), + "io.micronaut:micronaut-graal", + Optional.of(CORE_VERSION_PROPERTY)).applyTo(project); } } diff --git a/graalvm-plugin/src/test/groovy/io/micronaut/gradle/NativeImageTaskSpec.groovy b/graalvm-plugin/src/test/groovy/io/micronaut/gradle/NativeImageTaskSpec.groovy index b67ab0f2..38c23ef3 100644 --- a/graalvm-plugin/src/test/groovy/io/micronaut/gradle/NativeImageTaskSpec.groovy +++ b/graalvm-plugin/src/test/groovy/io/micronaut/gradle/NativeImageTaskSpec.groovy @@ -29,6 +29,8 @@ class NativeImageTaskSpec extends AbstractGradleBuildSpec { mainClassName="example.Application" """ + withNativeImageDryRun() + testProjectDir.newFolder("src", "main", "java", "example") def javaFile = testProjectDir.newFile("src/main/java/example/Application.java") javaFile.parentFile.mkdirs() @@ -82,6 +84,7 @@ class Application { } } """ + withNativeImageDryRun() testProjectDir.newFolder("src", "main", "java", "example") def javaFile = testProjectDir.newFile("src/main/java/example/Application.java") javaFile.parentFile.mkdirs() @@ -103,7 +106,6 @@ class Application { def task = result.task(":nativeCompile") then: result.output.contains("Native Image written to") - result.output.contains("Generating 'basic-app'") argFileContentsOf(result).contains('-Dfoo=bar') task.outcome == TaskOutcome.SUCCESS } diff --git a/minimal-plugin/src/main/java/io/micronaut/gradle/MicronautKotlinSupport.java b/minimal-plugin/src/main/java/io/micronaut/gradle/MicronautKotlinSupport.java index a8a41de0..238aacdd 100644 --- a/minimal-plugin/src/main/java/io/micronaut/gradle/MicronautKotlinSupport.java +++ b/minimal-plugin/src/main/java/io/micronaut/gradle/MicronautKotlinSupport.java @@ -2,6 +2,7 @@ import com.google.devtools.ksp.gradle.KspExtension; import io.micronaut.gradle.graalvm.GraalUtil; +import io.micronaut.gradle.internal.AutomaticDependency; import org.gradle.api.Project; import org.gradle.api.artifacts.Configuration; import org.gradle.api.artifacts.Dependency; @@ -20,10 +21,12 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.function.Consumer; -import static io.micronaut.gradle.PluginsHelper.resolveMicronautPlatform; +import static io.micronaut.gradle.PluginsHelper.CORE_VERSION_PROPERTY; import static io.micronaut.gradle.PluginsHelper.configureAnnotationProcessors; +import static io.micronaut.gradle.PluginsHelper.resolveMicronautPlatform; /** * Extension to integration support for Kotlin. @@ -141,7 +144,7 @@ private static void configureKotlinCompilerPlugin(Project project, String[] comp // add inject-java to kapt scopes DependencyHandler dependencies = project.getDependencies(); PluginsHelper.registerAnnotationProcessors(project, annotationProcessorModules, compilerConfigurations); - addGraalVmDependencies(compilerConfigurations, dependencies); + addGraalVmDependencies(compilerConfigurations, project); Configuration kotlinProcessors = project.getConfigurations().getByName(KOTLIN_PROCESSORS); for (String compilerConfiguration : compilerConfigurations) { @@ -197,20 +200,18 @@ private static void configureAdditionalSourceSet(String compilerType, Dependency implementationConfigurationName, annotationProcessorConfigurationName); if (GraalUtil.isGraalJVM()) { - dependencies.add( - annotationProcessorConfigurationName, - "io.micronaut:micronaut-graal" - ); + new AutomaticDependency(annotationProcessorConfigurationName, + "io.micronaut:micronaut-graal", + Optional.of(CORE_VERSION_PROPERTY)).applyTo(p); } } - private static void addGraalVmDependencies(String[] compilerConfigurations, DependencyHandler dependencies) { + private static void addGraalVmDependencies(String[] compilerConfigurations, Project project) { if (GraalUtil.isGraalJVM()) { for (String configuration : compilerConfigurations) { - dependencies.add( - configuration, - "io.micronaut:micronaut-graal" - ); + new AutomaticDependency(configuration, + "io.micronaut:micronaut-graal", + Optional.of(CORE_VERSION_PROPERTY)).applyTo(project); } } } diff --git a/minimal-plugin/src/testFixtures/groovy/io/micronaut/gradle/AbstractGradleBuildSpec.groovy b/minimal-plugin/src/testFixtures/groovy/io/micronaut/gradle/AbstractGradleBuildSpec.groovy index a95c2e18..5d0d905c 100644 --- a/minimal-plugin/src/testFixtures/groovy/io/micronaut/gradle/AbstractGradleBuildSpec.groovy +++ b/minimal-plugin/src/testFixtures/groovy/io/micronaut/gradle/AbstractGradleBuildSpec.groovy @@ -144,6 +144,14 @@ abstract class AbstractGradleBuildSpec extends Specification { } } + void withNativeImageDryRun() { + buildFile << """ + graalvmNative.binaries.all { + buildArgs.add("--dry-run") + } + """ + } + private void prepareBuild() { if (postSettingsStatements) { postSettingsStatements.each {