【问题标题】:how to import the ShadowJar plugin for Kotlin (Gradle) builds?如何为 Kotlin (Gradle) 构建导入 ShadowJar 插件?
【发布时间】:2018-07-17 12:13:58
【问题描述】:

构建失败:

thufir@dur:~/NetBeansProjects/kotlin_dsl$ 
thufir@dur:~/NetBeansProjects/kotlin_dsl$ gradle clean run 

> Configure project : 
e: /home/thufir/NetBeansProjects/kotlin_dsl/build.gradle.kts:4:12: Unresolved reference: github


FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'kotlin_dsl'.
> Could not open cache directory 74ykawxta6db3b2bfk9grjikp (/home/thufir/.gradle/caches/4.3.1/gradle-kotlin-dsl/74ykawxta6db3b2bfk9grjikp).
   > Internal error: unable to compile script, see log for details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
thufir@dur:~/NetBeansProjects/kotlin_dsl$ 

有问题的导入声明:

thufir@dur:~/NetBeansProjects/kotlin_dsl$ 
thufir@dur:~/NetBeansProjects/kotlin_dsl$ cat build.gradle.kts 

import org.gradle.api.JavaVersion
import org.gradle.kotlin.dsl.*
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar


plugins {
    application
    kotlin("jvm") version "1.1.51"
}

application {
    mainClassName = "samples.HelloWorldKt"
}

dependencies {
    compile(kotlin("stdlib"))
}

repositories {
    jcenter()
}


thufir@dur:~/NetBeansProjects/kotlin_dsl$ 

删除the import for Shadow as described 会提供干净的构建和运行。如何让 Kotlin 导入 Shadow 插件 JAR?

Gradle 本身,使用 DSL,创建 Shadow JAR 的罚款。

换个角度来看:

Kotlin meets Gradle: gradle.build.kt with ShadowJar

使用工作构建文件。

【问题讨论】:

  • 你试过this answer的构建脚本了吗?
  • 在您的构建脚本中,Shadow 插件似乎没有应用在 plugins { ... } 块中,这可能是问题所在。
  • 构建失败,因为它无法编译build.gradle.kts 文件——但错误消息不是那么好。另见:stackoverflow.com/q/48663427/262852

标签: gradle build kotlin gradle-kotlin-dsl shadowjar


【解决方案1】:

像这样:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

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

group = "xxx.yyy"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation(kotlin("stdlib-jdk8"))
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "11"
}

tasks.withType<ShadowJar> {
    archiveFileName.set("app.jar")
}

【讨论】:

  • 只是为了给这个线程一个更新......对于带有 Kotlin DSL 和 johnrengelman.shadow 的最新版本的 gradle,您需要进行一些更改以避免弃用警告。从 Louis Jacomet 回答的问题: getArchiveFileName() 返回一个 Property 这是一个可变对象,您可以在其上调用 Property.set(value) github.com/gradle/gradle/issues/10506#issuecomment-529333401 我使用了以下代码:tasks.withType() { archiveBaseName.set("app") archiveClassifier.set("...") archiveVersion.set("...") }
猜你喜欢
  • 2019-05-01
  • 1970-01-01
  • 2019-09-20
  • 2018-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-25
  • 2014-12-09
相关资源
最近更新 更多