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

时间:2019-04-15 21:14:07

标签: 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

3 个答案:

答案 0 :(得分:0)

好的,我知道了:)

因此,创建jar的方法是:./gradlew build。这将在build/libs中创建一个jar。

问题是,当运行该jar时,由于java.lang.intrinsics尚未打包到jar中,因此会遇到关于kotlin stdlib的异常。

虽然有一种方法可以手动完成,但我发现最简单的解决方案是仅使用shadowjar plugin

我的build.gradle最终看起来像这样:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.4'
    }
}

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.3.11'

}

group 'com.github.dynamik'
version '1.0-SNAPSHOT'
apply plugin: 'application'
apply plugin: 'kotlin'
apply plugin: 'java'


mainClassName = "interpreter.Repl"

repositories {
    mavenCentral()
    jcenter()
    maven { setUrl("https://dl.bintray.com/hotkeytlt/maven") }
    maven {
        url = uri("https://plugins.gradle.org/m2/")
    }

}
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'

}
apply plugin: 'com.github.johnrengelman.shadow'



compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

run {
    standardInput = System.in
}

jar {
    manifest {
        attributes 'Main-Class': 'interpreter.Repl'
    }
}

答案 1 :(得分:0)

从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) }
    })
}

答案 2 :(得分:-1)

使用Kotlin主类时,您需要在类名的末尾添加一个Kt,同时在MANIFEST上进行引用。 因此,如果您的主类称为interpreter.Repl,请使用:

jar {
    manifest {
        attributes 'Main-Class': 'interpreter.ReplKt'
    }
}

代替

jar {
    manifest {
        attributes 'Main-Class': 'interpreter.Repl'
    }
}