java项目上线时摘取更新文件工具

每当项目打补丁上线时,摘取文件是一件很头痛的事,以下代码很好的解决了这个问题。代码如下:


package com;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class GetUpdateFiles {
	public static void main(String[] args) {
		/**
		 *
		 */
		int types[] ={0};
		buildOneTime(types);
	}
	public static void buildOneTime(int[] types){
		for (int i = 0; i < types.length; i++) {
			biuld(types[i],false);
		}
	}
	/**
	 * 
	 * @param pronum 工程序号
	 * @param to6666 是否生成到6666目录,否则生成到版本发布目录	
	 */
	public static void biuld(int pronum,boolean to6666){
		String scnames [] = {"USDApp","USDServer"};//WEB应用将要发布名称
		String names [] = {"HE_USD","HE_USD_Server"};//web应用在本机上的发布名称
		String pathnames [] = {"app","server"};//应用更新文件名称,如2016_01_app.txt
		//修改此处 来修改扫描的工程
		String pronames [] = {"HE_USD","HE_USD_Server_10"};//工程名称
		Calendar cal = Calendar.getInstance();
		int year = cal.get(cal.YEAR);
		int month = cal.get(cal.MONTH)+1;
		boolean isclient = false;
		if(pronum==4){
			isclient = true;
		}
		String listPathFile = "F:/上线更新文件/"+year+"/"+month+"/"+year+"_"+month+"_"+pathnames[pronum]+".txt";
		System.out.println(listPathFile);
		//��������Ŀ��Դ·��
		String appName =names[pronum];
		String sourceApp = null;
		sourceApp = "D:/workspace/"+pronames[pronum]+"/WebRoot/";
		String destinationApp = "";
		if(!to6666){
			destinationApp = "F:/platform/producation/release/"+String.valueOf(year).subSequence(2, 4)+"."+month+".01/"+scnames[pronum];
		}else{
			destinationApp = "F:/platform/staging/"+scnames[pronum];
		}
		GetUpdateFiles.initByJava(listPathFile, sourceApp, destinationApp,appName);
//		String sourceJavaApp = "D:/workspacenew/HE_USD_New/";
//		initJavaFiles(listPathFile, sourceJavaApp, destinationApp,appName);
	}
	public static void initByClass(String listPathFile, String sourceApp,
			String destinationApp,String appName){
		init(listPathFile, sourceApp, destinationApp,"class",appName);
	}
	
	
	
	
	public static void initByJava(String listPathFile, String sourceApp,
			String destinationApp,String appName){
		init(listPathFile, sourceApp, destinationApp,"java",appName);
	}
	private static void init(String listPathFile, String sourceApp,
			String destinationApp,String type,String appName) {
		List fileList = new ArrayList();
		fileList = insertFiles(fileList, listPathFile);
		for (int i = 0; i < fileList.size(); i++) {
			String sourceRealPath = null;
			if(type.equals("java")){
				sourceRealPath = convert(fileList.get(i).toString(),appName);
			}else{
				sourceRealPath = fileList.get(i).toString();
			}
			File currfile = new File(sourceApp + sourceRealPath);
			if (!currfile.exists()) {
				System.err.println("---" + sourceApp + sourceRealPath);
			}
			fileCopy(currfile, new File(destinationApp + sourceRealPath));
			System.out.println("文件放置到:"+destinationApp + sourceRealPath);
		}
	}
	private static void initJavaFiles(String listPathFile, String sourceApp,
			String destinationApp,String appName) {
		List fileList = new ArrayList();
		fileList = insertFiles(fileList, listPathFile);
		for (int i = 0; i < fileList.size(); i++) {
			String sourceRealPath = null;
			String [] sd = fileList.get(i).toString().split("/"+appName+"/");
			File currfile = new File(sourceApp + sd[1]);
			if (!currfile.exists()) {
				System.err.println("文件不存在" + sourceApp + sourceRealPath);
			}
			fileCopy(currfile, new File(destinationApp + sourceRealPath));
			System.out.println("文件放置到:"+destinationApp + sd[1]);
		}
	}
	private static String convert(String path,String appName){
		if(path.endsWith(".java")&&path.contains("/"+appName+"/src")){
			path = path.substring(path.indexOf("/"+appName+"/src"), path.length()).replace("/"+appName+"/src", "/WEB-INF/classes").replace(".java", ".class");
		}
		if(path.contains("/"+appName+"/Resources")){
			path = path.substring(path.indexOf("/"+appName+"/Resources"), path.length()).replace("/"+appName+"/Resources", "/WEB-INF/classes");
		}
		if(path.contains("/"+appName+"/WebRoot")){
			path = path.substring(path.indexOf("/"+appName+"/WebRoot"), path.length()).replace("/"+appName+"/WebRoot", "");
		}
		return path;
	}
	private static List insertFiles(List fileList, String sourceFile) {
		BufferedReader br;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(
					new File(sourceFile)), "utf8"));
			String str = null;
			while ((str = br.readLine()) != null) {
				if (!str.contains("/**")&&!str.trim().equals("")) {
					fileList.add(str);
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return fileList;
	}

	private static void fileCopy(File sf, File df) {
		System.out.println("正在复制" + sf.getPath());
		if (!df.exists()) {
			String path = df.getPath();
			String direct = path.substring(0, path.lastIndexOf("\\"));
			File file = new File(direct);
			file.mkdirs();
		}
		try {
			FileInputStream input = new FileInputStream(sf);
			FileOutputStream output = new FileOutputStream(df);
			byte[] b = new byte[1024 * 5];
			int len;
			while ((len = input.read(b)) != -1) {
				output.write(b, 0, len);
			}
			output.flush();
			output.close();
			input.close();
		} catch (Exception e) {
			System.err.println("�����ļ�" + sf.getPath() + "���?");
			e.printStackTrace();
			e.getStackTrace();
		}
	}
}


你可能感兴趣的:(java,随笔)