Ktor 1.6.8 Help

Gradle Application plugin

The Gradle Application plugin provides the ability to package applications, including code dependencies and generated start scripts. In this topic, we'll show you how to package and run a Ktor application created in the Adding Ktor to an existing Gradle project topic.

Prerequisites

Before starting this tutorial, clone the ktor-gradle-sample repository.

Apply the Application plugin and configure the main class

To package an application, you need to apply the Application plugin first:

  1. Open the ktor-gradle-sample project.

  2. Make sure that the build.gradle file contains the following code:

    plugins { id 'application' } application { mainClass = 'com.example.ApplicationKt' }
    • The id 'application' line applies the Application plugin.

    • The mainClass property is used to configure the main class of the application.

Package the application

The Application plugin provides various ways for packaging the application, for example, the installDist task installs the application with all runtime dependencies and start scripts. To create full distribution archives, you can use the distZip and distTar tasks.

In this topic, we'll use installDist:

  1. Open a terminal.

  2. Run the installDist task in one of the following ways depending on your operating system:

    ./gradlew installDist
    gradlew.bat installDist

    Gradle will create an image of the application in the build/install/ktor-gradle-sample folder.

Run the application

To run the packaged application:

  1. Go to the build/install/ktor-gradle-sample folder in a terminal.

  2. Depending on your operating system, run the ktor-gradle-sample or ktor-gradle-sample.bat executable:

    ./ktor-gradle-sample
    ktor-gradle-sample.bat
  3. Wait until the following message is shown:

    [main] INFO Application - Responding at http://0.0.0.0:8080 You can click the link to open the application in a default browser:

    Ktor app in a browser
Last modified: 06 September 2021