Auto-reload
Restarting a server during development might take some time. Ktor allows you to overcome this limitation by using the Auto-reload feature, which reloads application classes on code changes and provides a fast feedback loop.
Enable Auto-reload
To use the Auto-reload feature, 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 development mode in the application.conf 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.
Watch specific 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:
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, specify the
watch
option:ktor { development = true deployment { watch = [ classes ] } }You can specify several watch paths separated by a comma, for example:
watch = [ classes, resources ]You can find the full example here: autoreload-engine-main.
If you are using
embeddedServer
, pass a watch path as thewatchPaths
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 the Auto-reload feature 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
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