工具篇(Java)--- 增量打包

目录

概述

准备

源码增量打包

编译后的文件增量打包

指定文件复制


概述

    由于大多的生产环境都采用的是增量发版,因此需要把把改变的代码打包,本文采用的是使用Java代码对

SVN上的更新记录选择后进行打包

准备

    到项目的SVN管理出找到历史更新记录,选择要打包的记录然后生成变更到本地即可,需要注意存放的位置

源码增量打包

package xxx.xxx.xxx.prdPacking;

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.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * 增量打包工具类,打包源代码</br>
 * 项目右键-->Team-->显示资源历史记录-->选中要更新的日志保存到changeLog文件
 * 
 */
public class IncrementalPackagingSrc {
	
	// ----------------------无需改动
	// 正则表达式
	private static final String HEADSTR = "^\t[M|A] $";
	// 分割线
	private static final String DIVIDING_LINE = "----------------------------------------------------------------------------";
	// 提交的文件说明list
	private static Set<String> COMMIT_CONTEXT_LIST = new HashSet<>();
	// 项目文件夹路径  
	private static String PROJECTPATH = System.getProperty("user.dir").replace("\\", "/");
    
	
	// ----------------------根据需要进行改动配置
	// 补丁文件,由eclipse svn plugin生成 
	private static final String PATCHFILE="D:/changeLog.txt";  // 导出的日志地址
	// 补丁文件包存放路径  
	private static final String DESPATH="D:/update_pkg";   // 存放文件的地址
	// 补丁版本 
	private static final String VERSION="xxx/xxx/xxx"; // 更详细的地址,比如日期,版本,项目名
    
    private static int COUNT = 0 ;
    
