Heim  >  Artikel  >  Web-Frontend  >  maven 关于构建版本号,以及在构建的时候压缩css,js并为这些文件追加版本号

maven 关于构建版本号,以及在构建的时候压缩css,js并为这些文件追加版本号

PHP中文网
PHP中文网Original
2016-06-24 11:54:411771Durchsuche


最近负责公司一个项目框架的搭建,由于我们这边是后端团队,没有专业的前端工程师支持我们,我就在这个搭建过程中遇到了一些前端问题,给大家分享一下。

 

主要分享点:

  1. 构建项目时自动在css,js文件名中加入版本号     解决新上线版本时,浏览器可以更新缓存

  2. 构建项目自动压缩css,js资源文件     加快响应速度

解决第一个问题,是在构建的时候必须生成一个构建版本号,比如构建时刻的timestamp,正好在网上看到这样的插件,配置信息如下

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>buildnumber-maven-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>create-timestamp</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <configuration>
                        <format>{0,date,yyyy-MM-dd HH:mm:ss}</format>
                        <items>
                            <item>timestamp</item>
                        </items>
                    </configuration>
                </configuration>
            </plugin>

这个插件的作用是在构建的时候在maven的生命周期validate阶段生成一个版本号,当你需要这个版本号的时候你可以通过${timestamp}引入,具体这个插件的使用可以查看官网:   http://mojo.codehaus.org/buildnumber-maven-plugin/index.html

有了这个版本号,接下来的问题就是在每个页面引入css,js的文件可以被copy一份名字以xx.{version}.css,xx.${version}.js的文件,这就要用到第二个插件了,看下面的配置信息:

 <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>yuicompressor-maven-plugin</artifactId>
                <version>1.5.0</version>
                <executions>
                    <execution>
                        <phase>${assert.compress}</phase> 
                        <goals>
                            <goal>compress</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--<nosuffix>true</nosuffix>-->
                    <suffix>.${timestamp}</suffix>
                    <force>true</force>
                    <encoding>utf-8</encoding>
                    <excludes>
                        <exclude>**/*.pack.js</exclude>
                        <exclude>**/compressed.css</exclude>
                        <exclude>**/*.min.css</exclude>
                        <exclude>**/*.min.js</exclude>
                    </excludes>
                </configuration>
            </plugin>



这个插件不但解决我上面分享里写的第二个压缩css,js文件,同时还可以把压缩后的文件名追加版本号,有了这个插件真是太好了,关于这个插件具体使用信息请看: http://davidb.github.io/yuicompressor-maven-plugin/index.html

看下下面的构建信息,注意框框里面的内容,可以看到jquery-ui.js被压缩成以文件名 jquery-ui.1414549216019.js命名的文件,同时这个插件还打印出了其它一些信息。

到这里,想要的样式文件都已经有了,紧接着要做的是怎么在文件内进行替换


以上就是maven 关于构建版本号,以及在构建的时候压缩css,js并为这些文件追加版本号的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn