• Java 通过ant打包时给JS/Jsp/css等文件加上版本号 防止有缓存


    AddJsAndCssVersion.java

      1 import java.io.BufferedReader;
      4 import java.io.BufferedWriter;
      5 import java.io.File;
      6 import java.io.FileInputStream;
      7 import java.io.FileOutputStream;
      8 import java.io.InputStream;
      9 import java.io.InputStreamReader;
     10 import java.io.OutputStream;
     11 import java.io.OutputStreamWriter;
     12 import java.io.Reader;
     13 import java.io.Writer;
     14 import java.text.SimpleDateFormat;
     15 import java.util.ArrayList;
     16 import java.util.Date;
     17 import java.util.Iterator;
     18 import java.util.List;
     19 
     20 import org.apache.tools.ant.BuildException;
     21 import org.apache.tools.ant.Task;
     22 
     23 
     27 public class AddJsAndCssVersion extends Task {
     28 
     29     private String path;
     30 
     31     @Override
     32     public void execute() throws BuildException {
     33         Date date = new Date();
     34         SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
     35         String version = df.format(date);
     36         addVersion(path, version);
     37     }
     38 
     39     private void addVersion(String path, String version) {
     40         File dir = new File(path);
     41         File[] files = dir.listFiles();
     42         if (files == null)
     43             return;
     44         for (int i = 0; i < files.length; i++) {
     45             if (files[i].isDirectory()) {
     46                 addVersion(files[i].getAbsolutePath(), version);
     47             } else {
     48                 String strFileName = files[i].getAbsolutePath().toLowerCase();
     49                 //Add version in HTML/JSP page
     50                 if (strFileName.endsWith(".html") || strFileName.endsWith(".jsp")  ) {
     51                     System.out.println(strFileName);
     52                     InputStream is = null;
     53                     OutputStream os = null;
     54                     List<String> contentList = new ArrayList<String>();
     55 
     56                     try {
     57                         is = new FileInputStream(files[i]);
     58                         Reader r = new InputStreamReader(is);
     59                         BufferedReader br = new BufferedReader(r);
     60                         String line = null;
     61                         while ((line = br.readLine()) != null) {
     62                             String modLine = getModLine(line, version);
     63                             if (modLine != null) {
     64                                 line = modLine;
     65                             }
     66                             line = line + "
    ";
     67                             contentList.add(line);
     68                         }
     69                         br.close();
     70                         r.close();
     71                     } catch (Exception e) {
     72                         e.printStackTrace();
     73                     } finally {
     74                         if (null != is) {
     75                             try {
     76                                 is.close();
     77                             } catch (Exception e) {
     78                                 e.printStackTrace();
     79                             }
     80                         }
     81                     }
     82 
     83                     try {
     84                         os = new FileOutputStream(files[i]);
     85                         Writer w = new OutputStreamWriter(os);
     86                         BufferedWriter bw = new BufferedWriter(w);
     87                         for (Iterator<String> it = contentList.iterator(); it
     88                                 .hasNext();) {
     89                             String line = it.next();
     90                             bw.write(line);
     91                         }
     92                         bw.flush();
     93                         bw.close();
     94                         w.close();
     95                     } catch (Exception e) {
     96                         e.printStackTrace();
     97                     } finally {
     98                         if (null != os) {
     99                             try {
    100                                 os.close();
    101                             } catch (Exception e) {
    102                                 e.printStackTrace();
    103                             }
    104                         }
    105                     }
    106                 }
    107             }
    108         }
    109     }
    110 
    111     /**
    112      * Add version number for JS/CSS link.
    113      * */
    114     private String getModLine(String line, String version) {
    115         line.trim();
    116         if (line.indexOf("<script") !=-1
    117             && line.indexOf("type="text/javascript") !=-1
    118             && line.endsWith("</script>")) {
    119 
    120             int pos = line.indexOf(".js");
    121             String modLine = line.substring(0, pos) + ".js?version=" + version + ""></script>";
    122             return modLine;
    123 
    124         } else if (line.indexOf("<link ") !=-1
    125             && line.indexOf("rel="stylesheet"") !=-1
    126             && line.indexOf("type="text/css"") !=-1
    127             && ( line.endsWith("/>") || line.endsWith(">"))) {
    128 
    129             int pos = line.indexOf(".css");
    130             String modLine = line.substring(0, pos) + ".css?version=" + version  + ""/>";
    131             return modLine;
    132 
    133         } else {
    134             return null;
    135         }
    136 
    137     }
    138 
    139     public void setPath(String path) {
    140         this.path = path;
    141     }
    142 
    143     public String getPath() {
    144         return this.path;
    145     }
    146 }

     达到的效果如下图:

    其中build.xml中代码参考上一篇文章,搜索AddJsAndCssVersion。

  • 相关阅读:
    C#中枚举的使用
    04 Spring的依赖注入
    03 Spring对Bean的管理
    02 Spring IOC
    01-Spring概述
    java8-方法引用
    java8-Stream
    java8-lambda
    centos中执行apt-get命令提示apt-get command not found
    Eclipse里git提交冲突rejected – non-fast-forward
  • 原文地址:https://www.cnblogs.com/yunyunde/p/8336489.html
Copyright © 2020-2023  润新知