How to create a fat Jar in Gradle with Kotlin DSL and plugin

Viewed 263

I did a lot of research on how to create fat jars with Gradle. However, I can not figure out how to it with Kotlin DSL and a plugin. I have this code:

plugins {
    application
    id("org.openjfx.javafxplugin") version "0.0.9"
    id("com.github.johnrengelman.shadow") version "6.1.0"
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("javax.vecmath", "vecmath", "1.5.2")
    implementation("org.apache.commons", "commons-csv", "1.8")
}

application {
    mainModule.set("de.weisbrja")
    mainClass.set("de.weisbrja.App")
}

javafx {
    modules("javafx.controls")
}

modularity.disableEffectiveArgumentsAdjustment()

But I do not know how to specify the main class for the fat jar manifest. The tutorial I followed did this:

jar {
    manifest {
        attributes "Main-Class": "com.baeldung.fatjar.Application"
    }

    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

But that is Groovy DSL and not Kotlin DSL and I am not really familiar with the Kotlin DSL yet, so I do not know how to convert this to Kotlin DSL. Help very much appreciated.

1 Answers

try this vanilla solution in kotlin dsl (build.gradle.kts)

dependencies {
     implementation(group="org.mycomp",name="foo",version="1.0")
}

tasks {
   jar {
       //package org.mycomp.foo inside the .jar file 
       from(configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) })
   }
}

alternatively you can use the shadowJar plugin

plugins {
    id("com.github.johnrengelman.shadow") version "7.0.0"
}

then just run ./gradlew :shadowJar

Related