zlb

利用svn的补丁文件打包生成增量文件

下面的代码是maven版本

1. 创建patch.txt增量文件

保存到 文件目录下

比如 E:\aa\patch.txt

2. 编写java代码

package utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/*
 * 利用svn的补丁文件打包生成增量文件
 * 参考 https://blog.csdn.net/happydecai/article/details/81940484
 * 
 * */

public class FreePatchUtil {
	public static String patchFile = "E:\\patch.txt";// 补丁文件,由eclipse svn plugin生成
	public static String projectPath ="E:\\Projects\\SourceCode\\adpp";// "D:\\javaprojects\\portal\\tepin-manage";// 项目文件夹路径
	public static String webContent = "src/main/webapp";// web应用文件夹名
	public static String classPath ="F:\\tomcat-7.0.69\\webapps\\adpp\\WEB-INF\\classes"; //"D:\\javaprojects\\portal\\tepin-manage\\target\\classes";// class存放路径
	public static String desPath ="E:\\zengliangbao" ;//"C:\\Users\\sz\\Documents\\portal_for_bi";// 补丁文件包存放路径
	public static String version = "patch_20190411";//+UtilDateTime.getNowTime().substring(0, 10);// 补丁版本
	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		copyFiles(getPatchFileList());
	}
 
	/****
	 * 读取补丁配置文件解析出修改的文件并返回到list集合
	 * 
	 * @return
	 * @throws Exception
	 */
	public static List<String> getPatchFileList() throws Exception {
		List<String> fileList = new ArrayList<String>();
		FileInputStream f = new FileInputStream(patchFile);
		BufferedReader dr = new BufferedReader(new InputStreamReader(f, "utf-8"));
		String line;
		while ((line = dr.readLine()) != null) {
			if (line.startsWith("Index:")) {
				line = line.replaceAll(" ", "");
				line = line.substring(line.indexOf(":") + 1, line.length());
				fileList.add(line);
			}
		}
		dr.close();
		return fileList;
	}
 
	/***
	 * 
	 * @param list
	 *            修改的文件
	 */
	public static void copyFiles(List<String> list) {
 
		for (String fullFileName : list) {
			if (fullFileName.indexOf("src/main/webapp") == -1) {// 对源文件目录下的文件处理
				String fileName = fullFileName.replace("src", "");//目标路径
				fullFileName = classPath + fileName;
				if (fileName.endsWith(".java")) {
//					fileName = fileName.replace(".java", ".class").replace("/main/java", "");
//					fullFileName = fullFileName.replace(".java", ".class").replace("/main/java", "");
					int i= fileName.indexOf("/com");
					//System.out.println(i);
					if (i>0) {
						fileName=fileName.substring(i);
					}
					//System.out.println(fileName);
					fileName = fileName.replace(".java", ".class").replace("/main/java", "");
					//System.out.println(fileName);
					fullFileName = classPath + fileName;
					fullFileName = fullFileName.replace(".java", ".class").replace("/main/java", "");
					//System.out.println("fullFileName>"+fullFileName);
				}else{
					fileName = fileName.replace("/main/resources", "");
					fullFileName = fullFileName.replace("/main/resources", "");
				}
				String tempDesPath = fileName.substring(0,fileName.lastIndexOf("/"));
				String desFilePathStr = desPath + "/" + version + "/WEB-INF/classes" + tempDesPath;
				String desFileNameStr = desPath + "/" + version + "/WEB-INF/classes" + fileName;
				File desFilePath = new File(desFilePathStr);
				if (!desFilePath.exists()) {
					desFilePath.mkdirs();
				}
				copyFile(fullFileName, desFileNameStr);
				System.out.println(fullFileName + "复制完成");
				// 遍历目录,是否存在内部类,如果有内部,则将所有的额内部类挑选出来放到
				//copyInnerClassFile(fullFileName, desFileNameStr);
			} else {// 对普通目录的处理
				String desFileName = fullFileName.replace(webContent, "");
				fullFileName = projectPath + "/" + fullFileName;// 将要复制的文件全路径
				String fullDesFileNameStr = desPath + "/" + version + "/" + desFileName;
				String desFilePathStr = fullDesFileNameStr.substring(0,fullDesFileNameStr.lastIndexOf("/"));
				File desFilePath = new File(desFilePathStr);
				if (!desFilePath.exists()) {
					desFilePath.mkdirs();
				}
				copyFile(fullFileName, fullDesFileNameStr);
				System.out.println(fullFileName + "复制完成");
 
			}
 
		}
 
	}
 
	/***
	 * 处理内部类的情况 解析源路径名称,遍历此文件路径下是否存在这个类的内部类 内部类编译后的格式一般是
	 * OuterClassName$InnerClassName.class
	 * 
	 * @param sourceFullFileName
	 *            原路径
	 * @param desFullFileName
	 *            目标路径
	 */
	@SuppressWarnings("unused")
	private static void copyInnerClassFile(String sourceFullFileName,String desFullFileName) {
		String sourceFileName = sourceFullFileName.substring(sourceFullFileName.lastIndexOf("/") + 1);
		String sourcePackPath = sourceFullFileName.substring(0, sourceFullFileName.lastIndexOf("/"));
		String destPackPath = desFullFileName.substring(0, desFullFileName.lastIndexOf("/"));
		String tempFileName = sourceFileName.split("\\.")[0];
		File packFile = new File(sourcePackPath);
		if (packFile.isDirectory()) {
			String[] listFiles = packFile.list();
			for (String fileName : listFiles) {
				// 可以采用正则表达式处理
				if (fileName.indexOf(tempFileName + "$") > -1 && fileName.endsWith(".class")) {
					String newSourceFullFileName = sourcePackPath + "/" + fileName;
					String newDesFullFileName = destPackPath + "/" + fileName;
					copyFile(newSourceFullFileName, newDesFullFileName);
					System.out.println(newSourceFullFileName + "复制完成");
				}
			}
		}
 
	}
 
	private static void copyFile(String sourceFileNameStr, String desFileNameStr) {
		File srcFile = new File(sourceFileNameStr);
		File desFile = new File(desFileNameStr);
		try {
			copyFile(srcFile, desFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 
	public static void copyFile(File sourceFile, File targetFile)
			throws IOException {
		BufferedInputStream inBuff = null;
		BufferedOutputStream outBuff = null;
		try {
			// 新建文件输入流并对它进行缓冲
			inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
 
			// 新建文件输出流并对它进行缓冲
			outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
 
			// 缓冲数组
			byte[] b = new byte[1024 * 5];
			int len;
			while ((len = inBuff.read(b)) != -1) {
				outBuff.write(b, 0, len);
			}
			// 刷新此缓冲的输出流
			outBuff.flush();
		} finally {
			// 关闭流
			if (inBuff != null)
				inBuff.close();
			if (outBuff != null)
				outBuff.close();
		}
	}
}

posted on 2019-04-10 13:58  zlb  阅读(706)  评论(0编辑  收藏  举报

导航