From 01fe61f9c7ab3ab6d879c05dacd31782a9e0fda7 Mon Sep 17 00:00:00 2001 From: Alexey Tsvetkov Date: Fri, 30 Oct 2020 15:58:39 +0300 Subject: [PATCH] Include main jar into runtime classpath for Kotlin MPP target --- .../desktop/application/ApplicationPlugin.kt | 38 +++++-------------- .../desktop/application/dsl/Application.kt | 4 +- .../application/dsl/ConfigurationSource.kt | 31 +++++++++++++-- 3 files changed, 39 insertions(+), 34 deletions(-) diff --git a/gradle-plugins/compose-desktop-application/src/main/kotlin/org/jetbrains/compose/desktop/application/ApplicationPlugin.kt b/gradle-plugins/compose-desktop-application/src/main/kotlin/org/jetbrains/compose/desktop/application/ApplicationPlugin.kt index ad96c44398..43e226f4bf 100644 --- a/gradle-plugins/compose-desktop-application/src/main/kotlin/org/jetbrains/compose/desktop/application/ApplicationPlugin.kt +++ b/gradle-plugins/compose-desktop-application/src/main/kotlin/org/jetbrains/compose/desktop/application/ApplicationPlugin.kt @@ -148,21 +148,13 @@ internal fun AbstractJPackageTask.configurePackagingTask(app: Application) { launcherMainJar.set(app.mainJar.orNull) app._fromFiles.forEach { files.from(it) } dependsOn(*app._dependenciesTaskNames.toTypedArray()) - when (val configSource = app._configurationSource) { - is ConfigurationSource.None -> {} - is ConfigurationSource.GradleSourceSet -> { - val sourceSet = configSource.sourceSet - dependsOn(sourceSet.jarTaskName) - launcherMainJar.set(app.mainJar.orElse(jarFromJarTaskByName(sourceSet.jarTaskName))) - files.from(sourceSet.runtimeClasspath) - } - is ConfigurationSource.KotlinMppTarget -> { - val target = configSource.target - dependsOn(target.artifactsTaskName) - launcherMainJar.set(app.mainJar.orElse(jarFromJarTaskByName(target.artifactsTaskName))) - files.from(target.runtimeFiles()) - } + + app._configurationSource?.let { configSource -> + dependsOn(configSource.jarTaskName) + files.from(configSource.runtimeClasspath) + launcherMainJar.set(app.mainJar.orElse(configSource.jarTask(project).flatMap { it.archiveFile })) } + modules.set(provider { app.nativeDistributions.modules }) launcherMainClass.set(provider { app.mainClass }) launcherJvmArgs.set(provider { app.jvmArgs }) @@ -184,27 +176,15 @@ private fun Project.configureRunTask(app: Application) { cp.from(app._fromFiles) dependsOn(*app._dependenciesTaskNames.toTypedArray()) - when (val configSource = app._configurationSource) { - is ConfigurationSource.None -> {} - is ConfigurationSource.GradleSourceSet -> { - val sourceSet = configSource.sourceSet - dependsOn(sourceSet.jarTaskName) - cp.from(sourceSet.runtimeClasspath) - } - is ConfigurationSource.KotlinMppTarget -> { - val target = configSource.target - dependsOn(target.artifactsTaskName) - cp.from(target.runtimeFiles()) - } + app._configurationSource?.let { configSource -> + dependsOn(configSource.jarTaskName) + cp.from(configSource.runtimeClasspath) } classpath = cp } } -private fun KotlinTarget.runtimeFiles(): FileCollection = - (compilations.getByName("main") as KotlinCompilationToRunnableFiles).runtimeDependencyFiles - private fun Application.javaHomeOrDefault(): String = javaHome ?: System.getProperty("java.home") diff --git a/gradle-plugins/compose-desktop-application/src/main/kotlin/org/jetbrains/compose/desktop/application/dsl/Application.kt b/gradle-plugins/compose-desktop-application/src/main/kotlin/org/jetbrains/compose/desktop/application/dsl/Application.kt index 3684a4e5b8..874aa2faf7 100644 --- a/gradle-plugins/compose-desktop-application/src/main/kotlin/org/jetbrains/compose/desktop/application/dsl/Application.kt +++ b/gradle-plugins/compose-desktop-application/src/main/kotlin/org/jetbrains/compose/desktop/application/dsl/Application.kt @@ -15,7 +15,7 @@ open class Application @Inject constructor( val name: String, objects: ObjectFactory ) { - internal var _configurationSource: ConfigurationSource = ConfigurationSource.None + internal var _configurationSource: ConfigurationSource? = null internal val _fromFiles = objects.fileCollection() internal val _dependenciesTaskNames = ArrayList() @@ -28,7 +28,7 @@ open class Application @Inject constructor( _configurationSource = ConfigurationSource.KotlinMppTarget(from) } fun disableDefaultConfiguration() { - _configurationSource = ConfigurationSource.None + _configurationSource = null } fun fromFiles(vararg files: Any) { diff --git a/gradle-plugins/compose-desktop-application/src/main/kotlin/org/jetbrains/compose/desktop/application/dsl/ConfigurationSource.kt b/gradle-plugins/compose-desktop-application/src/main/kotlin/org/jetbrains/compose/desktop/application/dsl/ConfigurationSource.kt index 45fe29c90b..46951602c8 100644 --- a/gradle-plugins/compose-desktop-application/src/main/kotlin/org/jetbrains/compose/desktop/application/dsl/ConfigurationSource.kt +++ b/gradle-plugins/compose-desktop-application/src/main/kotlin/org/jetbrains/compose/desktop/application/dsl/ConfigurationSource.kt @@ -1,10 +1,35 @@ package org.jetbrains.compose.desktop.application.dsl +import org.gradle.api.Project +import org.gradle.api.file.FileCollection import org.gradle.api.tasks.SourceSet +import org.gradle.api.tasks.TaskProvider +import org.gradle.jvm.tasks.Jar import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget internal sealed class ConfigurationSource { - object None : ConfigurationSource() - class GradleSourceSet(val sourceSet: SourceSet) : ConfigurationSource() - class KotlinMppTarget(val target: KotlinJvmTarget) : ConfigurationSource() + abstract val jarTaskName: String + abstract val runtimeClasspath: FileCollection + fun jarTask(project: Project): TaskProvider = + project.tasks.named(jarTaskName, Jar::class.java) + + class GradleSourceSet(val sourceSet: SourceSet) : ConfigurationSource() { + override val jarTaskName: String + get() = sourceSet.jarTaskName + + override val runtimeClasspath: FileCollection + get() = sourceSet.runtimeClasspath + + } + + class KotlinMppTarget(val target: KotlinJvmTarget) : ConfigurationSource() { + override val jarTaskName: String + get() = target.artifactsTaskName + + override val runtimeClasspath: FileCollection + get() = target.project.objects.fileCollection().apply { + from(jarTask(target.project).flatMap { it.archiveFile }) + from(target.compilations.getByName("main").runtimeDependencyFiles) + } + } } \ No newline at end of file