-
-
Notifications
You must be signed in to change notification settings - Fork 420
Compat Kotlin Multiplatform plugin #1280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 14 commits
e76f49a
70cfa6f
0240988
a7e91f9
c7f8fef
32d9edf
3c71653
0ea49ac
cf18cfd
2b92b10
48a84b4
c08f88a
4f303af
ec506f7
c51900b
b09da44
8de94c3
a16f609
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Integrating with Kotlin Multiplatform Plugin | ||
|
||
Shadow honors Kotlin's | ||
[`org.jetbrains.kotlin.multiplatform`](https://kotlinlang.org/docs/multiplatform-intro.html) plugin and will automatically | ||
configure additional tasks for bundling the shadowed JAR for it's `jvm` target. | ||
|
||
```groovy | ||
// Using Shadow with KMP Plugin | ||
plugins { | ||
id 'org.jetbrains.kotlin.multiplatform' | ||
id 'com.gradleup.shadow' | ||
} | ||
|
||
def ktorVersion = "3.1.0" | ||
|
||
kotlin { | ||
jvm() | ||
sourceSets { | ||
commonMain { | ||
dependencies { | ||
implementation "io.ktor:ktor-client-core$ktorVersion" | ||
} | ||
} | ||
jvmMain { | ||
dependencies { | ||
implementation "io.ktor:ktor-client-okhttp$ktorVersion" | ||
} | ||
} | ||
} | ||
} | ||
|
||
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { | ||
manifest { | ||
// Optionally, set the main class for the shadowed JAR. | ||
attributes 'Main-Class': 'com.example.MainKt' | ||
} | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.github.jengelman.gradle.plugins.shadow | ||
|
||
import assertk.assertThat | ||
import com.github.jengelman.gradle.plugins.shadow.util.containsEntries | ||
import kotlin.io.path.appendText | ||
import kotlin.io.path.writeText | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
|
||
class KmpPluginTest : BasePluginTest() { | ||
@BeforeEach | ||
override fun setup() { | ||
super.setup() | ||
val projectBuildScript = getDefaultProjectBuildScript( | ||
plugin = "org.jetbrains.kotlin.multiplatform", | ||
withGroup = true, | ||
withVersion = true, | ||
) | ||
projectScriptPath.writeText(projectBuildScript) | ||
} | ||
|
||
@Test | ||
fun compatKmpJvmTarget() { | ||
val mainClass = writeMainClass(sourceSet = "jvmMain", isJava = false) | ||
projectScriptPath.appendText( | ||
""" | ||
kotlin { | ||
jvm() | ||
sourceSets { | ||
commonMain { | ||
dependencies { | ||
implementation 'my:b:1.0' | ||
} | ||
} | ||
jvmMain { | ||
dependencies { | ||
implementation 'my:a:1.0' | ||
} | ||
} | ||
} | ||
} | ||
""".trimIndent(), | ||
) | ||
|
||
run(shadowJarTask) | ||
|
||
assertThat(outputShadowJar).useAll { | ||
containsEntries( | ||
mainClass, | ||
*entriesInAB, | ||
) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.github.jengelman.gradle.plugins.shadow | ||
|
||
import com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin.Companion.registerShadowJarCommon | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension | ||
|
||
public abstract class ShadowKmpPlugin : Plugin<Project> { | ||
|
||
override fun apply(project: Project) { | ||
with(project) { | ||
val kmpExtension = extensions.getByType(KotlinMultiplatformExtension::class.java) | ||
val kotlinJvmMain = kmpExtension.jvm().compilations.named("main") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't call More over, they can specify a custom name for their jvm target. The correct API is to lazily iterate over targets collection. And check for KotlinJvmTarget instance.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right, I didn't know how to defer this. Thanks for the reporting. Creating a new issue in #1377. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this will also be useful for #1333. |
||
registerShadowJarCommon { task -> | ||
task.from(kotlinJvmMain.map { it.output.allOutputs }) | ||
task.configurations.convention( | ||
provider { listOf(configurations.getByName(kotlinJvmMain.get().runtimeDependencyConfigurationName)) }, | ||
) | ||
} | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.