|
| 1 | +package com.github.jengelman.gradle.plugins.shadow |
| 2 | + |
| 3 | +import com.github.jengelman.gradle.plugins.shadow.ShadowBasePlugin.Companion.shadow |
| 4 | +import com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin.Companion.shadowJar |
| 5 | +import com.github.jengelman.gradle.plugins.shadow.internal.applicationExtension |
| 6 | +import com.github.jengelman.gradle.plugins.shadow.internal.distributions |
| 7 | +import com.github.jengelman.gradle.plugins.shadow.internal.javaPluginExtension |
| 8 | +import com.github.jengelman.gradle.plugins.shadow.internal.javaToolchainService |
| 9 | +import com.github.jengelman.gradle.plugins.shadow.internal.requireResourceAsText |
| 10 | +import kotlin.io.path.Path |
| 11 | +import org.gradle.api.Plugin |
| 12 | +import org.gradle.api.Project |
| 13 | +import org.gradle.api.plugins.ApplicationPlugin |
| 14 | +import org.gradle.api.tasks.JavaExec |
| 15 | +import org.gradle.api.tasks.Sync |
| 16 | +import org.gradle.api.tasks.TaskContainer |
| 17 | +import org.gradle.api.tasks.TaskProvider |
| 18 | +import org.gradle.api.tasks.application.CreateStartScripts |
| 19 | +import org.gradle.jvm.application.scripts.TemplateBasedScriptGenerator |
| 20 | +import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension |
| 21 | + |
| 22 | +/** |
| 23 | + * A [Plugin] which packages and runs a project as a Java Application using the shadowed jar. |
| 24 | + * |
| 25 | + * Modified from [org.gradle.api.plugins.ApplicationPlugin.java](https://github.com/gradle/gradle/blob/45a20d82b623786d19b50185e595adf3d7b196b2/platforms/jvm/plugins-application/src/main/java/org/gradle/api/plugins/ApplicationPlugin.java). |
| 26 | + */ |
| 27 | +public abstract class ShadowKmpAppPlugin : Plugin<Project> { |
| 28 | + override fun apply(project: Project) { |
| 29 | + project.addRunTask() |
| 30 | + project.addCreateScriptsTask() |
| 31 | +// project.configureDistribution() |
| 32 | + } |
| 33 | + |
| 34 | + protected open fun Project.addRunTask() { |
| 35 | + tasks.register(SHADOW_RUN_TASK_NAME, JavaExec::class.java) { task -> |
| 36 | + task.description = "Runs this project as a JVM application using the shadow jar" |
| 37 | + task.group = ApplicationPlugin.APPLICATION_GROUP |
| 38 | + |
| 39 | + extensions.getByType(KotlinMultiplatformExtension::class.java).jvm().binaries { |
| 40 | + executable { |
| 41 | + task.mainModule.set(mainModule) |
| 42 | + task.mainClass.set(mainClass) |
| 43 | + task.jvmArguments.convention(applicationDefaultJvmArgs) |
| 44 | + val jarFile = executableDir.zip(tasks.shadowJar) { e, s -> |
| 45 | + Path(e).resolveSibling("lib/${s.archiveFile.get().asFile.name}") |
| 46 | + } |
| 47 | + task.classpath(jarFile) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + task.modularity.inferModulePath.convention(javaPluginExtension.modularity.inferModulePath) |
| 52 | + task.javaLauncher.convention(javaToolchainService.launcherFor(javaPluginExtension.toolchain)) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + protected open fun Project.addCreateScriptsTask() { |
| 57 | + tasks.register(SHADOW_SCRIPTS_TASK_NAME, CreateStartScripts::class.java) { task -> |
| 58 | + task.description = "Creates OS specific scripts to run the project as a JVM application using the shadow jar" |
| 59 | + task.group = ApplicationPlugin.APPLICATION_GROUP |
| 60 | + |
| 61 | + val dir = "com/github/jengelman/gradle/plugins/shadow/internal" |
| 62 | + (task.unixStartScriptGenerator as TemplateBasedScriptGenerator).template = |
| 63 | + resources.text.fromString(requireResourceAsText("$dir/unixStartScript.txt")) |
| 64 | + (task.windowsStartScriptGenerator as TemplateBasedScriptGenerator).template = |
| 65 | + resources.text.fromString(requireResourceAsText("$dir/windowsStartScript.txt")) |
| 66 | + |
| 67 | + task.classpath = files(tasks.shadowJar) |
| 68 | + |
| 69 | + extensions.getByType(KotlinMultiplatformExtension::class.java).jvm().binaries { |
| 70 | + executable { |
| 71 | + task.mainModule.set(mainModule) |
| 72 | + task.mainClass.set(mainClass) |
| 73 | + task.conventionMapping.map("applicationName", ::applicationName) |
| 74 | + task.conventionMapping.map("executableDir", ::executableDir) |
| 75 | + task.conventionMapping.map("defaultJvmOpts", ::applicationDefaultJvmArgs) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + task.modularity.inferModulePath.convention(javaPluginExtension.modularity.inferModulePath) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + protected open fun Project.configureDistribution() { |
| 84 | + distributions.register(ShadowBasePlugin.DISTRIBUTION_NAME) { distributions -> |
| 85 | + distributions.contents { distSpec -> |
| 86 | + distSpec.from(file("src/dist")) |
| 87 | + distSpec.into("lib") { lib -> |
| 88 | + lib.from(tasks.shadowJar) |
| 89 | + lib.from(configurations.shadow) |
| 90 | + } |
| 91 | + distSpec.into("bin") { bin -> |
| 92 | + bin.from(tasks.startShadowScripts) |
| 93 | + bin.filePermissions { it.unix(UNIX_SCRIPT_PERMISSIONS) } |
| 94 | + } |
| 95 | + distSpec.with(applicationExtension.applicationDistribution) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public companion object { |
| 101 | + /** |
| 102 | + * Reflects the number of 755. |
| 103 | + */ |
| 104 | + private const val UNIX_SCRIPT_PERMISSIONS = "rwxr-xr-x" |
| 105 | + |
| 106 | + public const val SHADOW_RUN_TASK_NAME: String = "runShadow" |
| 107 | + public const val SHADOW_SCRIPTS_TASK_NAME: String = "startShadowScripts" |
| 108 | + public const val SHADOW_INSTALL_TASK_NAME: String = "installShadowDist" |
| 109 | + |
| 110 | + public inline val TaskContainer.startShadowScripts: TaskProvider<CreateStartScripts> |
| 111 | + get() = named(SHADOW_SCRIPTS_TASK_NAME, CreateStartScripts::class.java) |
| 112 | + |
| 113 | + public inline val TaskContainer.installShadowDist: TaskProvider<Sync> |
| 114 | + get() = named(SHADOW_INSTALL_TASK_NAME, Sync::class.java) |
| 115 | + } |
| 116 | +} |
0 commit comments