项目上线后,由于某些原因.项目发布到服务器后,我们可能会删除开发时xml中字段注释.这里方案是部署后,处理部署后的文件,在应用根目录下遍历xml文件.找到注释内容然后替换为空. 下边是打好的jar包,直接放在你需要处理的发布好了的应用下执行.以tomcat为例就是webapp下由tomcat解压后的应用目录里.后面是源码
jar包地址下载:
https://pan.lanzou.com/i11m79a
备用外链:
https://d.lanzou.com/i11m79a
在cmd中执行: java -jar xmlCommentRemover.jar
打包时用的jdk1.8, 1.8以下报错用这个,重新用1.7打包了下
https://www.lanzous.com/i1nwh4h
package com.kimipoker.xmlHandle;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @program: myTest
* @description: 移除xml注释
* @author: kimipoker
* @create: 2018-05-17 00:24
**/
public class XmlCommentRemover {
private HashSet<File> fileset =new HashSet<File>();
public HashSet<File> getFileset() {
return fileset;
}
// 递归遍历
private void getDirectory(File file,String type) {
File flist[] = file.listFiles();
if (flist == null || flist.length == 0) {
return ;
}
for (File f : flist) {
if (f.isDirectory()) {
getDirectory(f,type);
} else {
if(type==null || type.equals("")){
fileset.add(f);
}else if(f.getName().endsWith(type)){
fileset.add(f);
}
}
}
}
public static void main(String[] args) throws IOException {
XmlCommentRemover handler=new XmlCommentRemover();
String path = handler.getClass().getResource("").toString();
File directory = new File("");
String courseFile = directory.getCanonicalPath();
handler.getDirectory(new File(courseFile),".xml");
int threadNum=10;
ExecutorService service = Executors.newFixedThreadPool(threadNum);
for(File f:handler.getFileset()){
service.execute(new XmlHandler(f));
}
service.shutdown();
}
}
class XmlHandler implements Runnable{
/**
* 匹配注释包括多行注释
*/
public static Pattern regcomm= Pattern.compile("<!--.*?-->",Pattern.DOTALL);
private File file=null;
public XmlHandler(File file) {
this.file = file;
}
@Override
public void run() {
String content = null;
try {
content = FileUtils.readFileToString(file,"utf-8");
} catch (IOException e) {
e.printStackTrace();
}
Matcher matcher = regcomm.matcher(content);
String s1 = matcher.replaceAll("");
try {
FileUtils.write(file,s1,"utf-8");
System.out.println("thread_id_"+Thread.currentThread().getId()+" handled "+file.getName());
} catch (IOException e) {
e.printStackTrace();
}
}
}