4

I am using Spring Boot and Thymeleaf via Maven. I can't seem to get Netbeans to automatically re-deploy any of my Thymeleaf template files when I make changes. In order to see the changes I need to do a full clean/build/run. This takes way too long.

The templates are in src/main/resources/templates. I have an application.properties file in src/main/resources/ with spring.thymeleaf.cache=false and spring.template.cache=false.

I have "Compile on save", "Copy resources on save" and "Deploy on save" turned on in the project settings.

My maven build produces a war file that Netbeans deploys to Tomcat and I am using the annotation @EnableAutoConfiguration.

Netbeans does hot deploy changes to the Java classes but not for any of the static files in src/main/resources/.

Software in use:

  • Mac OS X 10.9.4
  • Java 1.8
  • Netbeans 8.0.1
  • Tomcat 8.0.12
  • Spring Boot 1.1.7
  • Thymeleaf 2.1.3 (via Spring Boot)

Any guidance is much appreciated.

5
  • are the files in resource root with filtering turned on? These are not copied by netbeans afaik.
    – mkleint
    Sep 29, 2014 at 21:35
  • I haven't used Netbeans in a while, but just to be sure, are you simply running or debugging the project?
    – geoand
    Sep 30, 2014 at 5:54
  • @mkleint - what do you mean by filtering turned on? The static files are in src/main/resources.
    – Hamish
    Sep 30, 2014 at 8:47
  • @geoand - it does not make a difference if I debug or run normally. Either case Netbeans does hot-reloading of Java files but not of the static files in src/main/resources.
    – Hamish
    Sep 30, 2014 at 8:49
  • <resource> element in pom can have a subelement <filtering> that basically means resources are not plainly copied but also processed (more or less simple templating). netbeans won't copy such resource files.
    – mkleint
    Sep 30, 2014 at 22:38

7 Answers 7

5

An option would be to look into configuring Thymeleaf's FileTemplateResolver

To do that with Spring Boot, define a bean implementing the ITemplateResolver interface with the name defaultTemplateResolver, when present, Spring Boot would take it instead of its default, here is how that would be done, and assuming you have component scanning active so this configuration class will be picked up automatically:

@Configuration
public class ThymeleafConfiguration {
  @Bean
  public ITemplateResolver defaultTemplateResolver() {
    TemplateResolver resolver = new FileTemplateResolver();
    resolver.setSuffix(".html");
    resolver.setPrefix("path/to/your/templates");
    resolver.setTemplateMode("HTML5");
    resolver.setCharacterEncoding("UTF-8");
    resolver.setCacheable(false);
    return resolver;
  }
}

The prefix should be a relative path that when added to your runtime working directory (cwd), would resolve to the templates directory. If you are unsure, set that to the full absolute path, but then there would be no point of the above bean. Since setting the spring.thymeleaf.prefix property to an absolute path would probably have the same effect.

1

Have been looking for a solution to my eclipse+thymeleaf+sprint boot reloading templates dynamically for a log while....

Finally I found this question here and spring.thymeleaf.cache=false and spring.template.cache=false fixed my issue.

1
  • I just spent 30 minutes fighting the same problem... it looks like there are some old blogs which point at the property spring.template.cache. Even there's an issue on the spring boot github site about the same thing :S
    – Augusto
    Mar 21, 2015 at 20:47
1

Besides setting the Thymeleaf views as non-cacheable by ie. spring.thymeleaf.cache=false in your application.properties, try explicitly defining the resource directory in your pom.xml:

<build>      
    ...
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    ...
</build>
1
  • This worked flawlessly for me. Only needed to add the "resources" declaration. No need to make changes to the application.properties.
    – egalot
    Mar 15, 2018 at 20:29
1

To deal with that, the spring-boot-maven-plugin in the pom.xml should seems like this:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>1.2.0.RELEASE</version>
        </dependency>
    </dependencies>
</plugin>

And add this to your application properties:

spring.thymeleaf.cache=false

It usually works to Spring beans too.

0

Just to say this works nicely for me using an external instance of Tomcat:

  • Run Tomcat with JRebel or Spring Loaded javaagent as a VM option
  • Turn off "Compile on save", "Copy resources on save" and "Deploy on save"
  • Add a custom action in Netbeans that executes the compile goal
  • Run that when you want to see an update

http://wiki.netbeans.org/MavenBestPractices#Binding_Maven_goals_to_IDE_actions

https://github.com/spring-projects/spring-loaded

https://zeroturnaround.com/software/jrebel/quickstart/standalone/

Or you can you use the embedded tomcat with spring-boot-maven-plugin and Spring Loaded instead, then you won't need the compile action:

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-hotswapping.html

0

I had this problem too. I notice Netbeans reload automaticaly webpages that are in

/src/main/webapp/

You have to move all your templates from /src/main/resources/templates to this directory.

Also you have to change the spring boot property on application.properties file:

spring.thymeleaf.prefix=templates/

That works for me

0

I had this same issue on Netbeans 8.0.2 and Windows. I was building a WAR to be deployed to Tomcat, but I wanted to try out Spring Boot. It looks like newer versions of Netbeans might resolve this with the Spring Boot plugin or using Eclipse. It seemed nutty to swap IDEs over something tiny like this. I tried all of the suggestions I could find; spring loaded, caching properties, extending the TemplateResolver...I couldn't get any of them to work. I finally stumbled on this blog and following these instructions solved my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.