How to create .jar (Create Executable) Ktor Embedded Server

I am very new to Kotlin and Ktor and Gradle. An embedded server was created, as described on the Ktor website, with the following code:

BlogApp.kt

package blog

import org.jetbrains.ktor.netty.*
import org.jetbrains.ktor.routing.*
import org.jetbrains.ktor.application.*
import org.jetbrains.ktor.features.*
import org.jetbrains.ktor.host.*
import org.jetbrains.ktor.http.*
import org.jetbrains.ktor.response.*

fun Application.module() {
    install(DefaultHeaders)
    install(CallLogging)
    install(Routing) {
        get("/") {
            call.respondText("My Example Blog  sfs 122", ContentType.Text.Html)
        }
    }
}

fun main(args: Array<String>) {
    embeddedServer(Netty, 8080, watchPaths = listOf("BlogAppKt"), module = Application::module).start()
}

and build.gradle :

group 'Example'
version '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.1.4-3'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'java'
apply plugin: 'kotlin'

sourceCompatibility = 1.8
ext.ktor_version = '0.4.0'

repositories {
    mavenCentral()
    maven { url  "http://dl.bintray.com/kotlin/ktor" }
    maven { url "https://dl.bintray.com/kotlin/kotlinx" }
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
    compile "org.jetbrains.ktor:ktor-core:$ktor_version"
    compile "org.jetbrains.ktor:ktor-netty:$ktor_version"
    compile "ch.qos.logback:logback-classic:1.2.1"
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
kotlin {
    experimental {
        coroutines "enable"
    }
}

The server is working fine: localhost:8080

and I see the files below in the folder out:

C:\Users\Home\IdeaProjects\Example\out\production\classes\blog

How do I know how to create this server’s .jar executable so that I can distribute it to the user?

enter image description here

+4
source share
1 answer

What you need to do is add the following code snippet to the file build.gradle:

jar {
    baseName '<jar_name>'
    manifest {
        attributes 'Main-Class': 'blog.BlogAppKt'
    }

    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

Then run: gradle clean jar. When done, go to: build/libsand run java -jar <jar_name>.jar.

javaw -jar <jar_name>.jar java ( , , JAVA_HOME ). .

P.S. application shadowJar.

+5

Source: https://habr.com/ru/post/1686210/


All Articles