6

When using Spring without the Spring boot technology, I can start the application with the Tomcat Maven plugin and update my html, css and js without having to restart the server for the changes to take effect in the browser.

Now, when doing this with Spring boot, I will have to do a Make task in IntelliJ everytime I change something in my static assets, otherwise I just wont see the changes in browser. My Spring Boot application is also using the devtools dependency, and I have the browser connected to the live reload socket when looking.

This configuration still does not show the changes I make in my static files. What do I need to do to get this working?

Mike
  • 522
  • 2
  • 14
Kaspar
  • 1,600
  • 4
  • 24
  • 46

3 Answers3

9

From my experience static assets in webapp dir are not available if you build jar without war plugin. So i would avoid using this dir. With Spring Boot it is better to use resources/static folder for your static assets. But you want them to get reloaded while executing Gradle's bootRun goal or Maven's spring-boot:run.

To achieve this use the following settings for Gradle:

bootRun {
    addResources = true
}

and for Maven:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <fork>true</fork>
        <addResources>true</addResources>
    </configuration>
</plugin>
Kirill
  • 6,762
  • 4
  • 51
  • 81
  • I have those settings you describe above but are still having to CTRL-F9 and the refresh the browser to get the changes to html loaded. Anything else I might have to do to avoid CTRL-F9 ? – Al Grant Apr 13 '17 at 19:18
  • @AlGrant pressing CTRL+S should be enough – Kirill Apr 14 '17 at 17:13
  • I followed the technique here: http://stackoverflow.com/questions/43402551/intellijspringbootthymeleafgradle-autoreload-html-resources note that I had to retstart Intellij after the registry setting - it now reloads auto-magically. – Al Grant Apr 14 '17 at 18:46
1

It seems that the problem was in the placement of my static assets. I had to put them into a webapp folder under the main package. I was following a guide from Spring.io before that advised to put the static assets into resources package instead.

Now that I stopped using the resources package, everything works as i want it to.

Kaspar
  • 1,600
  • 4
  • 24
  • 46
  • I'm glad you found a solution. I find it strange that you had the static resource in the `resources ` folder, and it still didn't pick up the change. I'm 99% sure I read that it would in that documentation... Oh well. – Mike Dec 06 '15 at 04:35
  • I was having this same issue. Were you able to get this working for your html files too? This fixed it for my js/css when I moved them to src/main/webapp/[css|js] but when I moved my templates to src/main/webapp/templates, my controllers could not find the templates. Any help? – user2923779 Dec 23 '15 at 06:23
  • 2
    When running my application with spring-boot I also had to place the static files in `resources`. If I placed them in the `webapp` directory they would not be included in the JAR file that I was building. With spring-boot there is no need to build a WAR file anymore so it seems like `resources` is the preferred location for static content. LiveReload only worked when I Make the Project (CMD+F9) in IntelliJ15. – Miguel Reyes Jan 01 '16 at 21:31
0

If you follow the current Spring Boot 1.3.0.RELEASE documentation, it looks like it describes exactly how to solve the problem you're having. It describes using IntelliJ and Maven together too.

So maybe check that out and see if you're following the documented path first?

You should ensure you're using the correct Maven dependency to before following along in the docs (which are listed in the documentation page I've linked).

Mike
  • 522
  • 2
  • 14
  • That guide suggests using the hotswapping technology. But I am not the interested in hotswapping java files as I am about reloading static assets. Also the guide mentioned nothing about users who do not use any templating at all. All I have is regular .html files. Worth mentioning that I do have the springloaded plugin defined in Maven now and it seems to make no difference. – Kaspar Dec 05 '15 at 14:09
  • Yes, fair enough. I looked around a little more. Have you read [section 20.2 on Automatic Restarts](https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html), more specifically the info box titled *Triggering a change*. I work with Eclipse, so this section on IntelliJ might make more sense to you. – Mike Dec 05 '15 at 14:47
  • There's also another section on the [exclusion of static resources (Section 20.2.1)](https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html#using-boot-devtools-restart-exclude). It might also be helpful. And if all else fails, I think that section right below that on [Watching Additional Paths](https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html#using-boot-devtools-restart-additional-paths) might fix your issue. – Mike Dec 05 '15 at 14:51
  • Still seems to end up in a way that I need to press Ctrl + F9 to see changes even if I follow all the guides. Just not sure how I don't have to do it for an old Spring project that is using mostly .jsp files and some javascript. There it's all automatic and I also use it with Intellij – Kaspar Dec 05 '15 at 15:14