Polpetta/jkk

Use shadow to remove unused classes from jar

solonovamax opened this issue · 2 comments

Use the shadow plugin to remove unused classes from the jar via minimization.

For example (Kotlin gradle DSL):

tasks {
    withType<ShadowJar>().configureEach {
        archiveClassifier.set("standalone")

        mergeServiceFiles()
        minimize {
            exclude { // exclude any modules you need to exclude from minimization here
                it.moduleName == "something"
            }
        }
    }

    withType<Jar>().configureEach {
        from(rootProject.file("LICENSE"))
        manifest {
            attributes(
                    "Main-Class" to "it.polpetta.Cli",
                    "Built-By" to System.getProperty("user.name"),
                    "Built-Date" to Date(),
                    "Built-Jdk" to System.getProperty("java.version"),
                    "Implementation-Title" to "JKK: A git-like cli for Jenkins written in Kotlin",
                    "Implementation-Version" to project.version.toString(),
                    "Add-Opens" to "java.base/java.lang"
                      )
        }
    }
}

This will make your jar significantly lighter

From testing, minimization drops the size from 23Mb (with the updates I made on my fork) to 14Mb

I'll PR eventually