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

Thu*_*fir 9 build gradle kotlin build.gradle gradle-kotlin-dsl

最简单的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$ 
Run Code Online (Sandbox Code Playgroud)

为简洁起见,请参阅项目本身,该项目本身仅包含构建文件和kotlin脚本.

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

kur*_*urt 11

你的意思是胖子吗?如果是这样,您可以使用shadow gradle插件:

id 'com.github.johnrengelman.shadow' version '2.0.2'
Run Code Online (Sandbox Code Playgroud)

如果你想使罐子可执行文件,你还需要添加Main-class体现(例如下面主要方法文件Applicaion.kt与包test):

jar {
    manifest {
        attributes 'Main-Class': 'test.ApplicationKt'
    }
}
Run Code Online (Sandbox Code Playgroud)

有了这个你可以jar用命令运行:java -jar <your 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")
}
Run Code Online (Sandbox Code Playgroud)

档案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 Code Online (Sandbox Code Playgroud)

运行命令: gradle shadowJar

并在尝试运行jar之后: java -jar build/libs/shadow_test-all.jar

更新2018-02-17

build.gradle.kts 文件版本:

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")
}
Run Code Online (Sandbox Code Playgroud)

  • 更新答案并添加`build.gradle.kts`示例 (6认同)
  • 谢谢,非常清楚。但是,我正在寻找“build.gradle.kts”文件,而不是上面的“build.gradle”文件。 (2认同)