ktor生成可独立发布jar包(通过shadow打包)
2019-04-17 本文已影响0人
蓝不蓝编程
ktor简要介绍
如何打包
开发出了版本,除了本地运行,再就是要打包发布.ktor也支持几种打包方式,如war、jar.本文介绍如何生成jar包.
操作步骤
一、build.gradle配置:
- repositories增加
classpath 'com.github.jengelman.gradle.plugins:shadow:5.0.0'
- 添加mainClassName,指向启动类(如果已存在mainClassName,就修改其值)
mainClassName = 'com.cxyzy.ktor.demo.ApplicationKt'
- 添加shadowJar,其中baseName表示jar包名称前缀.
shadowJar {
baseName = 'ktor-sample'
classifier = null
version = '1.0.0'
}
- 完整样例:
buildscript {
repositories {
jcenter()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:5.0.0'
}
}
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'kotlin'
apply plugin: 'application'
mainClassName = 'com.cxyzy.ktor.demo.ApplicationKt'
shadowJar {
baseName = 'ktor-sample'
classifier = null
version = '1.0.0'
}
- 编译
gradle build
或./gradlew build
-
查看生成jar包
目录: build/libs
image - 运行
java -jar ktor-sample-1.0.0.jar
妥妥的启动啦,哈哈
2019-04-17 19:09:33.735 [main] DEBUG Application - Java Home: /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home
2019-04-17 19:09:33.739 [main] DEBUG Application - Class Loader: sun.misc.Launcher$AppClassLoader@55f96302: [/Users/jerry/projects/mine/github/server/ktor-kmongo-sample/build/libs/ktor-sample-1.0.0.jar, /System/Library/Java/Extensions/MRJToolkit.jar]
2019-04-17 19:09:33.741 [main] INFO Application - No ktor.deployment.watch patterns match classpath entries, automatic reload is not active
2019-04-17 19:09:34.131 [main] INFO Application - Responding at http://0.0.0.0:8080
2019-04-17 19:09:34.131 [main] INFO Application - Application started: io.ktor.application.Application@5a709816
点击关注专辑,查看最新技术分享
更多技术总结好文,请关注:「程序园中猿」