Simplest possible Kotlin hello world for gradle script Kotlin:

thufir@dur:~/github/gradleScriptKotlin$ 
thufir@dur:~/github/gradleScriptKotlin$ gradle clean shadowJar;java -jar build/libs/gradleScriptKotlin.jar 

> Task :compileKotlin 
Using Kotlin incremental compilation

> Task :shadowJar 
A problem was found with the configuration of task ':shadowJar'. Registering invalid inputs and outputs via TaskInputs and TaskOutputs methods has been deprecated and is scheduled to be removed in Gradle 5.0.
 - No value has been specified for property 'mainClassName'.
The SimpleWorkResult type has been deprecated and is scheduled to be removed in Gradle 5.0. Please use WorkResults.didWork() instead.


BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed
Hello gradle script Kotlin world!
thufir@dur:~/github/gradleScriptKotlin$ 

For the sake of brevity please refer to the project itself which only really consists of the build file and a kotlin script.

How to build a runnable ShadowJar with a gradle script Kotlin build file?

推荐答案

你是说法特加吗?

id 'com.github.johnrengelman.shadow' version '2.0.2'

如果您想使jar可执行,还需要将Main-class添加到 list 中(下面是Applicaion.kt文件包test中main方法的示例):

jar {
    manifest {
        attributes 'Main-Class': 'test.ApplicationKt'
    }
}

有了这个,你可以用命令java -jar <your jar>运行jar

下面我有一个简单的例子.文件build.gradle:

plugins {
    id 'com.github.johnrengelman.shadow' version '2.0.2'
    id "org.jetbrains.kotlin.jvm" version "1.2.21"
}

repositories {
    jcenter()
}

jar {
    manifest {
        attributes 'Main-Class': 'test.ApplicationKt'
    }
}

dependencies {
    compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    compile("org.simpleframework:simple-xml:2.5")
}

文件test.Application.kt:

package test

import org.simpleframework.xml.Element
import org.simpleframework.xml.ElementList
import org.simpleframework.xml.Root
import org.simpleframework.xml.core.Persister

private val testXml = """
<feed>
   <entry>
        <id> someid </id>
        <published> somedate </published>
   </entry>

   <entry>
        <id> someid2 </id>
        <published> somedate2 </published>
   </entry>
</feed>
""".trimIndent()

@Root(name = "feed", strict = false)
data class Feed(
        @field:ElementList(name = "entry", inline = true)
        var entriesList: List<Entry>? = null
)

@Root(name = "entry", strict = true)
data class Entry(
        @field:Element(name = "id")
        var id: String? = null,

        @field:Element(name = "published")
        var published: String? = null
)

fun main(args: Array<String>) {
    println(testXml)

    val serializer = Persister()

    val example = serializer.read(Feed::class.java, testXml)

    println(example)
}

Run command: gradle shadowJar

And after try to run jar: java -jar build/libs/shadow_test-all.jar

Update 2018-02-17

build.gradle.kts version of file:

import org.jetbrains.kotlin.gradle.dsl.Coroutines
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.2.21"
    id("com.github.johnrengelman.shadow") version "2.0.2"
}

repositories {
    jcenter()
}

tasks.withType<Jar> {
    manifest {
        attributes(mapOf(
                "Main-Class" to "test.ApplicationKt"
        ))
    }
}

dependencies {
    compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    compile("org.simpleframework:simple-xml:2.5")
}

Kotlin相关问答推荐

在Kotlin中将ClosedRange字符串转换为List?<>

Gradle Jooq配置自定义生成器

在构造函数中创建内部类实例时,只能使用包含类的接收器调用内部类的构造函数

创建包含 3 个相同项目的列表/使用返回类型重复

区分函数和扩展

如果不在可组合函数中,如何获取 stringResource

Kotlin 条件格式字符串

关于 Kotlin 函数类型转换的问题

如何为你的 Flutter 元素添加 Kotlin 支持?

Android 在将 androidx 生物识别更新为 1.0.0-alpha04 后崩溃

从列表中的每个对象中 Select 属性

如何使用 gradle 脚本 Kotlin 构建文件构建可运行的 ShadowJar?

如何用 kotlin 打包 List

无法在 kotlin android 中以编程方式禁用 EditText

使用 Kotlin 创建新目录,Mkdir() 不起作用

哪里可以找到aapt2日志(log)?

Jetpack Compose State:修改类属性

Kotlin 与 C# 中的标志枚举变量具有相似效果的方式是什么

Kotlin中的属性(properties)和参数(parameters)有什么区别?

Kotlin - 如何获取注释属性值