Building a self-executable jar with Gradle and Kotlin

前端 未结 7 1462
囚心锁ツ
囚心锁ツ 2020-12-08 18:40

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 ja

7条回答
  •  没有蜡笔的小新
    2020-12-08 19:03

    Add the plugin application, then set the mainClassName as

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

    For instance, suppose you have placed the following code in a file named main.kt:

    package net.mydomain.kotlinlearn
    
    import kotlin
    import java.util.ArrayList
    
    fun main(args: Array) {
    
        println("Hello!")
    
    }
    

    your build.gradle should be:

    apply plugin: 'kotlin'
    apply plugin: 'application'
    
    mainClassName = "net.mydomain.kotlinlearn.MainKt"
    

    In fact Kotlin is building a class to encapsulate your main function named with the same name of your file - with Title Case.

提交回复
热议问题