如何创建Ktor嵌入式服务器的.jar(创建可执行文件) [英] How to create .jar (Create Executable) of the Ktor embedded Server
问题描述
BlogApp.kt :
包博客
导入org.jetbrains.ktor.netty。*
导入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)
}
main main(args:Array< String>){
embeddedServer(Netty,8080,watchPaths = listOf(BlogAppKt),module = Application :: module).start()
}
和 build.gradle :
gro '
version'1.0-SNAPSHOT'
buildscript {
ext.kotlin_version ='1.1.4-3'
repositories {
mavenCentral()
}
依赖关系{
classpathorg.jetbrains.kotlin:kotlin-gradle-plugin:$ kotlin_version
}
}
apply plugin:'java'
apply plugin:'kotlin'
sourceCompatibility = 1.8
ext.ktor_version ='0.4.0'
存储库{
mavenCentral()
maven {urlhttp://dl.bintray.com/kotlin/ktor}
maven {urlhttps:// dl。 bintray.com/kotlin/kotlinx}
}
依赖关系{
编译org.jetbrains.kotlin:kotlin-stdlib-jre8:$ kotlin_version
编译org.jetbrains.ktor:ktor-core:$ ktor_version
compileorg.jetbrains.ktor:ktor-netty:$ ktor_version
compilech.qos.logback:logback-classic:1.2 .1
testCompile组:'junit',名称:'junit',版本:'4.12'
}
compileKotlin {
kotlinOptions.jvmTarget =1.8
}
compileTestKotlin {
kotlinOptions.jvmTarget =1.8
}
kotlin {
experimental {
coroutinesenable
}
}
服务器正常运行: localhost:8080
我可以看到以下文件位于 out
文件夹中:
C:\ Users \ Home \IdeaProjects\Example\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'这台服务器,所以我可以分发给用户?
您需要做的是将以下代码片段添加到 build.gradle
文件中:
<$ p $
jar {
baseName'< jar_name>'
manifest {
属性'Main-Class':'blog.BlogAppKt'
}
来自{configurations.compile.collect {it.isDirectory()? it:zipTree(it)}}
}
然后运行: gradle clean jar
。完成后,导航到: build / libs
并运行 java -jar< jar_name> .jar
。
或者使用 javaw -jar< jar_name> .jar
代替java运行它(您可能需要确保它的开启如果JAVA_HOME没有被很好地定义,那么路径)。这将运行它没有任何控制台。
您还可以使用应用程序
插件或 shadowJar
。
I'm very new to Kotlin and Ktor and Gradle. Was able to create embedded server as explained in Ktor site, 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 run fine at: localhost:8080
and the I can see the below files in the out
folder:
C:\Users\Home\IdeaProjects\Example\out\production\classes\blog
How can I know create an executable file .jar of this server, so I can distribute it to the user?
What you need to do is to add the following piece of code to build.gradle
file:
jar {
baseName '<jar_name>'
manifest {
attributes 'Main-Class': 'blog.BlogAppKt'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
Then run: gradle clean jar
. After it's finished navigate to: build/libs
and run java -jar <jar_name>.jar
.
Or run it using javaw -jar <jar_name>.jar
instead of java (you might need to make sure its on the path if the JAVA_HOME is not defined well). This will run it without any console.
P.S. You can also use application
plugin or shadowJar
.
这篇关于如何创建Ktor嵌入式服务器的.jar(创建可执行文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!