Skip to content

Commit 92340ef

Browse files
committed
chore: fix shadow plugin to work with kotlin 2.1.20-RC multiplatform plugin
1 parent 93c6b81 commit 92340ef

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,16 @@ For testing, a separate [sandbox project](/sandbox) is available with the plugin
7272
`settings.gradle.kts`. First publish the plugin to the local maven repository and then run the sandbox project.
7373

7474
```bash
75+
# Publish the plugins to maven local
7576
$ ./gradlew publishToMavenLocal
76-
$ ./gradlew -p sandbox :build
77+
78+
# Build the sandbox app using published plugin
79+
$ ./gradlew -p sandbox :build -PjvmArgs=
80+
81+
# Run the sandbox app
82+
$ sandbox/build/libs/sandbox-app
83+
84+
# Run other plugin tasks
7785
$ ./gradlew -p sandbox :dependencyUpdates --no-configuration-cache
7886
```
7987

plugins/project/src/main/kotlin/common/Multiplatform.kt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fun KotlinMultiplatformExtension.jvmTarget(project: Project) =
102102
// val test by testRuns.existing
103103
testRuns.configureEach { executionTask.configure { configureJavaTest() } }
104104

105-
// Configure application executable only it's enabled
105+
// Configures JavaExec task with name "runJvm" and Gradle distribution "jvmDistZip"
106106
if (isKmpExecEnabled) {
107107
binaries {
108108
executable {
@@ -113,14 +113,6 @@ fun KotlinMultiplatformExtension.jvmTarget(project: Project) =
113113
}
114114
}
115115

116-
// Register a task to execute a class using jvm runtime dependencies.
117-
// compilations.getByName("test") {
118-
// tasks.register<JavaExec>("ktExec") {
119-
// classpath(runtimeDependencyFiles, output)
120-
// mainClass = "dev.suresh.test.ExecMain"
121-
// }
122-
// }
123-
124116
// attributes.attribute(KotlinPlatformType.attribute, KotlinPlatformType.jvm)
125117
}
126118

plugins/project/src/main/kotlin/dev.suresh.plugin.kotlin.mpp.gradle.kts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@file:Suppress("UnstableApiUsage")
22
@file:OptIn(ExperimentalKotlinGradlePluginApi::class, ExperimentalBCVApi::class)
33

4+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
45
import com.google.devtools.ksp.gradle.KspAATask
56
import com.javiersc.kotlin.kopy.args.KopyFunctions
67
import common.*
@@ -135,6 +136,34 @@ tasks {
135136
}
136137
}
137138

139+
pluginManager.withPlugin("com.gradleup.shadow") {
140+
// Register a shadowJar task for the default jvm target
141+
val mainCompilation = kotlin.jvm().compilations.getByName("main")
142+
val shadowJvmJar by
143+
registering(ShadowJar::class) {
144+
from(tasks.named("jvmJar"))
145+
// from(mainCompilation.output.allOutputs) -> allOutputs == classes + resources
146+
val runtimeDepConfig =
147+
project.configurations.getByName(mainCompilation.runtimeDependencyConfigurationName)
148+
configurations = listOf(runtimeDepConfig)
149+
archiveClassifier = "all"
150+
mergeServiceFiles()
151+
manifest {
152+
attributes[Attributes.Name.MAIN_CLASS.toString()] = libs.versions.app.mainclass
153+
}
154+
}
155+
156+
val buildExecutable by
157+
registering(ReallyExecJar::class) {
158+
jarFile = shadowJvmJar.flatMap { it.archiveFile }
159+
javaOpts = jvmRunArgs
160+
execJarFile = layout.buildDirectory.dir("libs").map { it.file("${project.name}-app") }
161+
onlyIf { OperatingSystem.current().isUnix }
162+
}
163+
164+
build { finalizedBy(buildExecutable) }
165+
}
166+
138167
pluginManager.withPlugin("org.jetbrains.kotlinx.binary-compatibility-validator") {
139168
configure<ApiValidationExtension> {
140169
ignoredPackages.add("dev.suresh.test")
@@ -144,22 +173,9 @@ tasks {
144173
}
145174

146175
withType<KotlinApiBuildTask>().configureEach {
147-
// inputJar = named<Jar>("shadowJar").flatMap { it.archiveFile }
176+
// inputJar = named<Jar>("shadowJvmJar").flatMap { it.archiveFile }
148177
}
149178
}
150-
151-
pluginManager.withPlugin("com.gradleup.shadow") {
152-
val buildExecutable by
153-
registering(ReallyExecJar::class) {
154-
jarFile = named<Jar>("shadowJar").flatMap { it.archiveFile }
155-
// javaOpts = application.applicationDefaultJvmArgs
156-
javaOpts = named<JavaExec>("run").get().jvmArgs
157-
execJarFile = layout.buildDirectory.dir("libs").map { it.file("${project.name}-app") }
158-
onlyIf { OperatingSystem.current().isUnix }
159-
}
160-
161-
build { finalizedBy(buildExecutable) }
162-
}
163179
}
164180

165181
var npmEnabled: String? by rootProject.extra

0 commit comments

Comments
 (0)