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:
Open the ktor-gradle-sample project.
Make sure that the
build.gradlefile contains the following code:plugins { id 'application' } application { mainClass = 'com.example.ApplicationKt' }The
id 'application'line applies the Application plugin.The
mainClassproperty 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:
Open a terminal.
Run the
installDisttask in one of the following ways depending on your operating system:./gradlew installDistgradlew.bat installDistGradle will create an image of the application in the
build/install/ktor-gradle-samplefolder.
Run the application
To run the packaged application:
Go to the
build/install/ktor-gradle-samplefolder in a terminal.Depending on your operating system, run the
ktor-gradle-sampleorktor-gradle-sample.batexecutable:./ktor-gradle-samplektor-gradle-sample.batWait until the following message is shown:
[main] INFO Application - Responding at http://0.0.0.0:8080You can click the link to open the application in a default browser: