How I Fixed: Kotlin Spring Boot Auto Reload in IntelliJ IDEA

Coming from a world of JavaScript (and prior, PHP), I am used to my code automatically updating as soon as I save my project. Lately I’ve been playing around with Kotlin, and as of today, Kotlin with Spring Boot inside Intellij IDEA.

And what bugged the heck out of me was that the project needed a manual restart each and every time I made a change.

Now, I found several tutorials that explain how to fix this, but none of them actually solved my problem. So here’s what I had to do – and sadly it’s not absolutely perfect, but it is 95% of the way there.

As ever, if you know a better way, please do share by leaving a comment. Thanks!

The Stuff Everyone Else Says

Google for this problem and there’s a set of steps you will find in almost every result:

  1. Add the Spring Boot Dev Tools to your project dependencies. I am using Grade, so here’s what I had to do:
dependencies {
   implementation("org.springframework.boot:spring-boot-devtools")
   ... other stuff
}
  1. File > Settings… > Build, Execution, Deployment > Compiler > tick Build project automatically
  2. Press SHIFT+CTRL+A (Win/*nix) or Command+SHIFT+A (Mac) to open a pop-up windows, type registry
  3. Find and enabled this option compiler.automake.allow.when.app.running

So far, so good. All those steps are taken from here.

However, after doing this, my project didn’t start auto-restarting whenever I made a change.

The Extra Step

What I had to do was edit my project’s run/debug configuration:

And then in there, select to “update classes and resources” on update, and frame deactivation:

It’s slap bang in the middle, if you are missing it…

Once I’d done this, the Spring Boot app did begin to restart when I made a change to my, admittedly, very simple application.

I had this:

package demo

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@SpringBootApplication
class DemoApplication

fun main(args: Array<String>) {
   runApplication<DemoApplication>(*args)
}

data class Message(val id: String?, val text: String)

@RestController
class MessageResource {
   @GetMapping
   fun index(): List<Message> = listOf(
         Message("1", "Hello"),
         Message("2", "Dag"),
         Message("3", "Goedenavond"),
   )

}

And then I could add a fourth, fifth, sixth, etc entry to the list:

@GetMapping
fun index(): List<Message> = listOf(
      Message("1", "Hello"),
      Message("2", "Dag"),
      Message("3", "Goedenavond"),
      Message("4", "Chris"),
      Message("5", "Code Review Videos"),
)

And so why I say it’s not perfect is that yes, it does rebuild the project, but only when IntelliJ loses focus.

In other words, I need to manually switch to e.g. Postman and only then does the project rebuild. I find this a little strange, but it’s good enough.

My concerns would be that if you have a large Kotlin / Java project, this compilation step may take a while, and so that’s why this isn’t a thing by default(?).

Who knows. It’s fine for my “hello world” tier abilities. And also my PC is ridiculously OP anyway, so 🤷 right?

Published by

Code Review

CodeReviewVideos is a video training site helping software developers learn Symfony faster and easier.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.