31

When building my first Spring Boot app, I discovered that I need to restart my app every time I make a change to a Javascript file in /resources/static/some-file.js

This is very time consuming.

When I move the files to the /webapps/ folder, it works as expected, but the files are not packaged in a JAR. The docs mention this, so I cannot continue this way.

I read all about spring-boot-devtools, and have installed it in my POM, but the hotswapping doesn't seam to be working. I still need to restart the entire app to see a Javascript or CSS file change.

When the app starts, I can see the line LiveReload server is running on port 35729, so spring-boot-devtools must be doing something...

What would be the steps to further investigate what is going wrong?

My guess is that when IntelliJ runs the app, the files are copied, so when I make a change, I'm actually changing the original files and not the ones used by the running app.

PS: I'm using Maven, IntelliJ IDEA 15 and Spring Boot 1.3.3 with the embedded Tomcat server. I'm not sure what command IntelliJ runs to start the app. The IDE is handling this "automatically". Perhaps I need to change my run configuration?

Patrick Grimard
  • 7,033
  • 8
  • 51
  • 68
Wouter
  • 1,678
  • 3
  • 20
  • 32
  • is it a spring boot project ? in spring boot, by default the src/main/resources/templates is the place where views live and not the webapps folder. please post the pom.xml – Balaji Krishnan Mar 09 '16 at 15:29
  • My files are in the right location. I only tried to move them when I noticed the hotswapping was not working. I returned the files to their original location after realising I could not keep the "webapps" folder. – Wouter Mar 09 '16 at 15:30
  • 1
    Good video how it works in IDEA: https://www.youtube.com/watch?v=VWF7vCJSqrA – Grigory Kislin Feb 24 '17 at 18:33
  • Possible duplicate of [spring boot hotswap with Intellij IDE](https://stackoverflow.com/questions/23155244/spring-boot-hotswap-with-intellij-ide) – Ruslan López Aug 16 '18 at 03:13
  • But after these two steps the ide is compiling even when I am not editing any files . – Aditya Yada Jan 22 '21 at 14:26

7 Answers7

91

You need to turn on a couple of features in IntelliJ to make this work.

First, there's a project specific setting which you would need to apply on any project you want to use devtools in. Go to Preferences > Compiler and enable "Make project automatically."

The next setting is an IDEA registry setting that applies to all projects.

  • In macOS (OSX), press Shift+Command+A (Shift+Ctrl+A in Windows)
  • Type "Registry" in the search box that appears, and select the registry to open it.
  • Lookup compiler.automake.allow.when.app.running and enable it.

After that, restart your app. You will notice that the project keeps rebuilding with every change you make. When you check out the result in the browser, you will see both static files and code have been updated.

dersvenhesse
  • 6,276
  • 2
  • 32
  • 53
Patrick Grimard
  • 7,033
  • 8
  • 51
  • 68
  • 5
    On Windows: Press Ctrl-Alt-Shift-/ and select "Registry" from the menu that appears. – Daniil Shevelev Mar 28 '16 at 12:04
  • 1
    Pretty nice. For me the ultimate solution would be that the webserver restart only happens when I save a source file, not automatically. Investigating this. – Jannik Dec 13 '16 at 21:09
  • Have a look at the devtools docs. I believe there's an option for that. – Patrick Grimard Dec 14 '16 at 00:57
  • 1
    For the current version of IntelliJ it is : Ctrl+Shift+A and write `Registry` – Anthony Granger Apr 14 '17 at 14:32
  • 1
    sadly this does not work if you use webpack and watch. in this case you have to go into the bundle.js file and edit it manually (i.e. by adding blanks somewhere) all the time ... – KIC Nov 13 '17 at 12:06
  • 1
    PS I am using the File Watchers Plugin now (using checksum as binary). This helps, but you still need to set focus to InteliJ in case you edit the js files in some other editor. – KIC Nov 14 '17 at 09:56
  • 8
    I am not seeing compiler.automake.allow.when.app.running in registry. I am using intellij community edition 2018.1. please help – Gundamaiah Aug 16 '18 at 11:29
  • in addition to **Patrick Grimard**'s answer: You need to restart IDEA! – copbint Aug 15 '20 at 14:12
  • 3
    @Gundamaiah you can find this option in: File --> Settings --> Advanced Settings. Enable the checkbox: "Allow auto-make to start even if developed application is currently running" in Compiler section – Fabio Ribeiro de Carvalho Oct 24 '21 at 11:54
16

For Windows users the steps are:
1) Go to File->Settings, then to "Build,Execution,Deployment"->Compiler and enable the "Make project automatically" flag.
2) Press Ctrl-Alt-Shift-/ and select "Registry" from the menu that appears. Enable compiler.automake.allow.when.app.running flag.
3) Start/restart the app and observe static content reloading.

Daniil Shevelev
  • 11,739
  • 12
  • 50
  • 73
  • 2
    I think it was renamed to "Build project automatically". I finally managed to solve this by following the instructions here: https://stackoverflow.com/questions/40057057/spring-boot-and-thymeleaf-hot-swap-templates-and-resources-once-again#answer-41933216 – Zbarcea Christian Oct 22 '18 at 20:18
12

For IntelliJ 2021.2 version or above below are steps that you need to follow

  1. Add spring-boot-devtools dependency if not already added.

  2. Enable Build project automatically as shown below

enter image description here

  1. Enable option in Advanced Settings as shown below

enter image description here

Ankit Arya
  • 872
  • 7
  • 10
  • This does not seem to work on `application.properites` changes. Is this solution suppose to work on static file as well? – Eric Huang Sep 19 '21 at 11:34
  • Yes, this solution works as intended. It works with changes to static files and even when changing the configuration properties file. The only difference with the accepted answer is that the option (compiler.automake.allow.when.app.running) is now available through the interface in modern versions of the IDE. – Alessandro Alessandra Mar 27 '23 at 15:49
2

On Linux, press:

Ctrl-Alt-Shift-/

Anatolii Stepaniuk
  • 2,585
  • 1
  • 18
  • 24
1

For mac users I had to press Command + Shift + A

And restart INTELLIJ after Step1 and Step2 without which the registry option was not appearing.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
raj vivid
  • 11
  • 1
1

You can user like bellow:

  1. First Open Preferences.../Settings... -> Build, Execution, Deployment -> Compiler and allow Build project automatically. Image will look like

enter image description here

  1. Second, we change Registry configuration. Press command+shift+A for macOS or if you are using Windows press Ctrl+Shift+A, and search for Registry. enable compiler.automake.allow.when.app.running.Images look like bellow

Registry image look like..

enter image description here

compiler.automake.allow.when.app.running enable image look like enter image description here

Then Enjoy..

Enamul Haque
  • 4,789
  • 1
  • 37
  • 50
0

The option is now in Advanced settings:

IntelliJ Advanced Setting

Deehaz05
  • 51
  • 1
  • 5
  • 1
    This seems to be a duplicate of [this other](https://stackoverflow.com/a/68786501/10952503) answer but with less informations – Elikill58 Jan 09 '22 at 15:23