Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ internal fun JvmApplicationContext.configureNativeImage() {
// native-image itself.
val checkNoAwt = tasks.register<CheckNativeImageNoAwtTask>(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 ->
Expand All @@ -78,18 +83,22 @@ 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(
File(home, "bin/java").absolutePath,
"-agentlib:native-image-agent=config-merge-dir=${output.absolutePath}",
"-cp", runtime.files.joinToString(File.pathSeparator),
main,
) + app.args
) + appArgs.get()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -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<ApplicationWindowing>

@TaskAction
fun check() {
val file = executable.get().asFile
Expand Down
Original file line number Diff line number Diff line change
@@ -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.")
}
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fun main() {
println("native image configuration cache test")
}
Loading