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

nz_*_*_21 2 jar kotlin

我正在尝试使用 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'
    }
}

Run Code Online (Sandbox Code Playgroud)

(就目前情况而言,当我这样做时./gradlew run,一切都会按预期进行。)

我正在阅读一篇关于如何继续的文章 它说要做:java -jar <MY_PROJECT_NAME>.jar

我不太明白这一点——我们在哪里运行它?我尝试从项目根目录运行它,但出现错误:

Error: Unable to access jarfile <my_jarname>.jar

Run Code Online (Sandbox Code Playgroud)

Ent*_*ops 5

从 Gradle 5.4.1 开始,abuild.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) }
    })
}
Run Code Online (Sandbox Code Playgroud)