问题描述
我不知道如何在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
我在做什么错了?
解决方法
在您的简单情况下,建议您配置默认的IsConfirmedAsync
和shadowJar
任务(并非所有jar
和ShadowJar
类型的任务,因为您没有创建其他任务实例)。
Jar
,
问题是我试图将mainClassName
添加到ShadowJar
任务,它应该已经添加到application
函数中。像这样:
application {
val name = "com.cognite.ingestionservice.MainKt"
mainClass.set(name)
// Required by ShadowJar.
mainClassName = name
}
mainClassName
属性已被弃用,但ShadowJar自版本 6.1.0 起仍然需要。
mainClass.set()
在添加mainClassName
时不是必需的,但是它在gradle 6.7的文档中,所以我还是添加了它。