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.
Apply the Application plugin and configure the main class
To package an application, you need to apply the Application plugin first:
Open the
build.gradle(.kts)
file of your project.Make sure that the file contains the following code:
plugins { application } application { mainClass.set("io.ktor.server.netty.EngineMain") }plugins { id 'application' } application { mainClass = 'io.ktor.server.netty.EngineMain' }The Application plugin is applied inside the
plugins
block.The
mainClass
property is used to configure the main class of the application. Note that the application main class depends on the way used to create a server. In the example above, the main class depends on the used engine and looks as follows:io.ktor.server.netty.EngineMain
.
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
installDist
task 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/<project_name>
folder.
Run the application
To run the packaged application:
Go to the
build/install/<project_name>/bin
folder in a terminal.Depending on your operating system, run the
<project_name>
or<project_name>.bat
executable, for example:./ktor-samplektor-sample.batWait until the following message is shown:
[main] INFO Application - Responding at http://0.0.0.0:8080Open the link in a browser to see a running application: