我正试着和Kotlin一起创建一个可运行的罐子。
我的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
时,一切都按预期进行。)
我正在读一篇关于如何继续进行的这里文章,它说要做:java -jar <MY_PROJECT_NAME>.jar
。
我不太明白-- ,我们在哪里运行这个?我尝试从我的项目根运行它,我得到了一个错误:
Error: Unable to access jarfile <my_jarname>.jar
发布于 2019-04-15 22:27:06
好吧,我想出来了:)
因此,创建jar的方法是go:./gradlew build
。这将在build/libs
中创建一个jar。
问题是,当运行jar时,会遇到关于java.lang.intrinsics
的异常,因为kotlin stdlib
还没有打包到jar中。
虽然有一种手动完成此操作的方法,但我发现最简单的解决方案是简单地使用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'
}
}
发布于 2019-06-09 22:10:33
从第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) }
})
}
发布于 2019-04-15 21:18:57
当使用Kotlin主类时,您需要在类名末尾添加一个Kt
,同时在清单上引用它。因此,如果您的主类名为interpreter.Repl
,请使用:
jar {
manifest {
attributes 'Main-Class': 'interpreter.ReplKt'
}
}
而不是
jar {
manifest {
attributes 'Main-Class': 'interpreter.Repl'
}
}
https://stackoverflow.com/questions/55697328
复制相似问题