Lord Yggdrasill
Lord Yggdrasill

Reputation: 3397

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.

Upvotes: 1

Views: 229

Answers (2)

Joffrey
Joffrey

Reputation: 37720

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).

Upvotes: 1

Yasen
Yasen

Reputation: 4474

You can use external resources configuration

If you don't want a file to be part of the war package, just put a simple configuration to exclude it:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>**/*.css</exclude>
            </excludes>
        </resource>
    </resources>
</build>

Upvotes: 0

Related Questions