How to create the fat jar with gradle kotlin script

前端 未结 3 1836
南方客
南方客 2020-12-24 01:31

As titled, I\'d like to know how to modify the gradle.build.kts in order to have a task to create a unique jar with all the dependencies (kotlin li

相关标签:
3条回答
  • 2020-12-24 01:49

    Here is a version that does not use a plugin, more like the Groovy version.

    import org.gradle.jvm.tasks.Jar
    
    val fatJar = task("fatJar", type = Jar::class) {
        baseName = "${project.name}-fat"
        manifest {
            attributes["Implementation-Title"] = "Gradle Jar File Example"
            attributes["Implementation-Version"] = version
            attributes["Main-Class"] = "com.mkyong.DateUtils"
        }
        from(configurations.runtime.map({ if (it.isDirectory) it else zipTree(it) }))
        with(tasks["jar"] as CopySpec)
    }
    
    tasks {
        "build" {
            dependsOn(fatJar)
        }
    }
    

    Also explained here


    Some commenters pointed out that this does not work anymore with newer Gradle versions. Update tested with Gradle 5.4.1:

    import org.gradle.jvm.tasks.Jar
    
    val fatJar = task("fatJar", type = Jar::class) {
        baseName = "${project.name}-fat"
        manifest {
            attributes["Implementation-Title"] = "Gradle Jar File Example"
            attributes["Implementation-Version"] = version
            attributes["Main-Class"] = "com.mkyong.DateUtils"
        }
        from(configurations.runtimeClasspath.get().map({ if (it.isDirectory) it else zipTree(it) }))
        with(tasks.jar.get() as CopySpec)
    }
    
    tasks {
        "build" {
            dependsOn(fatJar)
        }
    }
    

    Note the difference in configurations.runtimeClasspath.get() and with(tasks.jar.get() as CopySpec).

    0 讨论(0)
  • 2020-12-24 01:51

    Here is how to do it as of Gradle 6.5.1, Kotlin/Kotlin-Multiplatform 1.3.72, utilizing a build.gradle.kts file and without using an extra plugin which does seem unnecessary and problematic with multiplatform;

    Note: in reality, few plugins work well with the multiplatform plugin from what I can tell, which is why I suspect its design philosophy is so verbose itself. It's actually fairly elegant IMHO, but not flexible or documented enough so it takes a ton of trial and error to setup even WITHOUT additional plugins.

    Hope this helps others.

    kotlin {
        jvm {
            compilations {
                val main = getByName("main")
                tasks {
                    register<Jar>("fatJar") {
                        group = "application"
                        manifest {
                            attributes["Implementation-Title"] = "Gradle Jar File Example"
                            attributes["Implementation-Version"] = archiveVersion
                            attributes["Main-Class"] = "[[mainClassPath]]"
                        }
                        archiveBaseName.set("${project.name}-fat")
                        from(main.output.classesDirs, main.compileDependencyFiles)
                        with(jar.get() as CopySpec)
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-24 01:53

    You could use the ShadowJar plugin to build a fat jar:

    import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
    
    buildscript {
        repositories {
            mavenCentral()
            gradleScriptKotlin()
        }
        dependencies {
            classpath(kotlinModule("gradle-plugin"))
            classpath("com.github.jengelman.gradle.plugins:shadow:1.2.3")
        }
    }
    
    apply {
        plugin("kotlin")
        plugin("com.github.johnrengelman.shadow")
    }
    
    repositories {
        mavenCentral()
    }
    
    val shadowJar: ShadowJar by tasks
    shadowJar.apply {
        manifest.attributes.apply {
            put("Implementation-Title", "Gradle Jar File Example")
            put("Implementation-Version" version)
            put("Main-Class", "com.mkyong.DateUtils")
        }
    
        baseName = project.name + "-all"
    }
    

    Simply run the task with 'shadowJar'.

    NOTE: This assumes you're using GSK 0.7.0 (latest as of 02/13/2017).

    0 讨论(0)
提交回复
热议问题