From 6501d9f9f2f43747fc89b3a7c741da8e49b1cf20 Mon Sep 17 00:00:00 2001 From: Alexey Tsvetkov <654232+AlexeyTsvetkov@users.noreply.github.com> Date: Mon, 31 Oct 2022 18:13:08 +0100 Subject: [PATCH] Fix packaging with JDK 19 (#2439) Resolves #2328 --- .../application/tasks/AbstractJPackageTask.kt | 7 ++-- .../integration/DesktopApplicationTest.kt | 20 +++++++++++ .../compose/test/utils/TestProject.kt | 34 ++++++++++++------- .../compose/test/utils/TestProjects.kt | 1 + .../application/customJavaHome/build.gradle | 34 +++++++++++++++++++ .../customJavaHome/settings.gradle | 11 ++++++ .../customJavaHome/src/main/kotlin/main.kt | 18 ++++++++++ 7 files changed, 111 insertions(+), 14 deletions(-) create mode 100644 gradle-plugins/compose/src/test/test-projects/application/customJavaHome/build.gradle create mode 100644 gradle-plugins/compose/src/test/test-projects/application/customJavaHome/settings.gradle create mode 100644 gradle-plugins/compose/src/test/test-projects/application/customJavaHome/src/main/kotlin/main.kt diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/AbstractJPackageTask.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/AbstractJPackageTask.kt index 764570e6d1..e0a4b796a8 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/AbstractJPackageTask.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/AbstractJPackageTask.kt @@ -231,6 +231,8 @@ abstract class AbstractJPackageTask @Inject constructor( @get:Optional val javaRuntimePropertiesFile: RegularFileProperty = objects.fileProperty() + private lateinit var jvmRuntimeInfo: JvmRuntimeProperties + @get:Optional @get:Nested internal var nonValidatedMacSigningSettings: MacOSSigningSettings? = null @@ -339,7 +341,7 @@ abstract class AbstractJPackageTask @Inject constructor( if (targetFormat != TargetFormat.AppImage) { // Args, that can only be used, when creating an installer - if (currentOS == OS.MacOS && macAppStore.orNull == true) { + if (currentOS == OS.MacOS && jvmRuntimeInfo.majorVersion >= 18) { // This is needed to prevent a directory does not exist error. cliArg("--app-image", appImage.dir("${packageName.get()}.app")) } else { @@ -444,7 +446,6 @@ abstract class AbstractJPackageTask @Inject constructor( fileOperations.delete(tmpDirForSign) tmpDirForSign.mkdirs() - val jvmRuntimeInfo = JvmRuntimeProperties.readFromFile(javaRuntimePropertiesFile.ioFile) MacJarSignFileCopyingProcessor( signer, tmpDirForSign, @@ -543,6 +544,8 @@ abstract class AbstractJPackageTask @Inject constructor( } override fun initState() { + jvmRuntimeInfo = JvmRuntimeProperties.readFromFile(javaRuntimePropertiesFile.ioFile) + val mappingFile = libsMappingFile.ioFile if (mappingFile.exists()) { try { diff --git a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/DesktopApplicationTest.kt b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/DesktopApplicationTest.kt index 152dfb6ea0..4d396344e1 100644 --- a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/DesktopApplicationTest.kt +++ b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/DesktopApplicationTest.kt @@ -127,6 +127,26 @@ class DesktopApplicationTest : GradlePluginTestBase() { testPackageJvmDistributions() } + private fun customJavaHomeProject(javaVersion: Int): TestProject = + testProject( + TestProjects.customJavaHome, + defaultTestEnvironment.copy(customJavaToolchainVersion = javaVersion) + ) + + @Test + fun testJdk15() = with(customJavaHomeProject(15)) { + testPackageJvmDistributions() + } + @Test + fun testJdk18() = with(customJavaHomeProject(18)) { + testPackageJvmDistributions() + } + + @Test + fun testJdk19() = with(customJavaHomeProject(19)) { + testPackageJvmDistributions() + } + private fun TestProject.testPackageJvmDistributions() { val result = gradle(":packageDistributionForCurrentOS").build() val ext = when (currentOS) { diff --git a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/utils/TestProject.kt b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/utils/TestProject.kt index f02c612027..3aec18114e 100644 --- a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/utils/TestProject.kt +++ b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/utils/TestProject.kt @@ -13,8 +13,26 @@ data class TestEnvironment( val workingDir: File, val kotlinVersion: String = TestKotlinVersions.Default, val composeGradlePluginVersion: String = TestProperties.composeGradlePluginVersion, - val composeCompilerArtifact: String? = null -) + val composeCompilerArtifact: String? = null, + val customJavaToolchainVersion: Int? = null +) { + private val placeholders = linkedMapOf( + "COMPOSE_GRADLE_PLUGIN_VERSION_PLACEHOLDER" to composeGradlePluginVersion, + "KOTLIN_VERSION_PLACEHOLDER" to kotlinVersion, + "COMPOSE_COMPILER_ARTIFACT_PLACEHOLDER" to composeCompilerArtifact, + "CUSTOM_JAVA_TOOLCHAIN_VERSION_PLACEHOLDER" to customJavaToolchainVersion?.toString() + ) + + fun replacePlaceholdersInFile(file: File) { + var content = file.readText() + for ((placeholder, value) in placeholders.entries) { + if (value != null) { + content = content.replace(placeholder, value) + } + } + file.writeText(content) + } +} class TestProject( private val name: String, @@ -36,18 +54,10 @@ class TestProject( val target = testEnvironment.workingDir.resolve(orig.relativeTo(originalTestRoot)) target.parentFile.mkdirs() + orig.copyTo(target) if (orig.name.endsWith(".gradle") || orig.name.endsWith(".gradle.kts")) { - val origContent = orig.readText() - var newContent = origContent - .replace("COMPOSE_GRADLE_PLUGIN_VERSION_PLACEHOLDER", testEnvironment.composeGradlePluginVersion) - .replace("KOTLIN_VERSION_PLACEHOLDER", testEnvironment.kotlinVersion) - if (testEnvironment.composeCompilerArtifact != null) { - newContent = newContent.replace("COMPOSE_COMPILER_ARTIFACT_PLACEHOLDER", testEnvironment.composeCompilerArtifact) - } - target.writeText(newContent) - } else { - orig.copyTo(target) + testEnvironment.replacePlaceholdersInFile(target) } } } diff --git a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/utils/TestProjects.kt b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/utils/TestProjects.kt index 1a6863e839..30e0226a82 100644 --- a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/utils/TestProjects.kt +++ b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/utils/TestProjects.kt @@ -20,6 +20,7 @@ object TestProjects { const val defaultArgsOverride = "application/defaultArgsOverride" const val unpackSkiko = "application/unpackSkiko" const val resources = "application/resources" + const val customJavaHome = "application/customJavaHome" const val jsMpp = "misc/jsMpp" const val jvmPreview = "misc/jvmPreview" } \ No newline at end of file diff --git a/gradle-plugins/compose/src/test/test-projects/application/customJavaHome/build.gradle b/gradle-plugins/compose/src/test/test-projects/application/customJavaHome/build.gradle new file mode 100644 index 0000000000..c12de0415e --- /dev/null +++ b/gradle-plugins/compose/src/test/test-projects/application/customJavaHome/build.gradle @@ -0,0 +1,34 @@ +import org.jetbrains.compose.desktop.application.dsl.TargetFormat + +plugins { + id "org.jetbrains.kotlin.jvm" + id "org.jetbrains.compose" +} + +repositories { + jetbrainsCompose() +} + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib" + implementation compose.desktop.currentOs +} + +compose.desktop { + application { + mainClass = "MainKt" + nativeDistributions { + targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb) + + packageVersion = "1.0.0" + packageName = "TestPackage" + description = "Test description" + copyright = "Test Copyright Holder" + vendor = "Test Vendor" + + javaHome = javaToolchains.launcherFor { + languageVersion.set(JavaLanguageVersion.of(CUSTOM_JAVA_TOOLCHAIN_VERSION_PLACEHOLDER)) + }.get().metadata.installationPath.asFile.absolutePath + } + } +} diff --git a/gradle-plugins/compose/src/test/test-projects/application/customJavaHome/settings.gradle b/gradle-plugins/compose/src/test/test-projects/application/customJavaHome/settings.gradle new file mode 100644 index 0000000000..43d540efb8 --- /dev/null +++ b/gradle-plugins/compose/src/test/test-projects/application/customJavaHome/settings.gradle @@ -0,0 +1,11 @@ +pluginManagement { + plugins { + id 'org.jetbrains.kotlin.jvm' version 'KOTLIN_VERSION_PLACEHOLDER' + id 'org.jetbrains.compose' version 'COMPOSE_GRADLE_PLUGIN_VERSION_PLACEHOLDER' + } + repositories { + mavenLocal() + gradlePluginPortal() + } +} +rootProject.name = "simple" \ No newline at end of file diff --git a/gradle-plugins/compose/src/test/test-projects/application/customJavaHome/src/main/kotlin/main.kt b/gradle-plugins/compose/src/test/test-projects/application/customJavaHome/src/main/kotlin/main.kt new file mode 100644 index 0000000000..6b688cf95e --- /dev/null +++ b/gradle-plugins/compose/src/test/test-projects/application/customJavaHome/src/main/kotlin/main.kt @@ -0,0 +1,18 @@ +import androidx.compose.material.Text +import androidx.compose.material.Button +import androidx.compose.material.MaterialTheme +import androidx.compose.ui.window.singleWindowApplication +import androidx.compose.runtime.* + +fun main() = singleWindowApplication { + val scope = rememberCoroutineScope() + var text by remember { mutableStateOf("Hello, World!") } + + MaterialTheme { + Button(onClick = { + text = "Hello, Desktop!" + }) { + Text(text) + } + } +} \ No newline at end of file