【问题标题】:Fat Jar expands dependencies with Gradle Kotlin DSLFat Jar 使用 Gradle Kotlin DSL 扩展依赖项
【发布时间】:2019-06-09 21:21:27
【问题描述】:

我正在尝试在基于 Kotlin 的 gradle 文件中使用以下内容构建一个胖 jar。

val fatJar = task("fatJar", type = Jar::class) {
  baseName = "safescape-lib-${project.name}"
  // manifest Main-Class attribute is optional.
  // (Used only to provide default main class for executable jar)
  from(configurations.runtimeClasspath.map({ if (it.isDirectory) it else zipTree(it) }))
  with(tasks["jar"] as CopySpec)
}

tasks {
  "build" {
    dependsOn(fatJar)
  }
}

然而,fat jar 扩展了所有依赖项。我想将 jars 原样包含在 /lib 目录中,但我不知道如何实现这一点。

谁能指点我如何做到这一点?

谢谢

【问题讨论】:

    标签: gradle gradle-kotlin-dsl


    【解决方案1】:

    您在规范的map 部分使用zipTree,它的行为符合the documentation:它解压缩不是目录的文件。

    如果您想要 /lib 中的罐子,请将您的 from 替换为:

    from(configurations.runtimeClasspath) {
        into("lib")
    }
    

    【讨论】:

      【解决方案2】:

      如果有人使用kotlin-multiplatform 插件,配置会有些不同。这是一个fatJar 任务配置,假设 JVM 应用程序带有来自 JS 模块的嵌入式 JS 前端:

      afterEvaluate {
        tasks {
          create("jar", Jar::class).apply {
            dependsOn("jvmMainClasses", "jsJar")
            group = "jar"
            manifest {
              attributes(
                mapOf(
                  "Implementation-Title" to rootProject.name,
                  "Implementation-Version" to rootProject.version,
                  "Timestamp" to System.currentTimeMillis(),
                  "Main-Class" to mainClassName
                )
              )
            }
            val dependencies = configurations["jvmRuntimeClasspath"].filter { it.name.endsWith(".jar") } +
                project.tasks["jvmJar"].outputs.files +
                project.tasks["jsJar"].outputs.files
            dependencies.forEach { from(zipTree(it)) }
            into("/lib")
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-29
        • 2020-07-29
        • 2016-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多