Gradle + Kotlin-Generate jar with DSL

I'm using Gradle with Kotlin-DSL, but I couldn't find a way to generate an executable jar, so I'll write it down as a memorandum.

build.gradle.kts


plugins {
    application
}

dependencies {
    //To jar the dependent libraries'compile'Must be described in

    // Use the Kotlin JDK 8 standard library.
    compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

    // Use the Kotlin test library.
    testCompile("org.jetbrains.kotlin:kotlin-test")

    // Use the Kotlin JUnit integration.
    testCompile("org.jetbrains.kotlin:kotlin-test-junit")
}

application {
    // Define the main class for the application
    mainClassName = "com.example.MainKt"
}

// 'gradle jar'Define a task so that you can use
val jar by tasks.getting(Jar::class) {
    manifest {
        attributes["Main-Class"] = "com.example.MainKt"
    }

    from(
            configurations.compile.get().map {
                if (it.isDirectory) it else zipTree(it)
            }
    )
    exclude("META-INF/*.RSA", "META-INF/*.SF", "META-INF/*.DSA")
}

I can find a lot of groovy articles ...

[11/4 postscript] Although it deviates from the purpose of this article, I will also write a method to give run-time arguments at the time of ./gradlew run execution.

build.gradle.kts


import kotlin.text.Regex

val run by tasks.getting(JavaExec::class) {
    if (project.hasProperty("args")) {
        args = (project.property("args") as String).split(Regex("\\s+))
    }
}

At runtime

./gradlew run -Pargs="hoge fuga piyo ..."

Note that the arguments do not complete

Recommended Posts

Gradle + Kotlin-Generate jar with DSL
Use ProGuard with Gradle
Integration Test with Gradle
Install Gradle with ubuntu16.04
DataNucleus starting with Gradle
Use WebJars with Gradle
Get started with Gradle
Use jlink with gradle
Java multi-project creation with Gradle
Lombok not working with Gradle5
Remote debugging with Gradle test
Generate a single executable jar with dependent libraries in Gradle 4.9
Use log4j2 with YAML + Gradle
Hello World with SpringBoot / Gradle
Build a Java project with Gradle
I tried using JOOQ with Gradle
Output test coverage with clover + gradle
I can't install lombok with Gradle.
Try using Redis with Java (jar)
Make multi-project executable JAR in Gradle
Make fat Jar with Minecraft modding
Develop Processing with IntelliJ + Kotlin + Gradle
Spring Boot gradle build with Docker
How to create a server executable JAR and WAR with Spring gradle