4

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.

weisbrja
  • 164
  • 10
  • Don't try to create fat jars for JavaFX programs. This does not really work for various reasons. Use a tool like jpackage instead to create a real installer bundle. – mipa Mar 25 '21 at 09:26
  • @mipa can you show me how to that instead then? – weisbrja Mar 25 '21 at 09:53
  • @mipa also, why should I not create a fat jar of a JavaFX program? – weisbrja Mar 25 '21 at 10:09
  • I don't know if JavaFX will work in a fat jar, but the tutorial you found is wrong. Instead, you can use the example in the Gradle user guide [here](https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_packaging) to get it working (be sure to select Kotlin for the code examples), and to add an entry to the manifest in Kotlin, look [here](https://docs.gradle.org/current/userguide/building_java_projects.html#sec:jar_manifest). – Bjørn Vester Mar 25 '21 at 20:52
  • I can not figure out what key and value I have to add to the manifest. Can you post a code answer? – weisbrja Mar 26 '21 at 10:51

1 Answers1

2

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

Ryu S.
  • 1,538
  • 2
  • 22
  • 41