1

I am trying to figure out a way by which I could rename the static files I have in my web project, all of them at the same time.

Exactly what I need is to be able of filtering all my statics filenames to add a version number or something similar just to avoid them from being cached by browsers.

Something like converting custom.css into custom-1.23.css where 1.23 would be the value of a given filter.

This behaviour looks really similar to what resources plugin does with the content of the files, but I couldn't find a way of doing the same with the filenames.

Does anyone knows something similar?

Thanks a lot

Jesus Benito
  • 386
  • 3
  • 15

2 Answers2

2

1) Use any Maven plugin that is able to copy and rename resources. For example, copy-rename-maven-plugin. Configure the plugin to copy all static resources which you want to version and put them into a new directory inside the target directory, for example target/static_versioned:

<plugin>
    <groupId>com.coderplus.maven.plugins</groupId>
    <artifactId>copy-rename-maven-plugin</artifactId>
    <version>1.0.1</version>
    <executions>
        <execution>
            <id>copy-file</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>copy</goal>
            </goals>
            <configuration>
                <fileSets>
                    <fileSet>
                        <sourceFile>src/main/webapp/css/style.css</sourceFile>
                        <destinationFile>target/static_versioned/css/style_${project.version}.css</destinationFile>
                    </fileSet>
                    <fileSet>
                        <sourceFile>src/main/webapp/js/app.js</sourceFile>
                        <destinationFile>target/static_versioned/js/app_${project.version}.js</destinationFile>
                    </fileSet>
                </fileSets>
            </configuration>
        </execution>
    </executions>
</plugin>

2) Configure maven-war-plugin to add versioned static files inside you war file:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
        <webResources>
            <resource>
                <directory>target/static_versioned</directory>
            </resource>
        </webResources>
    </configuration>
</plugin>

3) Maven copies both versioned and original files into the result war file, so the original files need to be excluded:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
        <packagingExcludes>**/css/style.css,**/js/app.js</packagingExcludes>
        <webResources>
            <resource>
                <directory>target/static_versioned</directory>
            </resource>
        </webResources>
    </configuration>
</plugin>

It is worth mentioning that the target directory still has original and versioned files, but the war file contains only versioned files.

4) Use maven-war-plugin's filtering functionality to rename static resource links so all links point to versioned resources:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
        <packagingExcludes>**/css/style.css,**/js/app.js</packagingExcludes>
        <webResources>
            <resource>
                <directory>target/static_versioned</directory>
            </resource>
            <resource>
                <directory>src/main/webapp</directory>
                <filtering>true</filtering>
            </resource>
        </webResources>
    </configuration>
</plugin>

For example, a link like <script type="text/javascript" src="js/app_${project.version}.js"></script> will be renamed into <script type="text/javascript" src="js/app_1.0-SNAPSHOT.js"></script>

Complete example is here: https://github.com/dmitrysobolev/maven-war-plugin-js-versioning-example

Dmitry Sobolev
  • 927
  • 15
  • 18
2

You could change the directory they are served from rather than the file name e.g.

/static/${version}/custom.css

The resources plugin would let you change the target directory in the war.

artbristol
  • 32,010
  • 5
  • 70
  • 103