92

I have a spring boot application running fine with Intellij IDE. i.e i started the Application class that has the main method which delegates to SpringApplication.run. Everything works great except hotswap. When I change the source, I am forced to re-start the application. Even If I start the application in debug mode, I dont see hotswap working. I could see that Intellij's Debug settings have hotswap enabled.

My observation shows that when I run the springboot application, classpath used is my

/projects/MyProject/classes/production/....

Files under classes/production are not getting updated when I change the code. Intellij IDE compiles the files but does not update classes/production directory. How do I get hotswap working with IntelliJ IDE for spring-boot?

suman j
  • 6,710
  • 11
  • 58
  • 109
  • check this out http://stackoverflow.com/questions/21399586/hot-swapping-in-spring-boot/43114954#43114954 – shabby Mar 30 '17 at 10:20

10 Answers10

120

A solution that uses devTools works :

1 - Adding devtools to your project

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

2 - Enabling automatic build

Open the Settings --> Build-Execution-Deployment --> Compiler and enable :

Build Project Automatically.

3 - Update the value of compiler.automake.allow.when.app.running

press ctrl+shift+A and search for the registry. In the registry, enable :

compiler.automake.allow.when.app.running

References :

starball
  • 20,030
  • 7
  • 43
  • 238
Radouane ROUFID
  • 10,595
  • 9
  • 42
  • 80
  • 8
    Does this not only restart SpringBoot, as when you do it manually? Takes the same amount of time during restart? – powder366 Nov 08 '17 at 15:27
  • 4
    `compiler.automake.allow.when.app.running` will restart every time when any file is changed. It took too long time – Gank Jan 17 '18 at 10:42
  • I tried this and it did not work for me. Is it perhaps b/c i have multiple maven projects in my Intellij Project? If so, I'm not even sure how to solve that b/c the "Build Project Automatically." setting is seemingly a global Intellij setting – Ron Stevenson May 01 '18 at 18:50
  • The last step (updating `compiler.automake.allow.when.app.running`) is not needed at least with IntelliJ Ultimate edition. – Ibrahim.H Nov 28 '18 at 17:47
  • 2
    The app only restarts when I close the file, and all spring boot is restarted, it's not a true hot reload... am I missing something? I'm using idea Ultimate – Johnny Willer Jan 15 '19 at 04:50
  • The last step is to restart IntelliJ to make it work. – Sunil Kumar May 01 '19 at 07:14
  • In 2019.2 the "Build project automatically" setting have a note "(only works when not running/debugging)". It appears that automatically hotswapping must be enabled in the debug launch configuration. – Thorbjørn Ravn Andersen Aug 12 '19 at 13:20
  • Don't forget to disable auto-save – eicksl Nov 02 '19 at 00:28
  • @ThorbjørnRavnAndersen where can I enable the hotswapping. I checked the launch configuration, but I did not find it. Is it a launch command parameter or something? – Jaanus Apr 13 '20 at 11:13
  • @Jaanus open a new question – Thorbjørn Ravn Andersen Apr 14 '20 at 16:20
  • 10
    In 2021.2 instead of step 3 enable *Settings --> Advanced Settings --> Allow auto-make to start even if developed application is currently running* – maechler Aug 26 '21 at 14:30
  • 1
    Just a helpful note for macOS users. if cmd+shift+a doesn't work, you can try shift+shift which should open the dialog box and there you can search for Registry. – anuraagdjain Sep 04 '21 at 16:16
  • Thanks @maechler this solved my issue. – WISERDIVISOR Nov 15 '21 at 10:37
  • As of version 2022.2 see [Reload modified classes](https://www.jetbrains.com/help/idea/2022.2/altering-the-program-s-execution-flow.html#reload_classes). And for the IntelliJ [Ultimate Edition](https://www.jetbrains.com/idea/buy/#commercial) only _[Spring Boot run configuration](https://www.jetbrains.com/help/idea/2022.2/run-debug-configuration-spring-boot.html#modify-options) → Modify option → On 'Update' action_ – cachius Aug 25 '22 at 14:50
43

Found out the root cause. This has nothing to do with Spring-boot. On changing my groovy source files, files were not auto-compiled.

To recompile changed files and swap them:

  • Ctrl+Shift+F9 on Windows
  • Cmd+Shift+F9 on Mac
eleven
  • 6,779
  • 2
  • 32
  • 52
suman j
  • 6,710
  • 11
  • 58
  • 109
  • How to diable restart project every time when one file is changed – Gank May 03 '18 at 09:09
  • You can use a trigger file. See: https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html#using-boot-devtools-restart-triggerfile – Lee Marlow Nov 01 '18 at 17:16
  • 1
    For the skeptical, [the keymap](https://resources.jetbrains.com/storage/products/intellij-idea/docs/IntelliJIDEA_ReferenceCard.pdf) lists this shortcut as, "Compile selected file, package or module". – Brad Turek May 20 '20 at 20:45
  • 1
    Among all these not working answers, this is the only useful! – Zony Zhao Nov 02 '21 at 12:54
30

Step 1: Add developer tools denpendency

Maven.

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

Gradle.

dependencies {
    compile("org.springframework.boot:spring-boot-devtools")
}

Step 2: Go to File | Settings | Build, Execution, Deployment | Compiler

enable Build project automatically & Apply & OK

enter image description here

Step 3: Press shortcut key Ctrl+Shift+A & Search Registry keyword & Press Enter

enter image description here

Enable complier.automake.allow.when.app.running & Click Close Button

enter image description here

Step 4: Disable cache on your favorite web browser

enter image description here

Step 5: Done!!!

Watch Solution On YouTube

Solution

duyuanchao
  • 3,863
  • 1
  • 25
  • 16
  • If your Action Window close immediately from second time after you typed `Registry` in Mac version, you could hit `Shift` button twice to pop the Action Window again. – Yao Li Nov 22 '19 at 01:21
  • Thank you @shellhub – Pasha Mar 18 '21 at 17:09
  • 3
    Additionally after 2021.2 version, complier.automake.allow.when.app.running option was moved to Advanced settings. Preferences -> Advanced Settings -> Compiler -> Allow Automake ..... so on... – iceberg Oct 27 '21 at 06:19
  • Couldn't find this in 2021.3.3 CE – RBz Jul 24 '22 at 03:00
  • https://youtrack.jetbrains.com/issue/IDEA-274903/In-IntelliJ-2021.2-compiler.automake.allow.when.app.running-disappear.-Unable-to-enable-live-reload-under-Spring-boot – Razkar Apr 28 '23 at 16:44
15

In my case even after adding the Spring Boot dev tools and checking the build project automatically, it was not working. What was missing was this:

  1. Go to the project run configuration.

  2. Set on'Update' action and 'On frame deactivation' both to 'Update classes and resources'. And it worked like a charm.

enter image description here

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
Sameer Khanal
  • 1,199
  • 12
  • 11
  • Hi, I cannot find 'Update' and 'On frame deactivation' in my Intellij. Can you help? – RichArt Jul 13 '18 at 10:46
  • 3
    This is exactly why I wrote a comment: I don't see any on 'Update' Action and on Frame Deactivation options when clicking on Run -> Edit Configurations. (I am on Intellij 2018.1.6) – RichArt Jul 15 '18 at 09:14
  • Could you send a screenshot or a link to it when you click on Edit Configurations? That would make it easier to see what is going on. – Sameer Khanal Jul 18 '18 at 01:39
  • 3
    @RichArt I was having same issue. Unfortunately, the run config been spoken about is for intellij spring-boot support - only available in ultimate edition. – Laurence Jul 21 '18 at 15:19
  • @BinaryMonkL: I am working on the Ultimate edition. Do I miss something else? – RichArt Jul 22 '18 at 15:13
  • @RichArt you need to configure it as a spring app and select the spring boot run config from left tab – Laurence Jul 22 '18 at 15:21
  • I would go with @BinaryMonkL 's suggestion. You will need to make sure that your project is configured as a Spring App. Did you create this project using Spring Initializr? That is the best way to make sure Intellij brings in tools to support Spring Boot. – Sameer Khanal Jul 24 '18 at 00:51
  • Did the trick for me. Is there a way to set this configuration as the default for all projects? – eicksl Nov 02 '19 at 17:51
9

Use spring-loaded. It works fine with bean reloading. It's free alternative to JRebel.

Another way is to use DCEVM or hotswapagent

Michal Zmuda
  • 5,381
  • 3
  • 43
  • 39
  • It's perfect except the Mybatis mapper.xml, after changing myapper.xml's sql code, the reload not work – Gank May 04 '18 at 09:51
5

I noticed the same problem as well. I am on the Intellij 13.1.1. and for it to hotswap the changed class. I have to run the app in debug mode, then after changing the class, select "Run->Reload Changed Classes" manually.

I also have the debugger set to hotswap as indicated in http://www.jetbrains.com/idea/webhelp/reloading-classes.html but doesn't seem to work.

Anyway, at least the manual "Run->Reload Changed Classes" menu works.

user3206144
  • 606
  • 4
  • 5
3

@Sameer Khanal's answer worked but since so much time has passed, I offer some clarification

  1. Install the spring boot devtools dependencies (https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools)

  2. Modify your run option Edit running configuration

  3. Update the options for On 'Update' action and On frame deactivation like so: enter image description here

  4. Click on OK and you are done

Hope that helps

Dũng Trần Trung
  • 6,198
  • 3
  • 24
  • 20
2

For multiple changes, use "Compile And Reload File"(Add "spring-boot-devtools" first). Double click "Shift" to show the diagram in Mac.

Or try spring-loaded which I haven't tried yet. But seems it could work. enter image description here

Levin
  • 298
  • 3
  • 8
0

Add the deployment artifact exploded, in the deployment tab, this did the trick for me.

-1

You can use either of the following methods to auto reload Thymeleaf templates:

  1. Spring Boot Dev Tools
  2. Change Thymeleaf Templates Path
  3. Gulp Watch

I recommend Gulp watch as it is easier to setup and works great:

var gulp = require('gulp'), 
watch = require('gulp-watch');

gulp.task('watch', function () {
    return watch('src/main/resources/**/*.*', () => {
            gulp.src('src/main/resources/**')
                //replace with build/resources/main/ for netBeans
                .pipe(gulp.dest('out/production/resources/')); 
    });
});

gulp.task('default', ['watch']);

Now enter the following command in your terminal to start gulp watch:

$ gulp 
//OR
$ gulp watch

I wrote a blog post on auto reloading Thymeleaf templates without restart in IntelliJ IDE.

attacomsian
  • 2,667
  • 23
  • 24