Refreshing static content with Spring MVC and Boot

Solution 1:

A recap of the original problem

I've run into the problem that when I make modifications to my static content (html, js, css), I have to restart the application every time

I had the same problem and finally solved it by adding

<configuration>
    <addResources>true</addResources>
</configuration>

to spring-boot-maven-plugin in the pom.xml I got confused by this spring-boot-devtools thing, but it had no effect whatever I did.

My static content is stored in "src/main/resources/public" folder.

Your path is just fine. src/main/resources/static is also fine.

Solution 2:

Ah ... I came across this issue too.

Instead of putting your static content in the classpath src/main/resources/public folder, put them in src/main/webapp, the same as you would any other Java web app. The embedded Tomcat will automatically reload them whenever they change.

As mentioned in the comments, the default configuration will not include the resources that are in src\main\webapp. To get around this issue, you can just add the following to your pom.xml <build> node:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/target/classes/static</outputDirectory>
                <resources>
                    <resource>
                        <directory>src/main/webapp</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

By using the resources plugin, you are able to do your local development by running the executable JAR:

java -jar target/.jar

While that is running you can use Chrome Dev Tools or whatever IDE you like for modifying the files, without restarts. However, whenever you run your build, then the package generated will include all of the files under src\main\webapp in src\main\resources\static.