更新于 2026/02/27

gradle打包依赖进jar

tasks.create<Jar>("fatJar") {
    archiveBaseName.set(project.name + "-all")
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    manifest {
        attributes["Main-Class"] = "top.e404.bot.loader.Main"
    }
    val exclude = listOf(
        "LICENSE.txt",
        "META-INF/MANIFSET.MF",
        "META-INF/maven/**",
        "META-INF/*.RSA",
        "META-INF/*.SF"
    )
    val dependencies = configurations.runtimeClasspath.get().map(::zipTree)
    from(dependencies).exclude {
        it.path in exclude
    }
    with(tasks.jar.get())
}

或者使用 shadow 插件

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
  kotlin("jvm") version "1.7.21"
  id("com.github.johnrengelman.shadow") version "7.1.2"
}

group = "top.e404"
version = "1.0.0"

repositories {}

dependencies {}

tasks.shadowJar {
  archiveFileName.set("${project.name}-${project.version}-all.jar")
}