maven 關於構建版本號碼,以及在構建的時候壓縮css,js並為這些檔案追加版本號碼

來源:互聯網
上載者:User

標籤:maven   壓縮   css   js   

最近負責公司一個項目架構的搭建,由於我們這邊是後端團隊,沒有專業的前端工程師支援我們,我就在這個搭建過程中遇到了一些前端問題,給大家分享一下。 主要分享點:
  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並為這些檔案追加版本號碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.