Skip to content

Commit a6aff56

Browse files
GooolerCopilot
andauthored
Document Kotlin JVM plugin (#1339)
* Rename files * Mention things about Kotlin JVM plugin * Fix kt writing * Note stdlib things * Fix typo * Remove duplicates --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent a3e46ba commit a6aff56

File tree

5 files changed

+75
-8
lines changed

5 files changed

+75
-8
lines changed

docs/kmp-plugin/README.md docs/kotlin-plugins/README.md

+48-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,49 @@
1-
# Integrating with Kotlin Multiplatform Plugin
1+
# Integrating with Kotlin Plugins
2+
3+
Kotlin standard libraries (stdlib) are added by Kotlin plugins by default, they will be bundled into the shadowed JARs automatically.
4+
If you don't need a standard library at all, you can add the following Gradle property to your gradle.properties file:
5+
6+
```properties
7+
kotlin.stdlib.default.dependency=false
8+
```
9+
10+
See more information about [Dependency on the standard library](https://kotlinlang.org/docs/gradle-configure-project.html#dependency-on-the-standard-library).
11+
12+
## For Kotlin JVM Plugin
13+
14+
Shadow works well for Kotlin JVM projects like Java projects. Here is an example:
15+
16+
=== "Kotlin"
17+
18+
```kotlin
19+
plugins {
20+
kotlin("jvm")
21+
id("com.gradleup.shadow")
22+
}
23+
24+
dependencies {
25+
implementation("io.ktor:ktor-client-okhttp:3.1.0")
26+
}
27+
```
28+
29+
=== "Groovy"
30+
31+
```groovy
32+
plugins {
33+
id 'org.jetbrains.kotlin.jvm'
34+
id 'com.gradleup.shadow'
35+
}
36+
37+
dependencies {
38+
implementation 'io.ktor:ktor-client-okhttp:3.1.0'
39+
}
40+
```
41+
42+
You can mix the Kotlin JVM plugin with `java-gradle-plugin`, `application`, and other Java plugins,
43+
easily organize your build logic for [Packaging Gradle Plugins](../plugins/README.md), [Publishing Libraries](../publishing/README.md),
44+
[Running Applications](../application-plugin/README.md), and so on.
45+
46+
## For Kotlin Multiplatform Plugin
247

348
Shadow honors Kotlin's
449
[`org.jetbrains.kotlin.multiplatform`](https://kotlinlang.org/docs/multiplatform-intro.html) plugin and will automatically
@@ -59,12 +104,12 @@ configure additional tasks for bundling the shadowed JAR for its `jvm` target.
59104
sourceSets {
60105
commonMain {
61106
dependencies {
62-
implementation "io.ktor:ktor-client-core:$ktorVersion"
107+
implementation 'io.ktor:ktor-client-core:$ktorVersion'
63108
}
64109
}
65110
jvmMain {
66111
dependencies {
67-
implementation "io.ktor:ktor-client-okhttp:$ktorVersion"
112+
implementation 'io.ktor:ktor-client-okhttp:$ktorVersion'
68113
}
69114
}
70115
}

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Omit automatic compile dependency on kotlin-stdlib.
2-
# https://kotlinlang.org/docs/gradle.html#dependency-on-the-standard-library
2+
# https://kotlinlang.org/docs/gradle-configure-project.html#dependency-on-the-standard-library
33
kotlin.stdlib.default.dependency=false
44

55
org.gradle.caching=true

mkdocs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ nav:
7070
- 'Reproducible Builds': configuration/reproducible-builds/README.md
7171
- 'Custom Tasks': custom-tasks/README.md
7272
- 'Application Plugin': application-plugin/README.md
73-
- 'KMP Plugin': kmp-plugin/README.md
73+
- 'Kotlin Plugins': kotlin-plugins/README.md
7474
- 'Groovy and Scala Plugins': groovy-and-scala-plugins/README.md
7575
- 'Publishing': publishing/README.md
7676
- 'Multi-Project': multi-project/README.md

src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/BasePluginTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ abstract class BasePluginTest {
225225
""".trimIndent()
226226
}
227227
JvmLang.Kotlin -> {
228-
val imports = if (withImports) "import junit.framework.Test;" else ""
229-
val classRef = if (withImports) "\"Refs: \" + Test.class.getName()" else "\"Refs: null\""
228+
val imports = if (withImports) "import junit.framework.Test" else ""
229+
val classRef = if (withImports) "\"Refs: \" + Test::class.java.name" else "\"Refs: null\""
230230
"""
231231
@file:JvmName("$className")
232232
package $packageName

src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/KmpPluginTest.kt src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/KotlinPluginsTest.kt

+23-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.junit.jupiter.api.Test
1313
import org.junit.jupiter.params.ParameterizedTest
1414
import org.junit.jupiter.params.provider.ValueSource
1515

16-
class KmpPluginTest : BasePluginTest() {
16+
class KotlinPluginsTest : BasePluginTest() {
1717
@BeforeEach
1818
override fun setup() {
1919
super.setup()
@@ -25,6 +25,28 @@ class KmpPluginTest : BasePluginTest() {
2525
projectScriptPath.writeText(projectBuildScript)
2626
}
2727

28+
@Test
29+
fun compatKotlinJvmPlugin() {
30+
projectScriptPath.writeText(
31+
"""
32+
${getDefaultProjectBuildScript(plugin = "org.jetbrains.kotlin.jvm", withGroup = true, withVersion = true)}
33+
dependencies {
34+
implementation 'junit:junit:3.8.2'
35+
}
36+
""".trimIndent(),
37+
)
38+
val mainClassEntry = writeClass(withImports = true, jvmLang = JvmLang.Kotlin)
39+
40+
run(shadowJarTask)
41+
42+
assertThat(outputShadowJar).useAll {
43+
containsEntries(
44+
mainClassEntry,
45+
*junitEntries,
46+
)
47+
}
48+
}
49+
2850
@Test
2951
fun compatKmpJvmTarget() {
3052
val mainClassEntry = writeClass(sourceSet = "jvmMain", jvmLang = JvmLang.Kotlin)

0 commit comments

Comments
 (0)