Skip to content

Commit d82fefa

Browse files
committed
chore: multiplatform jvm target support in shadow plugin
1 parent 6e6dd1c commit d82fefa

File tree

4 files changed

+58
-53
lines changed

4 files changed

+58
-53
lines changed

gradle/build-logic/src/main/kotlin/common/ProjectExtns.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import com.google.devtools.ksp.gradle.KspAATask
66
import java.io.ByteArrayOutputStream
77
import java.io.File
88
import java.nio.file.Path
9+
import java.time.ZonedDateTime
10+
import java.time.format.DateTimeFormatter
11+
import java.util.jar.Attributes
912
import org.graalvm.buildtools.gradle.dsl.GraalVMExtension
1013
import org.gradle.accessors.dm.LibrariesForLibs
1114
import org.gradle.api.Action
@@ -101,6 +104,27 @@ val Project.toolchainVendor
101104
val Project.addModules
102105
get() = libs.versions.java.addModules.get()
103106

107+
val Project.isAutomaticModuleEnabled
108+
get() = gradleBooleanProperty("java.automatic.module.enabled").get()
109+
110+
val Project.defaultJarManifest
111+
get() = buildMap {
112+
put("Enable-Native-Access", "ALL-UNNAMED")
113+
put("Built-By", System.getProperty("user.name"))
114+
put("Built-Jdk", System.getProperty("java.runtime.version"))
115+
put(
116+
"Built-OS",
117+
"${System.getProperty("os.name")} ${System.getProperty("os.arch")} ${System.getProperty("os.version")}")
118+
put("Build-Timestamp", DateTimeFormatter.ISO_INSTANT.format(ZonedDateTime.now()))
119+
put("Created-By", "Gradle ${gradle.gradleVersion}")
120+
put(Attributes.Name.IMPLEMENTATION_TITLE.toString(), project.name)
121+
put(Attributes.Name.IMPLEMENTATION_VERSION.toString(), project.version)
122+
put(Attributes.Name.IMPLEMENTATION_VENDOR.toString(), project.group)
123+
if (isAutomaticModuleEnabled) {
124+
put("Automatic-Module-Name", project.group)
125+
}
126+
}
127+
104128
/** Kotlin version properties. */
105129
val Project.kotlinVersion
106130
get() = libs.versions.kotlin.asProvider()
@@ -211,6 +235,7 @@ fun Project.jvmArguments(appRun: Boolean = false, headless: Boolean = true) = bu
211235
"-XX:+UnlockExperimentalVMOptions",
212236
"-XX:+UseCompactObjectHeaders",
213237
"-XX:MaxRAMPercentage=0.8",
238+
// "-XX:+UseLargePages",
214239
// "-XX:+UseEpsilonGC",
215240
// "-XX:+AlwaysPreTouch",
216241
"""-Xlog:gc*,stringdedup*:
@@ -279,6 +304,7 @@ fun Project.jvmArguments(appRun: Boolean = false, headless: Boolean = true) = bu
279304
// "-XshowSettings:system",
280305
// "-XshowSettings:properties",
281306
// "--show-module-resolution",
307+
// "-XX:-StackTraceInThrowable",
282308
// "-XX:+ShowHiddenFrames",
283309
// "-verbose:module",
284310
// "-XX:ConcGCThreads=2",
@@ -692,7 +718,11 @@ fun Project.printTaskGraph() {
692718
}
693719
}
694720

695-
/** Adds [file] as an outgoing variant to publication. */
721+
/**
722+
* Adds [file] as an outgoing variant to publication. Refer
723+
* [Customizing publishing](https://docs.gradle.org/current/userguide/publishing_customization.html)
724+
* for more details.
725+
*/
696726
@Suppress("UnstableApiUsage")
697727
fun Project.addFileToJavaComponent(file: File) {
698728
// Here's a configuration to declare the outgoing variant

gradle/build-logic/src/main/kotlin/dev.suresh.plugin.kotlin.jvm.gradle.kts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import com.javiersc.kotlin.kopy.args.KopyFunctions
55
import common.*
66
import java.io.PrintWriter
77
import java.io.StringWriter
8-
import java.time.ZonedDateTime
9-
import java.time.format.DateTimeFormatter
10-
import java.util.jar.Attributes
118
import java.util.spi.ToolProvider
129
import kotlinx.validation.*
1310
import org.gradle.internal.os.OperatingSystem
@@ -108,21 +105,7 @@ tasks {
108105
withType<KspAATask>().configureEach { configureKspConfig() }
109106

110107
withType<Jar>().configureEach {
111-
manifest {
112-
attributes(
113-
// "Automatic-Module-Name" to project.group,
114-
"Enable-Native-Access" to "ALL-UNNAMED",
115-
"Built-By" to System.getProperty("user.name"),
116-
"Built-Jdk" to System.getProperty("java.runtime.version"),
117-
"Built-OS" to
118-
"${System.getProperty("os.name")} ${System.getProperty("os.arch")} ${System.getProperty("os.version")}",
119-
"Build-Timestamp" to DateTimeFormatter.ISO_INSTANT.format(ZonedDateTime.now()),
120-
"Created-By" to "Gradle ${gradle.gradleVersion}",
121-
Attributes.Name.IMPLEMENTATION_TITLE.toString() to project.name,
122-
Attributes.Name.IMPLEMENTATION_VERSION.toString() to project.version,
123-
Attributes.Name.IMPLEMENTATION_VENDOR.toString() to project.group,
124-
)
125-
}
108+
manifest { attributes(defaultJarManifest) }
126109
duplicatesStrategy = DuplicatesStrategy.INCLUDE
127110
}
128111

gradle/build-logic/src/main/kotlin/dev.suresh.plugin.kotlin.mpp.gradle.kts

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
55
import com.google.devtools.ksp.gradle.KspAATask
66
import com.javiersc.kotlin.kopy.args.KopyFunctions
77
import common.*
8-
import java.time.ZonedDateTime
9-
import java.time.format.DateTimeFormatter
108
import java.util.jar.Attributes
119
import kotlinx.validation.*
1210
import org.gradle.internal.os.OperatingSystem
@@ -112,21 +110,7 @@ tasks {
112110
// withType<Kotlin2JsCompile>().configureEach {}
113111

114112
withType<Jar>().configureEach {
115-
manifest {
116-
attributes(
117-
// "Automatic-Module-Name" to project.group,
118-
"Enable-Native-Access" to "ALL-UNNAMED",
119-
"Built-By" to System.getProperty("user.name"),
120-
"Built-Jdk" to System.getProperty("java.runtime.version"),
121-
"Built-OS" to
122-
"${System.getProperty("os.name")} ${System.getProperty("os.arch")} ${System.getProperty("os.version")}",
123-
"Build-Timestamp" to DateTimeFormatter.ISO_INSTANT.format(ZonedDateTime.now()),
124-
"Created-By" to "Gradle ${gradle.gradleVersion}",
125-
Attributes.Name.IMPLEMENTATION_TITLE.toString() to project.name,
126-
Attributes.Name.IMPLEMENTATION_VERSION.toString() to project.version,
127-
Attributes.Name.IMPLEMENTATION_VENDOR.toString() to project.group,
128-
)
129-
}
113+
manifest { attributes(defaultJarManifest) }
130114
duplicatesStrategy = DuplicatesStrategy.INCLUDE
131115
}
132116

@@ -151,32 +135,37 @@ tasks {
151135
}
152136

153137
pluginManager.withPlugin("com.gradleup.shadow") {
154-
// Register a shadowJar task for the default jvm target
155-
val shadowJvmJar by
156-
registering(ShadowJar::class) {
157-
val main by kotlin.jvm().compilations
158-
// allOutputs == classes + resources
159-
from(main.output.allOutputs)
160-
val runtimeDepConfig =
161-
project.configurations.getByName(main.runtimeDependencyConfigurationName)
162-
configurations = listOf(runtimeDepConfig)
163-
archiveClassifier = "all"
164-
mergeServiceFiles()
138+
val shadowJar by
139+
existing(ShadowJar::class) {
140+
// https://gradleup.com/shadow/kmp-plugin/
165141
manifest {
166142
attributes[Attributes.Name.MAIN_CLASS.toString()] = libs.versions.app.mainclass
167143
}
168-
duplicatesStrategy = DuplicatesStrategy.INCLUDE
169144
}
170145

171146
val buildExecutable by
172147
registering(ReallyExecJar::class) {
173-
jarFile = shadowJvmJar.flatMap { it.archiveFile }
148+
jarFile = shadowJar.flatMap { it.archiveFile }
174149
javaOpts = jvmRunArgs
175150
execJarFile = layout.buildDirectory.dir("libs").map { it.file("${project.name}-app") }
176151
onlyIf { OperatingSystem.current().isUnix }
177152
}
178153

179154
build { finalizedBy(buildExecutable) }
155+
156+
// Shows how to register a shadowJar task for the default jvm target
157+
register<ShadowJar>("shadowJvmJar") {
158+
val main by kotlin.jvm().compilations
159+
// allOutputs == classes + resources
160+
from(main.output.allOutputs)
161+
val runtimeDepConfig =
162+
project.configurations.getByName(main.runtimeDependencyConfigurationName)
163+
configurations = listOf(runtimeDepConfig)
164+
archiveClassifier = "jvm-all"
165+
mergeServiceFiles()
166+
manifest { attributes[Attributes.Name.MAIN_CLASS.toString()] = libs.versions.app.mainclass }
167+
duplicatesStrategy = DuplicatesStrategy.INCLUDE
168+
}
180169
}
181170

182171
pluginManager.withPlugin("org.jetbrains.kotlinx.binary-compatibility-validator") {
@@ -188,7 +177,7 @@ tasks {
188177
}
189178

190179
withType<KotlinApiBuildTask>().configureEach {
191-
// inputJar = named<Jar>("shadowJvmJar").flatMap { it.archiveFile }
180+
// inputJar = named<Jar>("shadowJar").flatMap { it.archiveFile }
192181
}
193182
}
194183
}

gradle/libs.versions.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ kotlinx-lincheck = "2.36"
3636
kotlinx-html = "0.12.0"
3737
kotlinx-browser = "0.3"
3838
kotlinx-benchmark = "0.4.13"
39+
kotlinx-fuzz = "0.2.2"
3940
kotlinx-metadata = "0.9.0"
4041
kotlinx-reflect-lite = "1.1.0"
41-
kotlin-wrappers = "2025.3.3"
42+
kotlin-wrappers = "2025.3.5"
4243
kotlin-redacted = "1.12.1"
4344
kotlin-serviceloader = "0.0.15"
4445
kotlinx-multik = "0.2.3"
@@ -146,7 +147,7 @@ expiringmap = "0.5.10"
146147
otp = "2.1.0"
147148
password4j = "1.8.2"
148149
apache-commons-imaging = "1.0-alpha5"
149-
testcontainers = "1.20.5"
150+
testcontainers = "1.20.6"
150151
sourceBuddy = "2.5.0"
151152
tcp-javanioproxy = "1.6"
152153
kubernetes-client = "23.0.0"
@@ -214,7 +215,7 @@ nmcp = "0.0.9"
214215
nexus-publish = "2.0.0"
215216
vanniktech-publish = "0.30.0"
216217
gradle-publish = "1.3.0"
217-
shadow = "9.0.0-beta9"
218+
shadow = "9.0.0-beta10"
218219
spotless = "7.0.2"
219220
semver-plugin = "0.8.0"
220221
vercraft = "0.5.0"
@@ -346,6 +347,7 @@ kotlinx-io-core = { module = "org.jetbrains.kotlinx:kotlinx
346347
kotlinx-io-filesystem = { module = "org.jetbrains.kotlinx:kotlinx-io-filesystem" , version.ref = "kotlinx-io"}
347348
kotlinx-io-okio = { module = "org.jetbrains.kotlinx:kotlinx-io-okio" , version.ref = "kotlinx-io"}
348349
kotlinx-lincheck = { module = "org.jetbrains.kotlinx:lincheck" , version.ref = "kotlinx-lincheck"}
350+
kotlinx-fuzz-jazzer = { module = "org.jetbrains:kotlinx.fuzz.jazzer" , version.ref = "kotlinx-fuzz"}
349351
kotlinx-reflect-lite = { module = "org.jetbrains.kotlinx:kotlinx.reflect.lite" , version.ref = "kotlinx-reflect-lite"}
350352
kotlinx-collections-immutable = { module = "org.jetbrains.kotlinx:kotlinx-collections-immutable" , version.ref = "kotlinx-collections-immutable"}
351353
kotlinx-html = { module = "org.jetbrains.kotlinx:kotlinx-html" , version.ref = "kotlinx-html"}
@@ -737,6 +739,7 @@ kotlin-js-plainobjects = { id = "org.jetbrains.kotlin.plugin.js-pl
737739
kotlin-compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose" , version.ref = "kotlin"}
738740
kotlinx-atomicfu = { id = "org.jetbrains.kotlinx.atomicfu" , version.ref = "kotlinx-atomicfu"}
739741
kotlinx-benchmark = { id = "org.jetbrains.kotlinx.benchmark" , version.ref = "kotlinx-benchmark" }
742+
kotlinx-fuzz = { id = "kotlinx.fuzz.gradle" , version.ref = "kotlinx-fuzz" }
740743
kotlinx-rpc = { id = "org.jetbrains.kotlinx.rpc.plugin" , version.ref = "kotlinx-rpc" }
741744
jetbrains-compose = { id = "org.jetbrains.compose" , version.ref = "jetbrains-compose"}
742745
jetbrains-compose-hotreload = { id = "org.jetbrains.compose.hot-reload" , version.ref = "compose-hotreload"}

0 commit comments

Comments
 (0)