将Kotlin与Gradle结合使用 [英] using Kotlin with Gradle

查看:220
本文介绍了将Kotlin与Gradle结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是KotlinGradle的新手,并尝试遵循

I'm new to Kotlin and Gradle, and tried to follow these steps, so I got the following 2 files:

运行gradle init后,我将build.gradle更改为:

// set up the kotlin-gradle plugin
buildscript {
    ext.kotlin_version = '1.1.2-2'

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

// apply the kotlin-gradle plugin
apply plugin: "kotlin"
apply plugin: 'application'

mainClassName = "hello.main"

// add kotlin-stdlib dependencies.
repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

Hello.kt:

package hello   

fun main(args: Array<String>) {   
   println("Hello World!")        
}

然后我运行gradle build并获得了build\classes\main\hello\HelloKt.class

我的问题是:为什么生成的文件不是.class而不是.jar,以及如何获取.jar文件以及如何运行它,我尝试使用kotlin -classpath HelloKt.class main运行生成的文件,但出现错误

my question is: Why the file generated is .class not .jar and how to get the .jar file and how to run it, I tried running the generated file using kotlin -classpath HelloKt.class main but got an error error: could not find or load main class hello.main

推荐答案

感谢@hotkey的回答,它帮助我走了正确的路.

Thanks for @hotkey answer, it helped me going the correct way.

首先,在主类声明中有一个错误,因为它应该遵循新的方法,即以下格式:

First of all there is a mistake in the main class declaration, as it should follow the new methodology, that is in the below format:

mainClassName = '[your_namespace].[your_arctifact]Kt'

namespace =程序包名称

namespace = package name

arctifact =文件名

arctifact = file name

因此,考虑上例中给出的名称,其中filename为:Hello.kt,名称空间为hello,则:

so, considering the names given in the example above where filename is: Hello.kt, and the namespace is hello, then:

mainClassName = `[hello].[Hello]Kt`

使用先前的方法,其中包含:

using the previous method, that contains:

apply plugin: 'application'
mainClassName = 'hello.HelloKt'

生成的.jar文件不包含kotlin运行时,因此执行该文件的唯一方法是:

the generated .jar file is not including the kotlin runtime, so the only way to execute it, is by:

d:/App/build/libs/kotlin -cp App.jar hello.HelloKt

,但是为了生成可以自我执行并包含kotlin runtime的自我包含的jar,则build.gradle应该写为:

but in order to generate a self contained jar that can be self-executed, and contains the kotlin runtime then the build.gradle should be written as:

// set up the kotlin-gradle plugin
buildscript {
    ext.kotlin_version = '1.1.2-2'

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

// apply the kotlin-gradle plugin
apply plugin: "kotlin"


// add kotlin-stdlib dependencies.
repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

jar {
    manifest {
        //Define mainClassName as: '[your_namespace].[your_arctifact]Kt'
        attributes 'Main-Class': 'hello.HelloKt'
    }

    // NEW LINE HERE !!!
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

后跟gradle build,将在build/libs文件夹中生成[your_working_folder].jar文件,假设工作文件夹名为app,则将生成文件app.jar.

followed by gradle build, the [your_working_folder].jar file will be generated at the build/libs folder, assuming the working folder name is app, then file app.jar will be generated.

要运行此文件,可以使用以下两个命令之一:

To run this file, one of the following 2 commands can be used:

D:\App\build\libs\java -jar App.jar

OR

D:\App\build\libs\kotlin App.jar hello.HelloKt

这篇关于将Kotlin与Gradle结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