SoFunction
Updated on 2025-03-02

Use maven project file configuration packaging function and static resource file version number function

Maven project file configuration packaging function and static resource file version number function

For example:

Reference js file in html file

<script src="js/jquery-2.0."></script>

After packaging, it will become

<script src="js/jquery-2.0.?v=2021-07-09T09:29:42Z"></script>

Added in the tag

&lt;build&gt;
        &lt;plugins&gt;
        	&lt;!--Configure Packaging start--&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
            &lt;/plugin&gt;
            &lt;!--Configure Packaging end--&gt;
            &lt;!--Configure the page file with its own version number start--&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;-replacer-plugin&lt;/groupId&gt;
                &lt;artifactId&gt;replacer&lt;/artifactId&gt;
                &lt;version&gt;1.5.3&lt;/version&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;phase&gt;prepare-package&lt;/phase&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;replace&lt;/goal&gt;
                        &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
                &lt;configuration&gt;
                    &lt;includes&gt;
                        &lt;include&gt;${basedir}/target/classes/templates/**/*.html&lt;/include&gt;
                    &lt;/includes&gt;
                    &lt;replacements&gt;
                        &lt;replacement&gt;
                            &lt;token&gt;\.js\"&lt;/token&gt;
                            &lt;value&gt;.js?v=${}\"&lt;/value&gt;
                        &lt;/replacement&gt;
                        &lt;replacement&gt;
                            &lt;token&gt;\.js\'&lt;/token&gt;
                            &lt;value&gt;.js?v=${}\'&lt;/value&gt;
                        &lt;/replacement&gt;
                        &lt;replacement&gt;
                            &lt;token&gt;\.css\"&lt;/token&gt;
                            &lt;value&gt;.css?v=${}\"&lt;/value&gt;
                        &lt;/replacement&gt;
                        &lt;replacement&gt;
                            &lt;token&gt;\.css\'&lt;/token&gt;
                            &lt;value&gt;.css?v=${}\'&lt;/value&gt;
                        &lt;/replacement&gt;
                    &lt;/replacements&gt;

                &lt;/configuration&gt;
            &lt;/plugin&gt;
            &lt;!--Configure the page file with its own version number end--&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

Maven contains resource static file when packaged

&lt;build&gt;
        &lt;!-- mavenIncludes static resource files when packaged --&gt;
        &lt;resources&gt;

            &lt;resource&gt;
                &lt;directory&gt;src/main/resources&lt;/directory&gt;
                &lt;includes&gt;
                    &lt;include&gt;**/*.properties&lt;/include&gt;
                    &lt;include&gt;**/*.yaml&lt;/include&gt;
                    &lt;include&gt;META-INF/**&lt;/include&gt;
                    &lt;include&gt;**/*.xml&lt;/include&gt;
                &lt;/includes&gt;
                &lt;filtering&gt;true&lt;/filtering&gt;
            &lt;/resource&gt;

            &lt;resource&gt;
                &lt;directory&gt;src/main/java&lt;/directory&gt;
                &lt;includes&gt;
                    &lt;include&gt;**/*.properties&lt;/include&gt;
                    &lt;include&gt;**/*.xml&lt;/include&gt;
                &lt;/includes&gt;
                &lt;filtering&gt;false&lt;/filtering&gt;
            &lt;/resource&gt;

        &lt;/resources&gt;
    &lt;/build&gt;

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.