spring boot hotswap with Intellij IDE
Asked Answered
D

10

95

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?

Druse answered 18/4, 2014 at 13:32 Comment(1)
check this out #21400086Glengarry
D
46

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
Druse answered 18/4, 2014 at 13:44 Comment(4)
How to diable restart project every time when one file is changedDeane
You can use a trigger file. See: docs.spring.io/spring-boot/docs/current/reference/html/…Loydloydie
For the skeptical, the keymap lists this shortcut as, "Compile selected file, package or module".Bickford
Among all these not working answers, this is the only useful!Remarque
R
121

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 :

Restless answered 21/5, 2017 at 9:52 Comment(14)
Does this not only restart SpringBoot, as when you do it manually? Takes the same amount of time during restart?Prophylaxis
compiler.automake.allow.when.app.running will restart every time when any file is changed. It took too long timeDeane
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 settingLucania
The last step (updating compiler.automake.allow.when.app.running) is not needed at least with IntelliJ Ultimate edition.Verger
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 UltimateWaterproof
The last step is to restart IntelliJ to make it work.Auxiliaries
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.Couch
Don't forget to disable auto-saveRadmen
@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?Capone
@Capone open a new questionCouch
In 2021.2 instead of step 3 enable Settings --> Advanced Settings --> Allow auto-make to start even if developed application is currently runningSoldier
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.Pointillism
Thanks @Soldier this solved my issue.Marchand
As of version 2022.2 see Reload modified classes. And for the IntelliJ Ultimate Edition only Spring Boot run configuration → Modify option → On 'Update' actionScintillator
D
46

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
Druse answered 18/4, 2014 at 13:44 Comment(4)
How to diable restart project every time when one file is changedDeane
You can use a trigger file. See: docs.spring.io/spring-boot/docs/current/reference/html/…Loydloydie
For the skeptical, the keymap lists this shortcut as, "Compile selected file, package or module".Bickford
Among all these not working answers, this is the only useful!Remarque
A
32

Step 1: Add developer tools denpendency

Maven

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

or ..

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 compiler.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

Aircrewman answered 29/8, 2018 at 6:28 Comment(5)
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.Turoff
Thank you @shellhubSpherule
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...Tamayo
Couldn't find this in 2021.3.3 CEWanhsien
youtrack.jetbrains.com/issue/IDEA-274903/…Graeae
S
18

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

Sunbow answered 13/5, 2018 at 4:47 Comment(8)
Hi, I cannot find 'Update' and 'On frame deactivation' in my Intellij. Can you help?Cline
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)Cline
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.Sunbow
@Cline I was having same issue. Unfortunately, the run config been spoken about is for intellij spring-boot support - only available in ultimate edition.Bugs
@BinaryMonkL: I am working on the Ultimate edition. Do I miss something else?Cline
@Cline you need to configure it as a spring app and select the spring boot run config from left tabBugs
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.Sunbow
Did the trick for me. Is there a way to set this configuration as the default for all projects?Radmen
L
9

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

Another way is to use DCEVM or hotswapagent

Lame answered 27/6, 2014 at 8:29 Comment(1)
It's perfect except the Mybatis mapper.xml, after changing myapper.xml's sql code, the reload not workDeane
F
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.

Forty answered 18/4, 2014 at 17:52 Comment(0)
S
5

@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

Stichomythia answered 13/7, 2022 at 8:42 Comment(1)
Works only in ultimate edition.Wanhsien
K
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

Kemp answered 16/1, 2021 at 7:37 Comment(0)
L
0

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

Loment answered 13/7, 2022 at 0:46 Comment(0)
D
-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.

Dinodinoflagellate answered 1/5, 2019 at 20:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.