gpt4 book ai didi

kotlin - Kotlin:如何创建可运行的jar?

转载 作者:行者123 更新时间:2023-12-02 12:41:35 30 4
gpt4 key购买 nike

我正在尝试用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 - Kotlin:如何创建可运行的jar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55697328/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com