我在遇到与 Kotlin 和 gradle 相同的问题时遇到了这个答案。我想打包让罐子工作,但一直在起球错误。
使用像 com.example.helloworld.kt 这样的文件包含您的代码:
fun main(args: Array<String>) {
println("Hello, World!")
}
这就是文件build.gradle.kts 的样子,让你开始使用 gradle。
import org.gradle.kotlin.dsl.*
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
application
kotlin("jvm") version "1.3.50"
}
// Notice the "Kt" in the end, meaning the main is not in the class
application.mainClassName = "com.example.MainKt"
dependencies {
compile(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
tasks.withType<Jar> {
// Otherwise you'll get a "No main manifest attribute" error
manifest {
attributes["Main-Class"] = "com.example.MainKt"
}
// To add all of the dependencies otherwise a "NoClassDefFoundError" error
from(sourceSets.main.get().output)
dependsOn(configurations.runtimeClasspath)
from({
configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
})
}
所以一旦你gradle clean build 你可以这样做:
gradle run
> Hello, World!
假设您的投影仪使用build/libs/hello.jar 中的jar 假设您在settings.gradle.kts 中设置了rootProject.name = "hello"
然后就可以运行了:
java -jar hello.jar
> Hello, World!