web项目部署后删除xml中的字段注释

1 篇文章 0 订阅
1 篇文章 0 订阅

 

       项目上线后,由于某些原因.项目发布到服务器后,我们可能会删除开发时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();
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值