Skip to content
Draft
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 @@ -209,6 +209,38 @@ class KotlinPluginsTest : BasePluginTest() {
}
}

@Test
fun compatKmpApplicationDsl() {
writeClass(sourceSet = "jvmMain", withImports = true, jvmLang = JvmLang.Kotlin)
projectScriptPath.appendText(
"""
kotlin {
jvm {
binaries {
executable {
it.mainClass.set('my.Main')
}
}
}
sourceSets {
jvmMain {
dependencies {
implementation 'junit:junit:3.8.2'
}
}
}
}
""".trimIndent(),
)

val result = run(runShadowTask)

assertThat(result.output).contains(
"Hello, World! (foo) from Main",
"Refs: junit.framework.Test",
)
}

private fun compileOnlyStdlib(exclude: Boolean): String {
return if (exclude) {
// Disable the stdlib dependency added via `implementation`.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.github.jengelman.gradle.plugins.shadow

import com.github.jengelman.gradle.plugins.shadow.ShadowApplicationPlugin.Companion.SHADOW_RUN_TASK_NAME
import com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin.Companion.registerShadowJarCommon
import com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin.Companion.shadowJar
import com.github.jengelman.gradle.plugins.shadow.internal.javaPluginExtension
import com.github.jengelman.gradle.plugins.shadow.internal.javaToolchainService
import com.github.jengelman.gradle.plugins.shadow.internal.mainClassAttributeKey
import kotlin.collections.contains
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.ApplicationPlugin
import org.gradle.api.tasks.JavaExec
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion as KgpVersion
Expand All @@ -18,6 +24,7 @@ public abstract class ShadowKmpPlugin : Plugin<Project> {
if (target !is KotlinJvmTarget) return@configureEach

configureShadowJar(target)
addRunTask(target)
}
}
}
Expand Down Expand Up @@ -48,4 +55,26 @@ public abstract class ShadowKmpPlugin : Plugin<Project> {
}
}
}

private fun Project.addRunTask(target: KotlinJvmTarget) {
// if (KgpVersion.DEFAULT < KgpVersion.fromVersion("2.1.20")) return@configureEach

tasks.register(SHADOW_RUN_TASK_NAME, JavaExec::class.java) { task ->
task.description = "Runs this project as a JVM application using the shadow jar"
task.group = ApplicationPlugin.APPLICATION_GROUP

task.classpath = files(tasks.shadowJar)

target.binaries {
executable { dsl ->
Comment on lines +68 to +69
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CC @osipxd

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created an issue to address this:
KT-77499 Provide a way to access and configure JVM binaries defined by users

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this feedback!

task.mainModule.set(dsl.mainModule)
task.mainClass.set(dsl.mainClass)
task.jvmArguments.convention(dsl.applicationDefaultJvmArgs)
}
}

task.modularity.inferModulePath.convention(javaPluginExtension.modularity.inferModulePath)
task.javaLauncher.convention(javaToolchainService.launcherFor(javaPluginExtension.toolchain))
}
}
}
Loading