21

It should be possible to have a Spring Boot app continuously build (i.e. hot reload) by running gradle build --continuous and gradle bootRun in sequence.

I'm trying to modify the bootRun task in the gradle build file so that it calls the build task in continuous mode, but I can't seem to add arguments to it.

bootRun.dependsOn build

How can I get that build to run continuously?

3 Answers3

34

This question and the corresponding answers are pretty interesting.

Short answer : you can't have the bootRun task running with the continuous option (if your app stays alive indefinitely)

But there is a hack by Stefan Crain :

To get it to live reload you need to have 2 terminals open.

  1. gradle build --continuous

    • build --continuous will keep satisfying the initial build request until stopped
    • gradle build --continuous --quiet & 2>1 >/dev/null runs in the background, but you would miss the important build warnings/errors. gradle --stop to stop watching.
  2. gradle bootRun

    • bootrun starts with spring-boot-devtools on classpath, which will detect changes and restart application.

I think it's what you are looking for.

ToYonos
  • 16,469
  • 2
  • 54
  • 70
  • 3
    FYI `gradle build --continuous -xtest` skips `test` task which I think would be the expectation in this answer. – Ivar Dec 19 '19 at 11:47
  • 1
    For me using the `build` task as a "sentinel" didn't work, as during the restart bootRun couldn't find the main class name anymore. I've replaced it with the `bootRunMainClassName` task, which is the task `bootRun` directly depends upon and that also exits upon completion. This also makes sure that upon changes all the tasks `bootRun` depends have a chance to run and update. – Stefano Zanella Apr 14 '22 at 11:32
  • 2
    As 2022 you can just do `gradle bootRun --continuous` – Emmy Steven Jul 17 '22 at 14:45
4

To get hot reload using Gradle in Spring boot first you need dependency(if you are using Gradle Kotlin) Put this into build.gradle

developmentOnly("org.springframework.boot:spring-boot-devtools")

Then you have to open terminal and run command

gradle -t :bootJar

and then open a new terminal and run command

gradle bootRun

This will solve the problem for hot reload.

Ali Sasoli
  • 51
  • 2
-1

Another option to Toyonos solution seems to work for me, run the commands in two separate terminals to maintain the build warning messages:

  • gradle bootRun
  • gradle build --continuous
gunslingor
  • 1,358
  • 12
  • 34