Learning notes: Maven constructs version number methods to resolve browser cache issues

Source: Internet
Author: User

Issues that need to be addressed

In the web system development, in order to improve performance will take advantage of the browser's caching function, in fact, even if not explicitly declare the cache, the modern browser will be static files (JS, CSS, pictures, etc.) cache. But it is also because of this problem that the cache logic of the resource is sometimes problematic after the server's latest version of the file cannot update the client's cache.

This problem will cause a lot of trouble to the user, of course, first of all, the testers will be very headache, some seemingly not fixed bugs why development to say good? This time I will be helpless to say: Ctrl+f5 refresh a bit. But this is not the way to solve the problem.

Thoughts and methods of thinking

  There's no way to deal with such a problem before, just knowing that the cache can be resolved by timestamp, version stamp. To put it bluntly, the URL of the link is not the same browser will naturally download the latest version, think this is not very easy things. The project is not going to be a problem, so we have time to solve it.

Well, it's time to start solving. Find a lot of ways to find this is really simple plus a version number/timestamp can be resolved:

<rel= "stylesheet"  type= "text/css"  href= "/css/ Common.css?ver=${sysversion} "/>

In the above code, a resource is appended with a ? Ver=${sysversion}

Note: Velocity is used in the project, so ${sysversion} refers to the variable in velocity.

The new question

But a new problem arises, and we use Hudsun+maven to construct and automatically publish and deploy to the Web server. So how do we make a version stamp on the MAVEN construct?

Method
    1. A version number is written to a file by a method, and the version number is loaded when the system starts, so that velocity can use the version number to resolve the issue of cache updates. This is a single unified version number method.
    2. Through the front-end compiler tools, such as Grunt, F.I.S, such as tools to complete, especially F.I.S can implement MD5-stamp mode, directly for a single resource version update, so that the more accurate and maximize the role of caching.

Finally, I chose the first method, because the method of integrating the front-end compiler tool I did not fix t_t, reason:

1, F.I.S need to be based on node. JS, this has to configure a lot of things, a little trouble, when I set up the environment and repeated many days, and later found that the network wall reasons.

2, in addition, because the project does not very standard, so F.I.S generation need to change more code, this is my lazy people do not want to do

So the first to use a simple method to solve, and finally after 1 days of configuration is indeed completed.

Use the simplest single version number

A single version number makes it easy to solve the problem at once, just to generate a version number at the time of release, regardless of the timing, number, or whatever, all resources refer to this version number. Found a maven plugin: maven-svn-revision-number-plugin, this plugin can get the SVN version number as the build number, the advantage is that as long as there is a code to submit a new version will be generated. The code for the plugin is as follows:

          <plugin>              <groupId>Com.google.code.maven-svn-revision-number-plugin</groupId>              <Artifactid>Maven-svn-revision-number-plugin</Artifactid>              <version>1.7</version>              <Configuration>                  <verbose>True</verbose>                  <Entries>                      <entry>                          <prefix>Prefix</prefix>< prefix!--version stamp---                          <Depth>Empty</Depth>                      </entry>                  </Entries>              </Configuration>              <executions>                  <Execution>                      <Phase>Validate</Phase><!--life cycle phase --                      <Goals>                          <goal>Revision</goal>                      </Goals>                  </Execution>              </executions>              <Dependencies>                  <Dependency>                    <groupId>Org.tmatesoft.svnkit</groupId><!--Because of the svn1.8 version, you need to rely on this plugin--                    <Artifactid>Svnkit</Artifactid>                    <version>1.8.9</version>                  </Dependency>              </Dependencies>          </plugin>  

The purpose of this plugin is to get the latest SVN version number when Maven constructs, and then use the filtering of MAVEN's resources to work with variables in each configuration file. This allows the version number to be written to the file when it is constructed.

The Resources code:

      <Resources>        <Resource>            <Directory>Src/main/resources</Directory>            <includes>                <include>**.properties</include><!--only handle the properties file --            </includes>            <filtering>True</filtering><!--means that the open variable is embedded --        </Resource>        <Resource>            <Directory>Src/main/resources</Directory>                <excludes>                    <Exclude>**.properties</Exclude>                </excludes>            <filtering>False</filtering>        </Resource>              </Resources>

Because the properties file is used as a carrier, so the above setting is to find the properties file and then write the version number, I built a build_version.properties file, which wrote a set of items:

Build.version=${prefix.committedrevision}
${prefix.committedrevision} to get the version number generated by the Maven-svn-revision-number-plugin plugin.

After this is set up, use Maven install to see the effect, after the construction of the target directory to find the Build_version.properties file to view the content:
build.version=114912

At this point, it becomes a number.

Here's a look at what variables are generated in the Maven-svn-revision-number-plugin plugin:

[INFO]---maven-svn-revision-number-plugin:1.7: Revision (default) @ ZRTC---[INFO]inspecting E:\workspace\web[INFO]prefix =prefix[INFO]depth =Empty[INFO]Report unversioned =true[INFO]Report ignored =false[INFO]Report Out-of-date =false[INFO]Collecting status information[INFO]114969 114912E:\workspace\web[INFO]Setting Properties[INFO]Prefix.repository =[INFO]Prefix.path =[INFO]Prefix.revision = 114969[INFO]Prefix.mixedrevisions =false[INFO]Prefix.committedrevision = 114912[INFO]Prefix.committeddate = 2015-10-23 09:03:39 +0800 (Fri, OCT 2015)[INFO]Prefix.status =[INFO]Prefix.specialstatus =

You can see that the plugin has several variables, where I use committedrevision, which is the last version of the Project SVN directory commit. revision is the latest version of SVN. You need to add prefix when using other files. This prefix, which is defined in the configuration file, can be seen in the configuration above.

< prefix >prefix</prefix><!--  version stamp prefixes-- 

This prefix can be named on its own.

The issue of resource version updates has been resolved since this feature was completed. Look at the browser front page source code:

<rel= "stylesheet"  href= "/css/h_login.css?ver=114912"  type= "Text/css">

Goals for the future

 Recently, the problem of resolving front-end client resource updates also saw some better ways, such as F.I.S, a front-end engineering building tool. In the process of understanding the knowledge of a front-end engineering concept, some large, but is indeed a future trend of development. In the B/s development more and more deep stage, browser + server This development platform has proved its superiority, mobile internet, cloud computing has already manifested this development trend. and the browser is the most popular software in the last 10 years? And the browser has proved its omnipotent, based on the HTML+CSS can be achieved countless seconds of the interface, the original common business systems are also moved to the B/s structure. What a great platform for the development of HTML is visible.

Is it a little outdated to stand in this age and not do something related to the times? So in the future, we should think about the modularization of the front-end system and finally realize the engineering.

Learning notes: Maven constructs version number methods to resolve browser cache issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.