    public static void main(String[] args) {
		copyFiles(getPatchFileList());
	}
	/**
	 * 得到patch里面更改过的文件
	 * @return
	 * @throws Exception
	 */
    private static Set<String> getPatchFileList(){  
		FileInputStream f = null;   
		BufferedReader dr = null;  
		Set<String> fileList = new HashSet<>();
		try {
			f = new FileInputStream(PATCHFILE);  
			dr =new BufferedReader(new InputStreamReader(f,"utf-8"));
        	String line = "";  
        	String commit = "" ; 
        	while((line=dr.readLine())!=null){   
        		
        		// 保存提交时候说明内容
        		if (DIVIDING_LINE.equals(line)){
        			COMMIT_CONTEXT_LIST.add(commit);
        		}
        		commit = line;
        		// 匹配更新文件
        		if(line.length()>=3 && Pattern.matches(HEADSTR,line.substring(0, 3))){  
        			line = line.substring(line.indexOf("src/main"));
        			fileList.add(line);
        		}
        	}
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("文件集合error");
		}finally{
			if (null != dr) {
				try {
					dr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(null != f){
				try {
					f.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		System.out.println("--------------------------待更新文件集合 start---文件个数:"+fileList.size());
		for(String t:fileList){
			System.out.println(t);
		}
		System.out.println("--------------------------待更新文件集合   end ---文件个数:"+fileList.size());
		
		System.out.println("***************************待更新文件备注   start *****");
		for (String s : COMMIT_CONTEXT_LIST){
			System.out.println(s);
		}
		System.out.println("***************************待更新文件备注   end *****");
		return fileList;
    } 
	
	/**
	 * copy 文件 ,有一部分文件含有匿名内部类,会生成ClassName$1.class类型的文件,需要用正则表达式获取文件
	 * @param list
	 */
    private static void copyFiles(Set<String> list){  
		if(null == list || list.size() <=0 ){
			return ;
		}
		System.out.println("开始copy源码文件-------------------------------------------------------------------------");
        for(String fullFileName:list){ 
        	
    		String fileName=fullFileName; 
    		
    		fullFileName = PROJECTPATH+"/"+fullFileName;//将要复制的文件全路径 
    		
    		String desFileNameStr=DESPATH+"/"+VERSION+"/"+fileName; 
    		
    		String tempDesPathStr=desFileNameStr.substring(0,desFileNameStr.lastIndexOf("/"));
    		
    		File desFilePath=new File(tempDesPathStr);  
    		if(!desFilePath.exists()){  
    			desFilePath.mkdirs();  
    		}
    		copyFile(fullFileName, desFileNameStr);
    		System.out.println("普通文件:"+fullFileName);  
    		COUNT ++ ;
        }  
        System.out.println("-------共增量打包源码文件数量:" + COUNT);  
    } 
	
 	private static void copyFile(String sourceFileNameStr, String desFileNameStr) {  
        File srcFile=new File(sourceFileNameStr);  
        File desFile=new File(desFileNameStr);  
        if (srcFile.isDirectory() || desFile.isDirectory()) {
        	System.out.println("该路径是文件夹已经忽略打包:"+sourceFileNameStr);
        	return ;
        }
        try {  
            copyFile(srcFile, desFile);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
	
	
 	private 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();  
        }  
    }  
}

编译后的文件增量打包


package com.stream.util.prdPacking;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.stream.app.common.StringUtil;
import com.stream.util.DateUtil;

public class IncrementalPackaging {

	private final static String workSpace = "XXX"; // 工作空间
	private final static String installDir = "xxx"; // 包目录

	public static void main(String[] args) {
	
        // 要打包编译文件的地址和文件名
		incrementalPackaging("xxxxxxxxxxx");
);

	}

	/**
	 * 增量包打包
	 * 
	 * @param file1
	 * @param file2
	 */
	public static void incrementalPackaging(String file) {
		String file1 = workSpace + file;
		String file2 = installDir + DateUtil.getCurrentStrDate("yyyyMMddHHmm") + "/FMC/WEB-INF/classes/" + file;
		if (StringUtil.isNotNullOrEmpty(file1) && StringUtil.isNotNullOrEmpty(file2)) {
			// 如果创建成功,在进行复制文件
			if (createFile(file2)) {
				copyFile(new File(file1), new File(file2));
			}
		}
	}

	/**
	 * 复制文件
	 * 
	 * @param f1
	 * @param f2
	 */
	public static String copyFile(File f1, File f2) {
		try {
			int length = 2097152;
			FileInputStream in = new FileInputStream(f1);
			FileOutputStream out = new FileOutputStream(f2);
			byte[] buffer = new byte[length];
			while (true) {
				int ins = in.read(buffer);
				if (ins == -1) {
					in.close();
					out.flush();
					out.close();
					return "操作成功!";
				} else {
					out.write(buffer, 0, ins);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 创建文件
	 * 
	 * @param destFileName
	 * @return
	 */
	public static boolean createFile(String destFileName) {
		File file = new File(destFileName);

		if (file.exists()) {
			System.out.println("创建单个文件" + destFileName + "失败,目标文件已存在!");
			return true;
		}
		if (destFileName.endsWith(File.separator)) {
			System.out.println("创建单个文件" + destFileName + "失败,目标文件不能为目录!");
			return false;
		}

		// 判断目标文件所在的目录是否存在
		if (!file.getParentFile().exists()) {
			// 如果目标文件所在的目录不存在,则创建父目录
			System.out.println("目标文件所在目录不存在,准备创建它!");
			if (!file.getParentFile().mkdirs()) {
				System.out.println("创建目标文件所在目录失败!");
				return false;
			}
		}

		// 创建目标文件
		try {
			if (file.createNewFile()) {
				System.out.println("创建单个文件" + destFileName + "成功!");
				return true;
			} else {
				System.out.println("创建单个文件" + destFileName + "失败!");
				return false;
			}
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("创建单个文件" + destFileName + "失败!" + e.getMessage());
			return false;
		}
	}
}

指定文件复制

      用于修改后的文件复制到其它路径或项目里(本案例用于同一个项目不同的甲方个性化需求的开发方便源码复制发版)


import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Logger;

/**
 * 该类用于各个项目上的个性化需求更改后的文件复制(可跨项目复制)
 * 把修改后的文件地址放在下面,可以省去创建文件夹或复制文件的过程
 * 并在复制完成后自动打开文件位置,方便后续操作
 */
public class ChangeFileCopy {

    // 目标项目名称(根据自己的支线项目名称更改)
    private final static String TARGETNAME = "ats_gf_branch";

    // 原文件详细地址(点击要复制的文件Copy Path然后放到下面,多个文件以','分割)
    private final static String SOURCEFILES = "D:\\IdeaProjects\\ats_gf\\fundats\\ats-core-db\\src\\main\\java\\com\\hundsun\\fund\\ats\\core\\warning\\dao\\impl\\WarningInfosDaoImpl.java" +
            ",D:\\IdeaProjects\\ats_gf\\fundats\\ats-modules-server\\src\\main\\java\\com\\hundsun\\fund\\ats\\modules\\server\\warning\\WarningBO.java";


    public static void main(String[] args) {
        CopyFiles(SOURCEFILES);
    }


    /**
     * @param string 文件地址字符串
     */
    public static void CopyFiles(String string) {

        // 日志记录
        Logger logger = Logger.getAnonymousLogger();
        int count = 0;

        // 文件信息打印
        ArrayList<String> list = new ArrayList<String>();

        // 循环得到文件项目名和路径
        String projectName = null;
        String targetFile = null;
        String result = null;
        String[] arrTarget = string.split(",");
        for (String dir :
                arrTarget) {
            String[] split = dir.split("\\\\");

            for (int i = 0; i < split.length; i++) {
                if ("fundats".equals(split[i])) {
                    projectName = split[i - 1];
                }
            }

            targetFile = dir.replace(projectName, TARGETNAME);

            try {
                // 创建文件目录
                CreateFile(targetFile);
            } catch (Exception e) {
                int lastIndexOf = targetFile.lastIndexOf("\\");
                String substring = targetFile.substring(0, lastIndexOf);
                String dirString = substring.replace("\\", "\\\\");

                logger.warning("创建目录失败{" + dirString + "}");
                continue;
            }

            try {
                // 复制文件
                result = CopyFile(dir, dir.replace(projectName, TARGETNAME));
                list.add(result);
            } catch (Exception e) {
                count++;
                logger.warning("复制文件失败{" + result + "}");
                continue;
            }
        }

        System.out.println("\n" + Arrays.toString(list.toArray()) + "\n文件复制结束,成功数量为:" + (arrTarget.length - count));
    }

    /**
     * 创建文件夹目录
     *
     * @param targetFile 目标文件地址
     */
    public static void CreateFile(String targetFile) {

        int lastIndexOf = targetFile.lastIndexOf("\\");
        String substring = targetFile.substring(0, lastIndexOf);
        String dirString = substring.replace("\\", "\\\\");

        // 创建文件夹
        try {
            File file = new File(dirString);
            file.mkdirs();
        } catch (Exception e) {
            System.out.println("创建目录失败 --- " + dirString);
        }
    }


    /**
     * 文件复制
     *
     * @param sourceFile 原文件地址
     * @param targetFile 目标件地址
     */
    public static String CopyFile(String sourceFile, String targetFile) {

        FileInputStream input = null;
        FileOutputStream output = null;
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;

        File inFile = new File(sourceFile);
        File outFile = new File(targetFile);
        // 新建文件输入流并对它进行缓冲
        try {
            input = new FileInputStream(inFile);
            inBuff = new BufferedInputStream(input);

            // 新建文件输出流并对它进行缓冲
            output = new FileOutputStream(outFile);
            outBuff = new BufferedOutputStream(output);

            // 缓冲数组
            byte[] b = new byte[1024];
            int len;
            while ((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            // 刷新此缓冲的输出流
            outBuff.flush();

            // 文件复制成功后并打开文件位置,方便查看和操作
            openDirAndSelectFile(targetFile);

        } catch (IOException e) {
            System.out.println("文件复制出错:" + e.getMessage());
        }

        //关闭流
        try {
            inBuff.close();
            outBuff.close();
            output.close();
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return inFile.getName();
    }

    /**
     * 打开文件
     *
     * @param targetFile 目标文件地址
     */
    public static void openFile(String targetFile) {
        File file = new File(targetFile);
        try {
            Desktop.getDesktop().open(file);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * 打开文件目录并选定文件
     *
     * @param targetFile 目标文件地址
     */
    public static void openDirAndSelectFile(String targetFile) {
        try {
            Runtime.getRuntime().exec("explorer /select, " + targetFile);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值