使用 Gradle 和 Kotlin 构建一个可自我执行的 jar [英] Building a self-executable jar with Gradle and Kotlin
问题描述
为了开始使用,我编写了一个简单的 kotlin 源文件和一个 gradle 脚本文件.但是我不知道如何将 main func 添加到清单中,以便 jar 可以自我执行.
I've written a simple kotlin source file in order to get started, and a gradle script file. But I can't figure out how to add the main func to the manifest, so that the jar could be self-executable.
这里是我的 build.gradle 脚本:
Here my build.gradle script :
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.9.66'
}
}
apply plugin: "kotlin"
repositories {
mavenCentral()
}
dependencies {
compile 'org.jetbrains.kotlin:kotlin-stdlib:0.9.66'
}
jar {
manifest {
attributes 'Main-Class': 'com.loloof64.kotlin.exps.ExpsPackage'
}
}
这是我的 com.loloof64.kotlin.exps.Multideclarations.kt
Here is my com.loloof64.kotlin.exps.Multideclarations.kt
package com.loloof64.kotlin.exps
class Person(val firstName: String, val lastName: String) {
fun component1(): String {
return firstName
}
fun component2(): String {
return lastName
}
}
fun main(args: Array < String > ) {
val(first, last) = Person("Laurent", "Bernabé")
println("First name : $first - Last name : $last")
}
当我从终端 (java -jar MYJar.jar) 启动 jar 时,我得到以下堆栈跟踪,说我缺少 kotlin 反射库类,实际上它们还没有添加到 jar 中.似乎我缺少最终 jar 中的 kotlin-compiler 工件类,以及 kotlin-stdlib 源,但我不知道如何适应 gradle 构建.
When I launch the jar from terminal (java -jar MYJar.jar) I get the following stacktrace, saying me that the kotlin reflection library classes are missing, and indeed they have not been added to the jar. It seems that I am missing the kotlin-compiler artifact classes from the final jar, and also the kotlin-stdlib sources, but I don't know how to adapt the gradle build.
$> java -jar build/libs/kotlin_exps.jar
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/reflect/jvm/internal/InternalPackage
at com.loloof64.kotlin.exps.ExpsPackage.<clinit>(MultiDeclarations.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.reflect.jvm.internal.InternalPackage
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
我使用的是 Kotlin 0.9.66 和 gradle 2.1
I am using Kotlin 0.9.66 and gradle 2.1
推荐答案
我找到了解决方法(感谢 MkYong 网站)
I've found the workaround (thanks to MkYong website)
- gradle 脚本需要 kotlin-compiler 工件作为依赖项
- gradle 脚本需要一种方法来收集所有 kotlin 文件并将它们放入 jar 中.
所以我得到了以下 gradle 脚本:
So i get with the following gradle script :
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.1-2'
}
}
apply plugin: "kotlin"
repositories {
mavenCentral()
}
dependencies {
compile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.1-2'
}
jar {
manifest {
attributes 'Main-Class': 'com.loloof64.kotlin.exps.MultideclarationsKT'
}
// NEW LINE HERE !!!
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
这篇关于使用 Gradle 和 Kotlin 构建一个可自我执行的 jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!