Ktor 2.3.11 Help


Restarting a server during development might take some time. Ktor allows you to overcome this limitation by using Auto-reload, which reloads application classes on code changes and provides a fast feedback loop. To use Auto-reload, follow the steps below:

  1. Enable the development mode

  2. (Optional) Configure watch paths

  3. Enable recompiling on changes

Enable the development mode

To use Auto-reload, you need to enable the development mode first. This depends on the way you used to create and run a server:

  • If you use EngineMain to run a server, enable the development mode in a configuration file.

  • If you run a server using embeddedServer, you can use the io.ktor.development system property.

When development mode is enabled, Ktor will be watching output files from the working directory automatically. If required, you can narrow down a set of watched folders by specifying watch paths.

Configure watch paths

When you enable development mode, Ktor starts watching output files from the working directory. For example, for a ktor-sample project build with Gradle the following folders will be watched:

ktor-sample/build/classes/kotlin/main/META-INF ktor-sample/build/classes/kotlin/main/com/example ktor-sample/build/classes/kotlin/main/com ktor-sample/build/classes/kotlin/main ktor-sample/build/resources/main

Watch paths allow you to narrow down a set of watched folders. To do this, you can specify a part of a watched path. For example, to monitor changes in the ktor-sample/build/classes subfolders, pass classes as a watch path. Depending on the way you use to run a server, you can specify a watch path in the following ways:

  • In the application.conf or application.yaml file, specify the watch option:

    ktor { development = true deployment { watch = [ classes ] } }
    ktor: development: true deployment: watch: - classes

    You can also specify several watch paths, for example:

    watch = [ classes, resources ]
    watch: - classes - resources

    You can find the full example here: autoreload-engine-main.

  • If you are using embeddedServer, pass a watch path as the watchPaths parameter:

    embeddedServer(Netty, port = 8080, watchPaths = listOf("classes")) { routing { get("/") { call.respondText("Hello, world!") } } }.start(wait = true)

    You can find the full example here: autoreload-embedded-server.

Recompile on changes

Since Auto-reload detects changes in output files, you need to rebuild a project. You can do this manually in IntelliJ IDEA or enable continuous build execution in Gradle using the -t command-line option.

  • To rebuild a project manually in IntelliJ IDEA, select Build | Rebuild Project from the main menu.

  • To rebuild a project automatically using Gradle, you can run the build task with the -t option in a terminal:

    ./gradlew -t build
Last modified: 02 April 2024