diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/internal/configureNativeImage.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/internal/configureNativeImage.kt index 77c06f772b..d5980905cc 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/internal/configureNativeImage.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/internal/configureNativeImage.kt @@ -59,7 +59,12 @@ internal fun JvmApplicationContext.configureNativeImage() { // native-image itself. val checkNoAwt = tasks.register(taskNameAction = "check", taskNameObject = "nativeImageNoAwt") { description = "Fails when the native image executable still refers to AWT." - onlyIf { app.windowing.get() == ApplicationWindowing.AwtFree } + // The spec reads the task's own input, not the application: whatever a lambda here + // holds is stored in the configuration cache, and the application holds its Kotlin + // target, which cannot be stored ("Cannot run Project.afterEvaluate(Action) when the + // project is already evaluated"). + windowing.set(app.windowing) + onlyIf { task -> (task as CheckNativeImageNoAwtTask).windowing.get() == ApplicationWindowing.AwtFree } executable.set( packageTask.flatMap { image -> image.imageName.zip(image.destinationDir) { name, dir -> @@ -78,10 +83,14 @@ internal fun JvmApplicationContext.configureNativeImage() { description = "Runs the application with GraalVM's tracing agent to collect reachability metadata." val runtime = project.objects.fileCollection() useAppRuntimeFiles { (runtimeJars, _) -> runtime.from(runtimeJars) } + // Providers rather than the application, for the same reason as the check above: the + // action is stored in the configuration cache with everything it refers to. + val mainClass = project.provider { app.mainClass } + val appArgs = project.provider { app.args.toList() } doFirst { val home = graalvmHome.orNull ?: throw GradleException("Set nativeImage.graalvmHome or GRAALVM_HOME to a GraalVM.") - val main = app.mainClass ?: throw GradleException("compose.desktop.application.mainClass is not set.") + val main = mainClass.orNull ?: throw GradleException("compose.desktop.application.mainClass is not set.") val output = metadataDirectory.get().asFile.apply { mkdirs() } commandLine( listOf( @@ -89,7 +98,7 @@ internal fun JvmApplicationContext.configureNativeImage() { "-agentlib:native-image-agent=config-merge-dir=${output.absolutePath}", "-cp", runtime.files.joinToString(File.pathSeparator), main, - ) + app.args + ) + appArgs.get() ) } } diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/CheckNativeImageNoAwtTask.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/CheckNativeImageNoAwtTask.kt index ae979dd5ae..2be76e4ffe 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/CheckNativeImageNoAwtTask.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/CheckNativeImageNoAwtTask.kt @@ -8,11 +8,14 @@ package org.jetbrains.compose.desktop.application.tasks import org.gradle.api.DefaultTask import org.gradle.api.GradleException import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.TaskAction import org.gradle.work.DisableCachingByDefault +import org.jetbrains.compose.desktop.application.dsl.ApplicationWindowing import org.jetbrains.compose.desktop.application.internal.NativeImageNoAwt /** @@ -28,6 +31,10 @@ abstract class CheckNativeImageNoAwtTask : DefaultTask() { @get:PathSensitive(PathSensitivity.NONE) abstract val executable: RegularFileProperty + /** The application's windowing; the check runs only for [ApplicationWindowing.AwtFree]. */ + @get:Input + abstract val windowing: Property + @TaskAction fun check() { val file = executable.get().asFile diff --git a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/NativeImageConfigurationCacheTest.kt b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/NativeImageConfigurationCacheTest.kt new file mode 100644 index 0000000000..cccea3eb5b --- /dev/null +++ b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/NativeImageConfigurationCacheTest.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2026 thisisthepy and respective authors and developers. + * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. + */ + +package org.jetbrains.compose.test.tests.integration + +import org.jetbrains.compose.test.utils.GradlePluginTestBase +import org.jetbrains.compose.test.utils.checks +import org.junit.jupiter.api.Test + +/** + * The native image tasks can be stored in and reused from the configuration cache. + * + * A dry run stores the task graph like a real one, so nothing is compiled and no GraalVM is + * needed. Storing failed with "Cannot run Project.afterEvaluate(Action) when the project is + * already evaluated" while checkNativeImageNoAwt's onlyIf held the application's context, and + * through it a KotlinJvmTarget (plugin 1.11.1-ext.3, found by Gemstone). + */ +class NativeImageConfigurationCacheTest : GradlePluginTestBase() { + private fun project() = testProject( + "application/nativeImageConfigurationCache", + defaultTestEnvironment.copy(useGradleConfigurationCache = true) + ) + + @Test + fun packageNativeImageIsStoredAndReused() = with(project()) { + gradle("packageNativeImage", "--dry-run").checks { + check.logContains("Configuration cache entry stored.") + } + gradle("packageNativeImage", "--dry-run").checks { + check.logContains("Reusing configuration cache.") + } + } + + @Test + fun runNativeImageAgentIsStoredAndReused() = with(project()) { + gradle("runNativeImageAgent", "--dry-run").checks { + check.logContains("Configuration cache entry stored.") + } + gradle("runNativeImageAgent", "--dry-run").checks { + check.logContains("Reusing configuration cache.") + } + } +} diff --git a/gradle-plugins/compose/src/test/test-projects/application/nativeImageConfigurationCache/build.gradle b/gradle-plugins/compose/src/test/test-projects/application/nativeImageConfigurationCache/build.gradle new file mode 100644 index 0000000000..402d8c3a8f --- /dev/null +++ b/gradle-plugins/compose/src/test/test-projects/application/nativeImageConfigurationCache/build.gradle @@ -0,0 +1,29 @@ +import org.jetbrains.compose.desktop.application.dsl.ApplicationOutput +import org.jetbrains.kotlin.gradle.dsl.JvmTarget +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + +plugins { + id "org.jetbrains.kotlin.jvm" + id "org.jetbrains.kotlin.plugin.compose" + id "org.thisisthepy.compose" +} + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib" +} + +tasks.withType(KotlinCompile).configureEach { + compilerOptions { + jvmTarget.set(JvmTarget.JVM_11) + } +} + +compose.desktop { + application { + mainClass = "MainKt" + output = ApplicationOutput.NativeImage + nativeImage { + graalvmHome = "/no/graalvm/here" + } + } +} diff --git a/gradle-plugins/compose/src/test/test-projects/application/nativeImageConfigurationCache/gradle.properties b/gradle-plugins/compose/src/test/test-projects/application/nativeImageConfigurationCache/gradle.properties new file mode 100644 index 0000000000..9e160e5f0d --- /dev/null +++ b/gradle-plugins/compose/src/test/test-projects/application/nativeImageConfigurationCache/gradle.properties @@ -0,0 +1,4 @@ +# Ignore JVMs installed on the machine when resolving toolchains. +# This is needed to ensure that we get JBRSDK and not JBR (which lacks +# jpackage, jlink) when requesting KnownJvmVendor.JETBRAINS +org.gradle.java.installations.auto-detect=false \ No newline at end of file diff --git a/gradle-plugins/compose/src/test/test-projects/application/nativeImageConfigurationCache/settings.gradle b/gradle-plugins/compose/src/test/test-projects/application/nativeImageConfigurationCache/settings.gradle new file mode 100644 index 0000000000..9712753bdb --- /dev/null +++ b/gradle-plugins/compose/src/test/test-projects/application/nativeImageConfigurationCache/settings.gradle @@ -0,0 +1,30 @@ +pluginManagement { + plugins { + id 'org.jetbrains.kotlin.jvm' version 'KOTLIN_VERSION_PLACEHOLDER' + id 'org.jetbrains.kotlin.plugin.compose' version 'KOTLIN_VERSION_PLACEHOLDER' + id 'org.thisisthepy.compose' version 'COMPOSE_GRADLE_PLUGIN_VERSION_PLACEHOLDER' + } + repositories { + mavenLocal() + gradlePluginPortal() + mavenCentral() + google() + maven { + url 'https://packages.jetbrains.team/maven/p/cmp/dev' + } + } +} +plugins { + id 'org.gradle.toolchains.foojay-resolver-convention' version '1.0.0' +} +dependencyResolutionManagement { + repositories { + mavenCentral() + google() + maven { + url 'https://packages.jetbrains.team/maven/p/cmp/dev' + } + mavenLocal() + } +} +rootProject.name = "native-image-cc" \ No newline at end of file diff --git a/gradle-plugins/compose/src/test/test-projects/application/nativeImageConfigurationCache/src/main/kotlin/main.kt b/gradle-plugins/compose/src/test/test-projects/application/nativeImageConfigurationCache/src/main/kotlin/main.kt new file mode 100644 index 0000000000..3cc41c276b --- /dev/null +++ b/gradle-plugins/compose/src/test/test-projects/application/nativeImageConfigurationCache/src/main/kotlin/main.kt @@ -0,0 +1,3 @@ +fun main() { + println("native image configuration cache test") +}