2

In my Spring Boot project I have the following structure:

- src
  - main
    - java
    - resources
      - static
        - css
        - js
        - img
    - webapp
      - WEB-INF
          - views

According with the documentation at Excluding Resources, resources inside static/ should trigger a reload, but instead whenever I save a file inside css/ or js/ a full restart is triggered.

I'm using Spring Boot 2.0.4 including spring-boot-devtools (default configuration) and spring-boot-starter-security with Eclipse Oxygen and Tomcat 8.5

Packaging type is war since I have to deploy to a shared container and views are created using jsp.

Any help will be greatly appreciated.

Relevant files:

pom.xml:

<packaging>war</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-envers</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2.1-b03</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
</dependencies>

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

application.properties:

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

#spring.jpa.show-sql=true

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

spring.jpa.open-in-view=false
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158

2 Answers2

2

Finally found the solution. It was much simpler than I thought.

Since I'm using war packaging I can use webapp's default behaviour. Everything under source folders trigger a restart, but resources under webapp are fetched at runtime, so there is no need to reload anything.

I just placed my static content under /webapp/static and then added the resource handler:

@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/"); 
    }
}

Just don't use the default Spring Boot placement for statics (basically anything under resources) and you'll be fine.

  • 1
    I had a similar [problem](https://stackoverflow.com/q/52587818/6267583). Thanks for the remark with the source folder, it helped me to find a solution for my problem. – morecore Oct 30 '18 at 18:05
0

Did you also read the chapter for the "LiveReload"? You'll need to make sure that your LiveReload Server is started from the SpringBoot-Devtools. You can also use the browser plugin from LiveReload.com. Or you could use the Plugin from the Chrome Web Store. See Chapter LiveReload for more details.

the hand of NOD
  • 1,639
  • 1
  • 18
  • 30
  • I can't use a livereload server as I'm using jsp for the view. Therefor I can't use jar packaging, which I believe is required for livereload. However, as far as I know, livereload is only needed to update the browser in real time whenever the application is reloaded. What I need is to avoid the restart, not to trigger the refresh. – Ricardo Fernández Sep 04 '18 at 11:47
  • Hmmm. not sure if I understand correctly but you could try to set the livereload flag to false and see if your server itself supports hot code replacement. `spring.devtools.livereload.enabled=false` in your `application.properties` should do that. – the hand of NOD Sep 04 '18 at 12:54