我正在尝试使用 Kotlin 创建一个可运行的 jar。
我的 gradle.build 是这样的:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.11'
}
group 'com.github.dynamik'
version '1.0-SNAPSHOT'
apply plugin: 'application'
apply plugin: 'kotlin'
mainClassName = "interpreter.Repl"
repositories {
mavenCentral()
maven { setUrl("https://dl.bintray.com/hotkeytlt/maven") }
}
configurations {
ktlint
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile 'com.github.h0tk3y.betterParse:better-parse-jvm:0.4.0-alpha-3'
// https://mvnrepository.com/artifact/junit/junit
testCompile group: 'junit', name: 'junit', version: '4.4'
ktlint "com.github.shyiko:ktlint:0.31.0"
implementation 'com.github.ajalt:clikt:1.7.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.0'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
run {
standardInput = System.in
}
jar {
manifest {
attributes 'Main-Class': 'interpreter.Repl'
}
}
(就目前情况而言,当我执行 ./gradlew run
时,一切都会按预期进行。)
我正在读一篇文章 here关于如何继续,它说要做:java -jar <MY_PROJECT_NAME>.jar
.
我不太明白这一点 - 我们在哪里运行它?我尝试从项目根目录运行它,但出现错误:
Error: Unable to access jarfile <my_jarname>.jar
最佳答案
从 Gradle 5.4.1 开始,build.gradle.kts
需要这样的部分:
tasks.register<Jar>("uberJar") {
archiveClassifier.set("uber")
manifest {
attributes(
"Main-Class" to "mytest.AppKt",
"Implementation-Title" to "Gradle",
"Implementation-Version" to archiveVersion
)
}
from(sourceSets.main.get().output)
dependsOn(configurations.runtimeClasspath)
from({
configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
})
}
关于Kotlin:如何创建可运行的 jar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55697328/