Search code examples
spring-bootkotlindeploymentwar

Spring Boot: Is it possible to separate css, js and template files from .war?


I have a new spring boot kotlin application deployed to live server by uploading the .war file created by Maven, it is working properly now. However, it seems that the static contents(css, js and template files) are also compressed in this .war file, this creates some issues for me as if I want to change the css on the live server, I have to re-deploy the entire .war file.

So I wonder, is it possible to separate the css/js/template files from the .war file? This way I upload the .war file to the server, and the css/js/template files separately. If I want to change the css or js file, I dont have to redeploy. Thanks, plz lemme know how this is possible.


Solution

  • You should be able to register resource locations outside your WAR, in order for Spring Boot to serve them.

    For instance, you can serve all files from the /opt/files directory at the /files/<file_path_relative_to_optfiles_dir> URL this way:

    @Configuration
    @EnableWebMvc
    class MvcConfig : WebMvcConfigurer {
        override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
            registry
                .addResourceHandler("/files/**")
                .addResourceLocations("file:/opt/files/");
        }
    }
    

    You can have more explanations in this Baeldung tutorial (it's in Java but you can get the idea).