1 静态资源统一添加资源文件版本号有三种办法,
使用相关的资源管理插件生成对应的资源文件引用(这种太笨重,个人原因没有选择这种方式)
使用Maven统一打包生成(由于需要改变工程结构,感觉仅仅为了添加版本号没有必要这样搞)
使用document动态打印script引用,这种方式每次都需要在客户端动态打印
基于以上原因,自己动手写了个小程序统一解决,一下为源码:
public class FileParser {
//收集所有的页面文件,包括jsp和html
public static List<File> htmlFile = new ArrayList<File>();
public static void main(String[] args) {
/*
* 获取项目路径
*/
String webRoot = "E:/workcode/qikupassport/WebRoot"; //原文件根目录
File rootDir = new File(webRoot);
List<File> htmlFile = managerFile(rootDir);// 获取所有的jsp,html文件
/*
* 生成版本号
*/
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String versionNum = sdf.format(new Date());
/*
* 循环遍历所有的文件
*/
for (File file : htmlFile) {
String fileName = file.getPath().substring(3);
String filePath = "E:/dst/" + file.getParent().substring(3);// 生成目标文件目录
/*
* 创建新目录
*/
File fileDir = new File(filePath);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));//读取文件
File targetFile = new File("E:/dst/" + fileName);
/*
* 生成新文件
*/
if (!targetFile.exists()) {
targetFile.createNewFile();
}
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(targetFile)));
String line = reader.readLine();
while (null != line) {
/*
* 给引用的js自动添加版本号
*/
String regEx = "(\\s+<script|<script).*(src=\".*\")";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(line);
if(m.find()){
String srcStr = m.group(2);
String dstStr = srcStr.substring(0, srcStr.length()-1)+"?v="+versionNum+"\"";
line = line.replaceAll(srcStr, dstStr);
}
/*
* 给引用的css自动添加版本号
*/
String linkRegEx = "(\\s+<link|<link).*(href=\".*?\")";
p = Pattern.compile(linkRegEx);
m = p.matcher(line);
if(m.find()){
String srcStr = m.group(2);
String dstStr = srcStr.substring(0, srcStr.length()-1)+"?v="+versionNum+"\"";
line = line.replaceAll(srcStr, dstStr);
}
/*
* 给引用的img自动添加版本号
*/
String imageRegEx = ".*<img.*(src=\"https:.*?\")";
p = Pattern.compile(imageRegEx);
m = p.matcher(line);
if(m.find()){
String srcStr = m.group(1);
System.out.println(srcStr);
String dstStr = srcStr.substring(0, srcStr.length()-1)+"?v="+versionNum+"\"";
line = line.replaceAll(srcStr, dstStr);
}
writer.write(line+"\r\n");
line = reader.readLine();
}
reader.close();
writer.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
/**
* 遍历跟文件夹并过滤目标文件
* @param rootFile
* @return
*/
public static List<File> managerFile(File rootFile) {
File[] rootFiles = rootFile.listFiles();
for (File file : rootFiles) {
if (file.isDirectory()) {
managerFile(file);
} else {
/*
* 收集css jsp html文件
*/
if (file.getName().endsWith(".css")// || file.getName().endsWith(".js")
|| file.getName().endsWith(".jsp") || file.getName().endsWith(".html")) {
htmlFile.add(file);
}
}
}
return htmlFile;
}
}
remark:由于还不完善所以在使用时html页面中所有的引用文件不能换行,例如
<link rel="stylesheet" href="https://***/pass/2.0/login/public.css" type="text/css" />
不能写成
<link rel="stylesheet"
href="https://***/pass/2.0/login/public.css" type="text/css" />