java 根据 SVN changeLog 文件 生成增量升级包

        近日在做项目的时候 遇到了个问题,因为本地环境与生产环境 不一致,所以修改之后的代码不能整体打包 放到生产环境,而是只能以增量包的形式升级,手动打包 费时费力 而且容易出错,于是参照网上一些博客开发了一个增量包打包工具,好闲言少叙,上代码。

        java 根据 SVN changeLog 文件 生成增量升级包_第1张图片

1. 首先根据上图方式 获取svn历史提交记录,然后单击其中一条,右键--》Generate changeLog--》svn log with affected paths--》ok 生成changeLog文件。

2. 根据自身的项目路径初始化以下路径:

        //svn生成的历史修改文件记录
	static String CHANGE_LOG_FILE_NAME = "d:/changeLog.txt";
	//changeLog文件中 多余的路径前缀
	static String CHANGE_LOG_PREFIX = "d:/main/";
	//classes 文件夹本地路径
	static String CLASS_SOURCE_PREFIX = "d:/main/WebRoot/WEB-INF/classes/";
	//webroot 文件夹本地路径
	static String JSP_SOURCE_PREFIX = "d:/main/WebRoot/";
	//生成 增量包目标文件夹
	static String TARGET_PREFIX = "d:/main/release/webapps/";
	//增量包生成 主名称--后面会加上生成时间 年月日时分秒
	static String PACKAGE_NAME = "main";
	//项目WebRoot 文件夹
	static String WEBROOT = "WebRoot";
	//项目Classes 文件夹
	static String WEB_INF = "WEB-INF/classes/";
	//项目 src 文件夹
	static String SRC = "src";
	//项目配置文件 文件夹 
	static String ROURCE = "resources";
	static {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String dateStr = sdf.format(date);
		PACKAGE_NAME = PACKAGE_NAME + "_" + dateStr;
		File file = new File(TARGET_PREFIX + PACKAGE_NAME);
		if (file.exists()) {
			try {
				del(TARGET_PREFIX + PACKAGE_NAME);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			file.mkdirs();
		}
	}

3. 编写删除文件工具方法,用于删除已有重复文件:

/**
     * 
     * @param filepath
     * @throws IOException
     */
    public static void del(String filepath) throws IOException {
        File f = new File(filepath);// 定义文件路径
        if (f.exists() && f.isDirectory()) {// 判断是文件还是目录
            if (f.listFiles().length == 0) {// 若目录下没有文件则直接删除
                f.delete();
            } else {// 若有则把文件放进数组,并判断是否有下级目录
                File delFile[] = f.listFiles();
                int i = f.listFiles().length;
                for (int j = 0; j < i; j++) {
                    if (delFile[j].isDirectory()) {
                        del(delFile[j].getAbsolutePath());// 递归调用del方法并取得子目录路径
                    }
                    delFile[j].delete();// 删除文件
                }
            }
        }
    }

4. 编写文件复制工具方法,用于将本地编译目录文件复制到打包目录:

/**
	 * 复制文件
	 * @param sourceFile
	 * @param targetFile
	 * @throws Exception
	 */
    public static void copyFile(File sourceFile, File targetFile) throws Exception {
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;
        if (sourceFile.isFile()) {
	        try {
	            // 新建文件输入流并对它进行缓冲
	            inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
	            
	            if(!targetFile.exists()){
	            	try{
	            		targetFile.getParentFile().mkdirs();
	            		targetFile.createNewFile();
	            	}catch(Exception e){
	            		System.out.println(targetFile.getAbsolutePath());
	            		throw e;
	            	}
	            }
	            // 新建文件输出流并对它进行缓冲
	            outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
	
	            // 缓冲数组
	            byte[] b = new byte[256];
	            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();
	        }
        }
    }

4. 有了以上工具,才可以更好的实现 文件打包,下面 是应用上面方法,读取log文件 并打包的方法:

public static void main(String[] args) throws Exception {
		File file = new File(CHANGE_LOG_FILE_NAME);
		BufferedReader br = new BufferedReader(new FileReader(file));
		String line = null;
		System.err.println(PACKAGE_NAME);
		try {
			while ((line = br.readLine())!= null) {
				if (line.contains(CHANGE_LOG_PREFIX)) {
					String[] relativePaths = line.split(CHANGE_LOG_PREFIX);
					String relativePath = relativePaths[1];
					if (relativePath.startsWith(SRC+"/")) {
						if (relativePath.contains(".java")) {
							relativePath = relativePath.replaceAll(".java", ".class");
						}
						relativePath = relativePath.replaceAll(SRC+"/", "");
						String sourcePath = CLASS_SOURCE_PREFIX + relativePath;
						String targetPath = TARGET_PREFIX + PACKAGE_NAME + "/" + WEB_INF + relativePath;
						copyFile(new File(sourcePath), new File(targetPath));
						
					} else if (relativePath.startsWith(WEBROOT+"/")) {
						relativePath = relativePath.replaceAll(WEBROOT+"/", "");
						String sourcePath = JSP_SOURCE_PREFIX + relativePath;
						String targetPath = TARGET_PREFIX + PACKAGE_NAME + "/" + relativePath;
						copyFile(new File(sourcePath), new File(targetPath));
					} else if (relativePath.startsWith(ROURCE+"/")) {
						relativePath = relativePath.replaceAll(ROURCE+"/", "");
						String sourcePath = CLASS_SOURCE_PREFIX + relativePath;
						String targetPath = TARGET_PREFIX + PACKAGE_NAME + "/" + WEB_INF + relativePath;
						copyFile(new File(sourcePath), new File(targetPath));
					}
					
					System.err.println(relativePath);
				}
				
//				if(line) {
//					
//				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			br.close();
		}
	}

此方法是仁者见仁智者见智的方法,靠个人根据自身项目目录自由发挥,开发出适用于自己的打包工具,当然与此都是大同小异的。


注意:执行拷贝前,记得clean一下工程,确保编译好的是最新的class文件,还有内部类的class文件注意下,免得遗漏了!

以上文章是引用此文章修改所得,点击可查看


你可能感兴趣的:(个人应用总结)