111

Having issues with the new LiveReload feature with Spring Boot devtools 1.3. It doesn't reload on class changes. I've seen it demo'd with IntelliJ @ Devoxx 2015. Is there some IDE setting I need to have enabled? I'm running via the IDE and not through Gradle. I tried enabling "Make project automatically" which doesn't seem to help.

It seems to load correctly and is looking in the correct path

2015-11-23 05:55:30.592 DEBUG 4700 --- [  restartedMain] o.s.boot.devtools.restart.Restarter      : Starting application com.myapp.Application with URLs [file:/E:/Projects/myapp/build/classes/main/, file:/E:/Projects/myapp/build/resources/main/]

My files

build.gradle

buildscript {
    ext {
        springBootVersion = '1.3.0.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    }
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'idea'
apply plugin: 'spring-boot' 
apply plugin: 'war'


war {
    baseName = 'myapp'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
}

configurations {
    providedRuntime
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-devtools')
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile('org.projectlombok:lombok')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('net.sourceforge.jtds:jtds:1.3.1');
    testCompile('org.springframework.boot:spring-boot-starter-test') 
}

dependencyManagement {
    imports { 
        mavenBom "org.springframework.cloud:spring-cloud-starter-parent:Brixton.M3" 
    }
}


eclipse {
    classpath {
         containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
         containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.7'
}

HelloWorldController

@Controller
public class HelloWorldController {

    @RequestMapping("/")
    @ResponseBody
    String home(){

        return "Hello World test";
    }
}
Timm-ah
  • 1,276
  • 2
  • 10
  • 9
  • 3
    The gist of the answers: This feature restarts the server when the .class files in the target/ folder change, not when you just change the source .java files. There is a way to force intellij to update the .class files on the classpath when you save a changed source code file. However, this feature was intended to be used with eclipse ide which does this automatically. – masterxilo Apr 15 '19 at 20:31
  • for me i updated in STS [check that build automatically] go to windows -> preferences -> Build – Harshal Biradar Nov 30 '20 at 15:19
  • I found a strange variation of this; livereload worked like a charm for some pages but not others, with the *Firefox* version of the plugin. Switching to Chrome and it worked perfectly for all pages. A bug in the Firefox extension perhaps? – Chris Huang-Leaver Mar 25 '21 at 01:23

24 Answers24

106

if you use IntelliJ IDEA, adding the spring-boot-devtools is not enough. This is because unlike Eclipse, you need to explicitly tell IntelliJ IDEA to “Make The Project” for it to build to the target classpath.

see on youtube

The easiest solution: run app debug mode and press Ctrl + f9 (short-cut for build)

or

  1. You need to enable the “Make project automatically” option. You can find it in Settings – Build, Execution, Deployment – Compiler
  2. To open the registry, Press Ctrl-Alt-Shift-/ and select "Registry" from the menu that appears, enable the “compiler.automake.allow.when.app.running” check-box.

build-project-automatically

compiler-automake-allow-when-app-running

LonIslam
  • 15
  • 4
i.karayel
  • 4,377
  • 2
  • 23
  • 27
  • 27
    After intelliJ 2021, this setting has moved to advanced settings. `File -> Settings... -> Advanced Settings` then check `Allow auto-make to start ...` – Aven Desta Feb 18 '22 at 22:25
  • In 2022.1.3 It will under preferences, Short cut to open preference window Command + , ( In Mac) – sujith s Aug 09 '22 at 05:56
91

To solve this You can do like:

  1. Add LiveReload extension in your browser.
  2. Add devtools dependencies to your pom.xml(if it's maven (spring-boot-devtools)).
  3. In your intellij IDEA go to: file->settings->build,execution,deployment. Go to ->compiler->build project automatically.
  4. In your intellij IDEA: SHIFT+Ctrl+A ->registry-> compiler.automake.allow.when.app.running
vinga
  • 1,912
  • 20
  • 33
Eddy Bayonne
  • 2,448
  • 1
  • 17
  • 23
  • 8
    For the fourth point, after intelliJ 2021, this setting has moved to advanced settings. `File -> Settings... -> Advanced Settings` then check `Allow auto-make to start ...` – Junaid Jul 01 '22 at 11:04
60

I am guessing you are coding and expect DevTools to auto-magically figure out you have changed something in your project? Devtools does that by watching your classpath folder(s). When a .class file (or a resource) has changed, devtools take the appopriate action.

If you don't see anything, that's probably because you're just coding and not updating the classpath. You have to invoke Make Project to update the classpath. See the documentation

Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89
  • 1
    Trying to understand the following "Intellij users will need to remover to use the Build → Make Project action to achieve the same result." Maybe this is meant to say "remember" rather than "remover"? Did you guys setup a macro or something to make your project automatically? IntelliJ supports this only if the app is not running... I've seen others needing to create a macro and tie it to "CTRL+S" actions. – Timm-ah Nov 23 '15 at 15:07
  • it was a typo that [got fixed on master already](http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-devtools-restart). Make Project is just one shortcut. If you feel that "CTRL-S" is better for you, do that. But it's essentially exactly the same thing. I really don't buy the "make automatically" approach but that might just be me. – Stephane Nicoll Nov 23 '15 at 17:08
  • 8
    Okay but what about static assets. Why would I need to invoke a Build for them? – Kaspar Dec 05 '15 at 19:15
  • 4
    @Kaspar - thank you for asking this. I am having to manually invoke a build in order to see changes to a static html file. To me, that's not a great workflow. – arcseldon May 22 '16 at 04:07
  • I agree. In this question: http://stackoverflow.com/questions/34104664/livereload-for-assets-in-intellij-using-spring-boot I mentioned that when I moved my static assets out of resources folder then the problem was actually fixed. You could try it also. – Kaspar Jun 11 '16 at 14:47
  • I'm using idea, when I modify a html file in static folder, I have to rebuilder project(command + shift + f9) explicitly. – zhuguowei Mar 09 '17 at 12:53
  • Yes, you need the changes to appear on the classpath. In IDEA, you can build the whole project (Build > Build Project), but also you can simply recompile the file you changed (Build > Recompile 'my.file'). – ocarlsen Feb 15 '19 at 04:41
  • To answer the question that was asked above, your static assets are read from the classpath as well so that shouldn't make a single difference. When you run your application, the static assets is not read from where it resides in your project. Since then there is better support for Devtools in IntelliJ IDEA anyway. – Stephane Nicoll Feb 15 '19 at 10:59
31

In IntelliJ 2021.2 compiler.automake.allow.when.app.running option dissappeared. This option was moved to Advanced settings: enter image description here

17

LiveReload and restart are different features. Livereload allows you to detect changes in resources/static folder and notify browser that files changed and the page should be reloaded.

Your question describes Restart scenario. If you want your application to reload automatically on *.class-files changes, make sure your IDE outoputs compiled classes to:

build\classes\main

In Intellij go to Project Structure (Ctrl+Alt+Shift+S) and setup project compiler output dir. After this you can make changes in your classes and press Ctrl+Shift+F9 to recompile current class or hit Ctrl+F9 to recompile the whole project. Spring Boot Devtools will detect changes in build\classes\main and perform restart of your application automatically.

If you want to enable LiveReload for your static assets add the following (otherwise you won't see cahnges of static content while executing bootRun goal):

bootRun {
    addResources = true
}
Kirill
  • 6,762
  • 4
  • 51
  • 81
  • 2
    That bootRun configuration has changed to `sourceResources sourceSets.main` in the meantime: https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/ – Alexander Abramov Nov 18 '18 at 17:35
10

Follow below simple steps, you will be up and running in less than 2 minutes.

  1. Press Ctrl+Shift+A
  2. Search for Registry ...
  3. scroll and find "compiler.automake.allow.when.app.running" then select the checkbox to make it active.
  4. Click close
  5. File Menu -> settings ...
  6. Expand Build, Execution, Deployment
  7. Select Compiler
  8. Select checkbox Build project automatically
  9. Apply
  10. Click Ok

Now stop your application server and then start your application, that's it you will find automatic restart/reload activated when any changes are detected in the project.

Happy Coding/Hacking.

Santhosh_Reddy
  • 274
  • 4
  • 18
9

Live reload is a different feature than the problem you have asked the solution for. In your case as you want to reload your classes after changing them, you need to follow the following 3 steps-

  1. Make change in compiler settings enter image description here
  2. Make changes in the registry enter image description here
  3. Make changes in the run/debug configuration enter image description here

That's it! After modifying your classes, you can simply press ctrl+F10 to reload the modified classes. You are good to go then!

Addtionally, if you want to configure single point of reload upon modification: https://www.logicbig.com/tutorials/spring-framework/spring-boot/trigger-file.html


For Live Reload: When a Spring Boot application is running in Intellij IDEA, the templates are served from out/production/resources/templates directory. You can change this behavior and serve the templates directly from src/main/resources/templates directory in development mode. Create a file application-dev.yml in src/main/resources directory and paste the following code snippet in it:

spring:
    # Templates reloading during development
    thymeleaf:
        prefix: file:src/main/resources/templates/
        cache: false

    # Static resources reloading during development
    resources:
      static-locations: file:src/main/resources/static/
      cache-period: 0

To load the above properties, you need to activate and set the default Spring Boot profile to dev. Add the following property to your application.yml file:

spring:
    profiles:
      active: dev

Start your application. Now whenever you make any changes in your html files, all you need is to refresh the browser to see the changes!

Caffeine Coder
  • 948
  • 14
  • 17
8

From the documentation here

As DevTools monitors classpath resources, the only way to trigger a restart is to update the classpath. The way in which you cause the classpath to be updated depends on the IDE that you are using. In Eclipse, saving a modified file will cause the classpath to be updated and trigger a restart. In IntelliJ IDEA, building the project (Build → Make Project) will have the same effect.

Ankush92
  • 401
  • 1
  • 9
  • 20
7

Before anything you need to know devtools is reloading pretty much everything under src/main/ path not the changing to pom.xml file.

1- Add Spring Boot devtools dependency to your maven pom.xml file.

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

2- Restart the project and reload maven.

3- Add live reload extension for your browser from http://livereload.com/extensions/

4- On IntelliJ idea 2021+ under File->Setting->Build, Execution, Deployment->Compiler: check build project automatically

5- On IntelliJ idea 2021+ under File->Setting->Advance Setting: check allow auto-make to start even if developed application is currently running

6- change something under src/main (it could be java code, template, or anything else) other than method signature, parameters, or something like this. For example, change a simple line of your code or change a String literal or some stylesheet in the template.

4- You can skip lines (3-4-5-6) and rebuild the current file by Ctrt+shift+f9 or rebuild the whole project by Ctrl_f9 or if you did the preceding lines and you don't want to even press Ctrt+shift+f9, you don't need to rebuild just enjoy the result on the browser.

5- Enjoy the result on the browser.

  • thank you :). this workaround worked for intellij. and i got to know this feature is for eclipse and not for intellij. – ahrooran Dec 16 '21 at 11:17
6

intellij 2021.2 has moved this option to AdvancedSettings:

Advanced Settings

Draken
  • 3,134
  • 13
  • 34
  • 54
  • 1
    This is correct answer for latest version of IntelliJ. This is only solution which worked for me. Thanks @Leonardo Moura – Manjeet Jan 16 '22 at 20:42
5

For me,

I update to the latest version and in File -> Setting > Advance setting and check

enter image description here

Samba
  • 176
  • 2
  • 7
4

To answer the above question let us first understand a feature in Intellij Idea. You must have seen that the file that you make change need not be saved. And the change that you make in any non .class file remains saved. To cross verify please make some change to the .java file(you can choose any other file as well) and close it and it won't prompt for Saving it and when you reopen the file then your changes are still there. But if you check the .class file the changes made are not reflected which is obvious because build has not happened. On the contrary when we enable automatic build in Eclipse it is done on an event and the event is Saving the File -> Ctrl+S(Windows). But in intellij idea that event is not happening or we do not carry out that event. So, consider the scenario when we do not have spring-boot-devtool then, in that case, we always had to rebuild or restart the server to pick up the changes thereby causing an event. Now, regarding spring-boot-devtool as Ankush92 rightly mentioned that devtool monitors the classpath changes and restart happens only if there is any change in the classpath. But as I explained earlier we are just adding our code changes in the non .class file and expecting devtool to trigger a restart even when that change is not reflected in the class path. Let me mention this again that in eclipse the event to do the same is Saving the File(Ctrl+S) when automatic build is enabled. So, now the question is how to have a workaround and mimic what Ctrl+S does in Eclipse into the Intellij Idea. It's quite simple lets build the application and the shortcut for that is Ctrl+F9. This will trigger the same effect in intellij as Ctrl+S(in this scenario) in eclipse and help the devtool to find the change in the classpath thereby encouraging it to restart the server. So everytime you make any change in the file and want the server to restart just press Ctr+F9. I hope this explanation and the workaround helps.

Saket Kumar
  • 113
  • 1
  • 9
2
Edit - Macros - Start Macro Recording
File - Save All
Build - Build Project
Edit - Macros - Stop Macro Recording - You can save the macro name as “Save & Make Project”
Preferences - Keymap - Macros

Go down to expand the macros directory to find your newly macro (i.e. “Save & Make Project”). Double click to Add Keyboard Shortcut and press Cmd+S if you use Mac and Ctrl+S if you use Windows.

Intellij will prompt saying that Ctrl+S already exist just click 'Replace'.

Now all set and Ctrl+S should trigger Spring-Boot Devtools

Reference: https://tomyjaya.github.io/2016/10/08/spring-boot-dev-tools-in-intellij/

Janardhan
  • 366
  • 4
  • 9
2

In IntelliJ IDEA 2021.2, compiler.automake.allow.when.app.running option is disappeared.

So follow these steps,

  1. File ⇒ Settings ⇒ Build, Execution, Deployment ⇒ Compiler ⇒ Build project automatically
  2. Advanced Settings ⇒ Allow auto-make to start even if developed application is currently running
  3. Add this line to the application.properties file ⇒ spring.devtools.restart.enabled=true
2

I am using IntelliJ idea 2021.2.2(ultimate edition).

I followed these steps,

File -> Settings -> Build, Execution, Deployment-> Compiler add tick to Build project automatically checkbox

Next, Press Ctrl+Shift+A Search for Registry ... scroll and find "compiler.automake.allow.when.app.running" then I can't find that.

then I reserch about that I find In IntelliJ 2021.2 compiler.automake.allow.when.app.running option dissappeared. This option was moved to Advanced settings:

Advanced settings

enter image description here

Sathiamoorthy
  • 8,831
  • 9
  • 65
  • 77
Sajith dilshan
  • 111
  • 1
  • 2
  • 9
1

I discovered that IntelliJ wasn't even using my Gradle configuration.

Visiting Build, Execution, Deployment > Build Tools > Gradle then under Delegate Settings I selected Build and Run Using and made it Gradle.

Working like a charm

enter image description here

For reference, here is my build configuration:

plugins {
    id 'org.springframework.boot' version '2.1.6.RELEASE'
    id 'java'
    id 'idea'
}

apply plugin: 'io.spring.dependency-management'

group = 'test'
version = '0.0.1'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

configurations {
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
}

This works well instead of:

bootRun {
    addResources = true // Did not work for me
}
sparkyspider
  • 13,195
  • 10
  • 89
  • 133
1

apply the following steps:

  1. Add LiveReload extension in your browser. link: https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei

  2. Add devtools dependencies to your pom.xml(for maven) or build.gradle (for gradle)

for maven:

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

for gradle

     compileOnly("org.springframework.boot:spring-boot-devtools")
  1. file->settings->build,execution,deployment. Go to ->compiler->build project automatically.

  2. Ctrl+Shift+A ->registry-> compiler.automake.allow.when.app.running

  3. Ctrl+Shift+A ->Edit Configurations->Spring Boot->your application ->

enter image description here

0

I've been facing this problem a lot on last months on my MAC. Even when following these steps listed here like checking "Make Project Automatically" and "Compiler Automake".

However at my Job, where we use Windows, it works perfectly when I do follow these steps.

The solution for this problem on a MAC I found though is to add a new Run Configuration (Press ⌃⌥R, then press 0) then add a new Maven configuration. On the "Command line" input set "spring-boot:run". Press "Apply" and "Ok" and run the project by selecting the new configuration created.

Of course you could just open a new terminal and just type "mvn spring-boot:run" it will work too.

0

This worked for me. Add the below line in application.properties file,

    spring.devtools.restart.enabled=true
0

If LiveReload worked for you in the past and then suddenly stops working, and if the other proposed solutions don't work, try uninstalling and reinstalling the LiveReload Browser Plugin. That's what finally fixed it for me.

Chris
  • 4,212
  • 5
  • 37
  • 52
0

After finishing all the settings above, you should recompile the html file that you want to reload!
Build → Recompile 'yourHtmlFile.html' (Ctrl+Shift+F9 in Windows)

Yoonju Lee
  • 21
  • 3
0

File -> Settings -> Build, Execution, Deployment-> Compiler add tick to Build project automatically checkbox

Ctrl+ Shift+alt+/ -> Select registry add tick to compiler.automate.allow.when.app.running

User
  • 1,460
  • 14
  • 11
0

IntelliJ IDEA > preferences > Tools > Advance Settings > Compiler > check the box to allow auto-make to start even if developed application is currently running.

Add the following dependency on the appropriate pom.xml.

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

Make the changes that you want to make while the server is running.

Build > Build Project

Reload the browser

0

If its Maven and you want dev-tools then check if in your pom.xml, the dependency has been imported by Intelli J properly. In my case it was showing in red so I did a Maven refresh and that resolved my issue after following other suggestions of Registry from other comments.

Keegs
  • 460
  • 6
  • 12