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

标签 kotlin jar

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

相关文章:

android - 如何在recyclerview中实现拖放和滑动删除?

android - 如何在 OnCompleteListener Firebase 中使用 Async/Await/Coroutines

java - 用 Maven 解决 jar 问题?

java - Spring Boot - 从外部 jar 映射实体

java - 运行时处理jar文件

java - 我可以将 REST 请求转发到其他资源吗?

android - 如何使用 android studio 在 kotlin 代码中添加 try/catch?

android - Kotlin - 另一个对象(不是类)内的伴随对象的任何替代品?

java - gwt-maven-plugin 无法编译,因为 "No source code is available for type org.hibernate.validator.constraints.impl.SizeValidatorForString;"

java - 检查 jar 文件的内容