Skip to content

创建应用程序分发

Ktor Gradle plugin 会自动应用 Gradle Application plugin,该插件提供了打包应用程序的能力,包括代码依赖项和生成的启动脚本。在本主题中,我们将向您展示如何打包和运行 Ktor 应用程序。

配置 Ktor 插件

要创建应用程序分发,您需要首先应用 Ktor 插件:

  1. 打开 build.gradle.kts 文件并将插件添加到 plugins 代码块中:

    kotlin
    plugins {
        id("io.ktor.plugin") version "3.2.3"
    }
  2. 确保已配置主应用程序类

    kotlin
    application {
        mainClass.set("com.example.ApplicationKt")
    }

打包应用程序

Application plugin 提供了各种打包应用程序的方式,例如,installDist 任务可以安装带有所有运行时依赖项和启动脚本的应用程序。要创建完整的应用程序分发归档,您可以使用 distZipdistTar 任务。

在本主题中,我们将使用 installDist

  1. 打开终端。

  2. 根据您的操作系统,以下列出一种方式运行 installDist 任务:

    Application plugin 将在 build/install/<project_name> 文件夹中创建应用程序镜像。

运行应用程序

要运行已打包的应用程序

  1. 在终端中进入 build/install/<project_name>/bin 文件夹。

  2. 根据您的操作系统,运行 <project_name><project_name>.bat 可执行文件,例如:

  3. 等待显示以下消息:

    Bash
    [main] INFO  Application - Responding at http://0.0.0.0:8080

    在浏览器中打开链接以查看正在运行的应用程序:

    浏览器中的 Ktor 应用