【问题标题】:How to configure Shadow plugin using kotlin dsl如何使用 kotlin dsl 配置 Shadow 插件
【发布时间】:2021-02-13 04:02:23
【问题描述】:

我不知道如何在带有 kotlin DSL 的 gradle 构建中使用 shadow 插件。 所有文档都使用 groovy dsl。

这是build.gradle.kts的内容:

import groovy.lang.GroovyObject
import org.gradle.jvm.tasks.Jar

plugins {
    // Apply the Kotlin JVM plugin to add support for Kotlin.
    id("org.jetbrains.kotlin.jvm") version "1.4.10"

    id("com.github.johnrengelman.shadow") version "6.1.0"
    application
}

allprojects {
    repositories {
        // Use jcenter for resolving dependencies.
        // You can declare any Maven/Ivy/file repository here.
        jcenter()
    }
    group = "com.example"
    version = "1.0-SNAPSHOT"
}

dependencies {
    // Align versions of all Kotlin components
    implementation(platform("org.jetbrains.kotlin:kotlin-bom"))

    // Use the Kotlin JDK 8 standard library.
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
}

application {
    mainClass.set("com.example.MainKt")
}


tasks.withType<Jar> {
    manifest {
        attributes(
                mapOf(
                    "ImplementationTitle" to project.name,
                    "Implementation-Version" to project.version)
        )
    }
}

这是src/main/kotlin/com/example/Main.kt的内容

package com.example

fun main() {
    println("Hello world")
}

但是当我尝试做gradle build 时,我得到了这个错误:

A problem was found with the configuration of task ':shadowJar' (type 'ShadowJar').
> No value has been specified for property 'mainClassName'.

我觉得这很奇怪,因为我已经在 application 参数中输入了应用程序主类。

我已经尝试添加这个:

tasks.withType<ShadowJar>() {
    mainClassName = "com.example.MainKt"
}

但是当我尝试使用此选项进行构建时,它会抱怨找不到ShadowJar 类型。

  Line 22: tasks.withType<ShadowJar>() {
                          ^ Unresolved reference: ShadowJar

我在这里做错了什么?

【问题讨论】:

  • 尝试tasks.withType&lt;com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar&gt; {} 或在脚本顶部导入该包。
  • 只需为ShadowJar 添加一个导入。事实上,IDE 应该向您推荐它,这是使用 Kotlin DSL 的一个好处。

标签: kotlin gradle shadowjar


【解决方案1】:

问题是我试图将mainClassName 添加到ShadowJar 任务中,它应该已经添加到application 函数中。像这样:

application {
    val name = "com.cognite.ingestionservice.MainKt"
    mainClass.set(name)

    // Required by ShadowJar.
    mainClassName = name
}

mainClassName 属性已弃用,但从 6.1.0 版本开始,ShadowJar 仍需要该属性。

mainClass.set()在添加mainClassName时不是必须的,但是在gradle 6.7的文档中,所以我还是添加了。

【讨论】:

    【解决方案2】:

    我建议在您的简单情况下配置默认的 shadowJar 和 jar 任务(不是所有 ShadowJar 和 Jar 类型的任务,因为您没有创建其他实例)。

    tasks {
        jar {
            manifest {
                attributes(
                    mapOf(
                        "Main-Class" to "com.example.MainKt", //will make your jar (produced by jar task) runnable 
                        "ImplementationTitle" to project.name,
                        "Implementation-Version" to project.version)
                )
            }
        }
        shadowJar {
            manifest.inheritFrom(jar.get().manifest) //will make your shadowJar (produced by jar task) runnable
        }
    }
    
    application {
        mainClassName = "com.example.MainKt" //will make run & runShadow tasks work. Have no idea why it can't take main class from jar/shadowJar manifests
    }
    
    

    【讨论】:

      猜你喜欢
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      相关资源
      最近更新 更多