如何使用Kotlin DSL配置Shadow插件

问题描述

我不知道如何在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

我在做什么错了?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)